feat(ocr): enhance OCR service for Linux compatibility and update image processing

- Added conditional registration of the system OCR service for non-Linux platforms.
- Updated SystemOcrService to handle Linux by returning an empty text response.
- Modified image preprocessing to dynamically require the 'sharp' library, improving compatibility and performance.
- Included additional files in the electron-builder configuration for packaging.
This commit is contained in:
kangfenmao 2025-09-02 02:29:24 +08:00
parent 5ab98c9d05
commit 80f49aecd7
4 changed files with 14 additions and 8 deletions

View File

@ -62,6 +62,7 @@ files:
asarUnpack:
- resources/**
- '**/*.{metal,exp,lib}'
- 'node_modules/@img/sharp-libvips-*/**'
win:
executableName: Cherry Studio
artifactName: ${productName}-${version}-${arch}-setup.${ext}

View File

@ -1,7 +1,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')
@ -33,4 +33,8 @@ 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))
}

View File

@ -1,6 +1,5 @@
import { isMac, isWin } from '@main/constant'
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,
@ -15,12 +14,14 @@ import { OcrBaseService } from './OcrBaseService'
export class SystemOcrService extends OcrBaseService {
constructor() {
super()
if (!isWin && !isMac) {
throw new Error('System OCR is only supported on Windows and macOS')
}
}
private async ocrImage(file: ImageFileMetadata, options?: OcrSystemConfig): Promise<OcrResult> {
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)

View File

@ -1,8 +1,8 @@
import { ImageFileMetadata } from '@types'
import { readFile } from 'fs/promises'
import sharp from 'sharp'
const preprocessImage = async (buffer: Buffer): Promise<Buffer> => {
const sharp = require('sharp')
return sharp(buffer)
.grayscale() // 转为灰度
.normalize()