File size: 1,401 Bytes
f871fed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/**
 * OCR API Client
 * 
 * Functions for image processing and text extraction.
 */

import { apiClient } from './client';
import type { OCRResponse, OCRBase64Request, OCRStatusResponse } from '../types/ocr';

/**
 * Process a base64 encoded image with OCR
 */
export async function processImageBase64(data: OCRBase64Request): Promise<OCRResponse> {
  const response = await apiClient.post('/ocr/process', data);
  return response.data;
}

/**
 * Process an uploaded image file with OCR
 */
export async function processImageFile(
  file: File,
  structure: boolean = true
): Promise<OCRResponse> {
  const formData = new FormData();
  formData.append('file', file);
  formData.append('structure', String(structure));
  
  const response = await apiClient.post('/ocr/upload', formData, {
    headers: {
      'Content-Type': 'multipart/form-data',
    },
  });
  return response.data;
}

/**
 * Check OCR service status
 */
export async function getOCRStatus(): Promise<OCRStatusResponse> {
  const response = await apiClient.get('/ocr/status');
  return response.data;
}

/**
 * Convert File to base64 string
 */
export function fileToBase64(file: File): Promise<string> {
  return new Promise((resolve, reject) => {
    const reader = new FileReader();
    reader.readAsDataURL(file);
    reader.onload = () => resolve(reader.result as string);
    reader.onerror = error => reject(error);
  });
}