Add Scan functional, tmp UI
This commit is contained in:
51
src/lib/common/services/scan.service.js
Normal file
51
src/lib/common/services/scan.service.js
Normal file
@ -0,0 +1,51 @@
|
||||
/**
|
||||
* @typedef {'unwrap' | 'deblur' | 'illumination_correct'} ScanEndpoint
|
||||
*/
|
||||
|
||||
/**
|
||||
* Отправляет изображение на обработку и возвращает обработанный blob
|
||||
* @param {Blob} imageFile - Исходный файл изображения
|
||||
* @param {ScanEndpoint} endpoint - Эндпоинт для обработки
|
||||
* @returns {Promise<Blob>} - Обработанное изображение
|
||||
*/
|
||||
export async function processImage(imageFile, endpoint) {
|
||||
const formData = new FormData();
|
||||
formData.append('file', imageFile);
|
||||
|
||||
const response = await fetch(`/api/scan/${endpoint}`, {
|
||||
method: 'POST',
|
||||
body: formData,
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const errorText = await response.text();
|
||||
throw new Error(`Ошибка обработки (${endpoint}): ${errorText}`);
|
||||
}
|
||||
|
||||
return await response.blob();
|
||||
}
|
||||
|
||||
/**
|
||||
* Создает URL для blob (для отображения в <img>)
|
||||
* @param {Blob} blob
|
||||
* @returns {string}
|
||||
*/
|
||||
export function createBlobUrl(blob) {
|
||||
return URL.createObjectURL(blob);
|
||||
}
|
||||
|
||||
/**
|
||||
* Скачивает blob как файл
|
||||
* @param {Blob} blob
|
||||
* @param {string} filename
|
||||
*/
|
||||
export function downloadBlob(blob, filename = 'processed.png') {
|
||||
const url = createBlobUrl(blob);
|
||||
const a = document.createElement('a');
|
||||
a.href = url;
|
||||
a.download = filename;
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
document.body.removeChild(a);
|
||||
URL.revokeObjectURL(url);
|
||||
}
|
||||
Reference in New Issue
Block a user