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 分钟)",