diff --git a/.yarn/patches/@napi-rs-system-ocr-npm-1.0.2-59e7a78e8b.patch b/.yarn/patches/@napi-rs-system-ocr-npm-1.0.2-59e7a78e8b.patch new file mode 100644 index 0000000000..5c64db053b --- /dev/null +++ b/.yarn/patches/@napi-rs-system-ocr-npm-1.0.2-59e7a78e8b.patch @@ -0,0 +1,30 @@ +diff --git a/index.js b/index.js +index dc071739e79876dff88e1be06a9168e294222d13..b9df7525c62bdf777e89e732e1b0c81f84d872f2 100644 +--- a/index.js ++++ b/index.js +@@ -380,7 +380,7 @@ if (!nativeBinding || process.env.NAPI_RS_FORCE_WASI) { + } + } + +-if (!nativeBinding) { ++if (!nativeBinding && process.platform !== 'linux') { + if (loadErrors.length > 0) { + throw new Error( + `Cannot find native binding. ` + +@@ -392,6 +392,13 @@ if (!nativeBinding) { + throw new Error(`Failed to load native binding`) + } + +-module.exports = nativeBinding +-module.exports.OcrAccuracy = nativeBinding.OcrAccuracy +-module.exports.recognize = nativeBinding.recognize ++if (process.platform === 'linux') { ++ module.exports = {OcrAccuracy: { ++ Fast: 0, ++ Accurate: 1 ++ }, recognize: () => Promise.resolve({text: '', confidence: 1.0})} ++}else{ ++ module.exports = nativeBinding ++ module.exports.OcrAccuracy = nativeBinding.OcrAccuracy ++ module.exports.recognize = nativeBinding.recognize ++} diff --git a/package.json b/package.json index e29c58c29b..0e6420ce03 100644 --- a/package.json +++ b/package.json @@ -72,7 +72,7 @@ "dependencies": { "@libsql/client": "0.14.0", "@libsql/win32-x64-msvc": "^0.4.7", - "@napi-rs/system-ocr": "^1.0.2", + "@napi-rs/system-ocr": "patch:@napi-rs/system-ocr@npm%3A1.0.2#~/.yarn/patches/@napi-rs-system-ocr-npm-1.0.2-59e7a78e8b.patch", "@strongtz/win32-arm64-msvc": "^0.4.7", "graceful-fs": "^4.2.11", "jsdom": "26.1.0", diff --git a/scripts/before-pack.js b/scripts/before-pack.js index f0d4bdb096..59c0a39171 100644 --- a/scripts/before-pack.js +++ b/scripts/before-pack.js @@ -7,15 +7,12 @@ const allArm64 = { '@img/sharp-darwin-arm64': '0.34.3', '@img/sharp-win32-arm64': '0.34.3', '@img/sharp-linux-arm64': '0.34.3', - '@img/sharp-linuxmusl-arm64': '0.34.3', '@img/sharp-libvips-darwin-arm64': '1.2.0', '@img/sharp-libvips-linux-arm64': '1.2.0', - '@img/sharp-libvips-linuxmusl-arm64': '1.2.0', '@libsql/darwin-arm64': '0.4.7', '@libsql/linux-arm64-gnu': '0.4.7', - '@libsql/linux-arm64-musl': '0.4.7', '@strongtz/win32-arm64-msvc': '0.4.7', '@napi-rs/system-ocr-darwin-arm64': '1.0.2', @@ -25,16 +22,13 @@ const allArm64 = { const allX64 = { '@img/sharp-darwin-x64': '0.34.3', '@img/sharp-linux-x64': '0.34.3', - '@img/sharp-linuxmusl-x64': '0.34.3', '@img/sharp-win32-x64': '0.34.3', '@img/sharp-libvips-darwin-x64': '1.2.0', '@img/sharp-libvips-linux-x64': '1.2.0', - '@img/sharp-libvips-linuxmusl-x64': '1.2.0', '@libsql/darwin-x64': '0.4.7', '@libsql/linux-x64-gnu': '0.4.7', - '@libsql/linux-x64-musl': '0.4.7', '@libsql/win32-x64-msvc': '0.4.7', '@napi-rs/system-ocr-darwin-x64': '1.0.2', diff --git a/src/main/services/ocr/OcrService.ts b/src/main/services/ocr/OcrService.ts index 20ea201226..dfd796346f 100644 --- a/src/main/services/ocr/OcrService.ts +++ b/src/main/services/ocr/OcrService.ts @@ -2,6 +2,7 @@ import { loggerService } from '@logger' import { isLinux } from '@main/constant' import { BuiltinOcrProviderIds, OcrHandler, OcrProvider, OcrResult, SupportedOcrFile } from '@types' +import { systemOcrService } from './builtin/SystemOcrService' import { tesseractService } from './builtin/TesseractService' const logger = loggerService.withContext('OcrService') @@ -34,7 +35,4 @@ export const ocrService = new OcrService() // Register built-in providers ocrService.register(BuiltinOcrProviderIds.tesseract, tesseractService.ocr.bind(tesseractService)) -if (!isLinux) { - const { systemOcrService } = require('./builtin/SystemOcrService') - ocrService.register(BuiltinOcrProviderIds.system, systemOcrService.ocr.bind(systemOcrService)) -} +!isLinux && ocrService.register(BuiltinOcrProviderIds.system, systemOcrService.ocr.bind(systemOcrService)) diff --git a/src/main/services/ocr/builtin/SystemOcrService.ts b/src/main/services/ocr/builtin/SystemOcrService.ts index f6fcfe32a7..34a8bb8ce9 100644 --- a/src/main/services/ocr/builtin/SystemOcrService.ts +++ b/src/main/services/ocr/builtin/SystemOcrService.ts @@ -1,5 +1,6 @@ import { isLinux, isWin } from '@main/constant' import { loadOcrImage } from '@main/utils/ocr' +import { OcrAccuracy, recognize } from '@napi-rs/system-ocr' import { ImageFileMetadata, isImageFileMetadata as isImageFileMetadata, @@ -20,8 +21,6 @@ export class SystemOcrService extends OcrBaseService { if (isLinux) { return { text: '' } } - - const { OcrAccuracy, recognize } = require('@napi-rs/system-ocr') const buffer = await loadOcrImage(file) const langs = isWin ? options?.langs : undefined const result = await recognize(buffer, OcrAccuracy.Accurate, langs) diff --git a/src/main/utils/ocr.ts b/src/main/utils/ocr.ts index d94fb002d1..446fbe63d6 100644 --- a/src/main/utils/ocr.ts +++ b/src/main/utils/ocr.ts @@ -1,8 +1,8 @@ import { ImageFileMetadata } from '@types' import { readFile } from 'fs/promises' +import sharp from 'sharp' const preprocessImage = async (buffer: Buffer): Promise => { - const sharp = require('sharp') return sharp(buffer) .grayscale() // 转为灰度 .normalize() diff --git a/src/renderer/src/pages/settings/DocProcessSettings/OcrImageSettings.tsx b/src/renderer/src/pages/settings/DocProcessSettings/OcrImageSettings.tsx index 8568c47011..b91c78b954 100644 --- a/src/renderer/src/pages/settings/DocProcessSettings/OcrImageSettings.tsx +++ b/src/renderer/src/pages/settings/DocProcessSettings/OcrImageSettings.tsx @@ -47,12 +47,14 @@ const OcrImageSettings = ({ setProvider }: Props) => { })) }, [getOcrProviderName, imageProviders, platformSupport]) + const isSystem = imageProvider.id === BuiltinOcrProviderIds.system + return ( <> {t('settings.tool.ocr.image_provider')}
- {!platformSupport && } + {!platformSupport && isSystem && }