From eae80e23ea1b739b2fbfe8890801c3d6bfae3f92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E5=8B=A4=E6=B0=91?= Date: Wed, 14 May 2025 18:30:59 +0800 Subject: [PATCH] =?UTF-8?q?feat(app):=20=E6=B7=BB=E5=8A=A0=E5=9B=BE?= =?UTF-8?q?=E7=89=87=E6=96=87=E5=AD=97=E8=AF=86=E5=88=AB=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在 ImageHelper 中添加 recognizeText 方法,用于提取图片中的文字 --- src/main/ets/utils/ImageHelper.ets | 37 ++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/main/ets/utils/ImageHelper.ets b/src/main/ets/utils/ImageHelper.ets index 170080d..ee1b128 100644 --- a/src/main/ets/utils/ImageHelper.ets +++ b/src/main/ets/utils/ImageHelper.ets @@ -1,4 +1,8 @@ import { image } from '@kit.ImageKit'; +import { FileHelper } from './FileHelper'; +import fs from '@ohos.file.fs'; +import { BusinessError } from '@kit.BasicServicesKit'; +import { textRecognition } from '@kit.CoreVisionKit'; export class ImageHelper { private constructor() { @@ -102,4 +106,37 @@ export class ImageHelper { } return compressedImageData; } + + /** + * 提取图片中文字 + * @param path + * @returns + */ + static recognizeText(path: string): Promise { + return new Promise((resolve, reject) => { + let file = FileHelper.openSync(path, fs.OpenMode.READ_ONLY) + const imageSource = image.createImageSource(file.fd); + + imageSource.createPixelMap().then((pixelMap) => { + let visionInfo: textRecognition.VisionInfo = { + pixelMap: pixelMap + }; + let textConfiguration: textRecognition.TextRecognitionConfiguration = { + isDirectionDetectionSupported: false + }; + + textRecognition.recognizeText(visionInfo, textConfiguration) + .then((data: textRecognition.TextRecognitionResult) => { + resolve(data) + }) + .catch((error: BusinessError) => { + reject(error) + }); + }).catch((err: BusinessError) => { + reject(err) + }); + }); + + + } }