From 37d9ff59cc48adf8fd6ea00803495a7c8a8ee14d Mon Sep 17 00:00:00 2001 From: icarus Date: Fri, 22 Aug 2025 15:41:43 +0800 Subject: [PATCH] =?UTF-8?q?feat(ocr):=20=E6=B7=BB=E5=8A=A0OCR=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=E6=94=AF=E6=8C=81=E5=8F=8A=E6=96=87=E4=BB=B6=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B=E6=A0=A1=E9=AA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加OCR功能钩子useOcr,支持图片文件识别 添加不支持文件类型的错误提示国际化文案 --- src/renderer/src/hooks/useOcr.ts | 39 ++++++++++++++++++++++++ src/renderer/src/i18n/locales/zh-cn.json | 5 +++ 2 files changed, 44 insertions(+) create mode 100644 src/renderer/src/hooks/useOcr.ts diff --git a/src/renderer/src/hooks/useOcr.ts b/src/renderer/src/hooks/useOcr.ts new file mode 100644 index 0000000000..47d0e4abc2 --- /dev/null +++ b/src/renderer/src/hooks/useOcr.ts @@ -0,0 +1,39 @@ +import { useAppSelector } from '@renderer/store' +import { ImageFileMetadata, isImageFile } from '@renderer/types' +import { SupportedOcrFile } from '@renderer/types/ocr' +import { useTranslation } from 'react-i18next' + +export const useImageOcr = () => {} + +export const useOcr = () => { + const { t } = useTranslation() + const imageProvider = useAppSelector((state) => state.ocr.imageProvider) + + /** + * 对图片文件进行OCR识别 + * @param image 图片文件元数据 + * @returns OCR识别结果的Promise + */ + const ocrImage = async (image: ImageFileMetadata) => { + return window.api.ocr.ocr(image, imageProvider) + } + + /** + * 对支持的文件进行OCR识别 + * @param file 支持OCR的文件 + * @returns OCR识别结果的Promise + * @throws 当文件类型不支持时抛出错误 + */ + const ocr = async (file: SupportedOcrFile) => { + if (isImageFile(file)) { + return ocrImage(file) + } + // @ts-expect-error all types should be covered + throw new Error(t('ocr.file.not_supported', { type: file.type })) + } + + return { + ocrImage, + ocr + } +} diff --git a/src/renderer/src/i18n/locales/zh-cn.json b/src/renderer/src/i18n/locales/zh-cn.json index 8b55525a9c..1abf83fe76 100644 --- a/src/renderer/src/i18n/locales/zh-cn.json +++ b/src/renderer/src/i18n/locales/zh-cn.json @@ -1558,6 +1558,11 @@ }, "tip": "如果响应成功,则只针对超过30秒的消息进行提醒" }, + "ocr": { + "file": { + "not_supported": "不支持的文件类型 {{type}}" + } + }, "ollama": { "keep_alive_time": { "description": "对话后模型在内存中保持的时间(默认:5 分钟)",