From af05960cd37a5171a5820b90ac89fa083347bd94 Mon Sep 17 00:00:00 2001 From: icarus Date: Fri, 22 Aug 2025 16:54:57 +0800 Subject: [PATCH] =?UTF-8?q?feat(OCR=E6=9C=8D=E5=8A=A1):=20=E6=94=AF?= =?UTF-8?q?=E6=8C=81base64=E5=AD=97=E7=AC=A6=E4=B8=B2=E4=BD=9C=E4=B8=BAOCR?= =?UTF-8?q?=E8=BE=93=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 扩展tesseractOcr函数以接受base64字符串或图像文件作为输入 --- src/main/services/ocr/OcrService.ts | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/main/services/ocr/OcrService.ts b/src/main/services/ocr/OcrService.ts index a54c009cf8..11f3a41ec0 100644 --- a/src/main/services/ocr/OcrService.ts +++ b/src/main/services/ocr/OcrService.ts @@ -19,19 +19,24 @@ const logger = loggerService.withContext('main:OcrService') /** * ocr by tesseract - * @param file image file + * @param file image file or base64 string * @returns ocr result * @throws {Error} */ -const tesseractOcr = async (file: ImageFileMetadata): Promise => { +const tesseractOcr = async (file: ImageFileMetadata | string): Promise => { try { const worker = await getTesseractWorker() - const stat = statSync(file.path) - if (stat.size > 50 * MB) { - throw new Error('This image is too large (max 50MB)') + let ret: Tesseract.RecognizeResult + if (typeof file === 'string') { + ret = await worker.recognize(file) + } else { + const stat = statSync(file.path) + if (stat.size > 50 * MB) { + throw new Error('This image is too large (max 50MB)') + } + const buffer = await readFile(file.path) + ret = await worker.recognize(buffer) } - const buffer = await readFile(file.path) - const ret = await worker.recognize(buffer) return ret.data.text } catch (e) { logger.error('Failed to ocr with tesseract.', e as Error)