capacitor-community / image-to-text

Capacitor Plugin for image to text processing (OCR)
Other
28 stars 10 forks source link

Make it work with base64 image #12

Open onigetoc opened 5 months ago

onigetoc commented 5 months ago

I tryed to read base64 image with OCR without success. I use the capacitor camera and it worked well when i used : resultType: CameraResultType.Uri, Then:

const imageUrl = photo.webPath; 
this.lines = await this.processImage(imageUrl);

My processImage function for base64:

async processImage(base64: string): Promise<string[]> {
const data: TextDetections = await Ocr.detectText({ base64});
// I Also try
const data: TextDetections = await Ocr.detectText({base64: base64});

And the rest of the code to make it work.

But if i try to send base64 data resultType: CameraResultType.Base64,

const imageUrl = 'data:image/jpeg;base64,'+base64Image; (the console.log see the base64data)
this.lines = await this.processImage(imageUrl);

The ProcessingImage function:

sync processImage(filename: string): Promise<string[]> {
const data: TextDetections = await Ocr.detectText({ filename });

The rest of the code.

If i use the name "filename" and .Uri from Capacitor Camera it will work.

If i use the name "base64" and .Base64 from Capacitor Camera it will not work.

Error: src/app/home/home.page.ts:388:56 - error TS2345: Argument of type '{ base64: string; }' is not assignable to parameter of type 'DetectTextOptions'.
  Object literal may only specify known properties, and 'base64' does not exist in type 'DetectTextOptions'.

388     const data: TextDetections = await Ocr.detectText({base64: base64});

I also do not know what to do with these options and if i need it to make it work, (Not very well explained) DetectTextFileOptions | DetectTextBase64Options

I cannot find any example to make it work with base64 data and i didn't find one anywhere.