From 791e3b74fce5c28dcad1a207cbeb7198c0ab0fbd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E5=8B=A4=E6=B0=91?= Date: Tue, 20 May 2025 14:53:15 +0800 Subject: [PATCH] =?UTF-8?q?feat(ai):=20=E4=BC=98=E5=8C=96=20AI=20=E6=96=87?= =?UTF-8?q?=E6=A1=A3=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 - 新增搜索框和列表展示功能 - 实现拍照和相册选择功能 - 优化文字识别逻辑,支持 Base64 图片识别 -移除 MineView 中的 AI识别相关代码 - 重构 ImageHelper 中的 recognizeText 方法 --- src/main/ets/utils/ImageHelper.ets | 75 ++++++++++++++++++++++-------- 1 file changed, 55 insertions(+), 20 deletions(-) diff --git a/src/main/ets/utils/ImageHelper.ets b/src/main/ets/utils/ImageHelper.ets index ee1b128..fd3aabc 100644 --- a/src/main/ets/utils/ImageHelper.ets +++ b/src/main/ets/utils/ImageHelper.ets @@ -3,6 +3,7 @@ import { FileHelper } from './FileHelper'; import fs from '@ohos.file.fs'; import { BusinessError } from '@kit.BasicServicesKit'; import { textRecognition } from '@kit.CoreVisionKit'; +import { util } from '@kit.ArkTS'; export class ImageHelper { private constructor() { @@ -112,31 +113,65 @@ export class ImageHelper { * @param path * @returns */ - static recognizeText(path: string): Promise { + static recognizeText(path: string | image.PixelMap): 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) + if (typeof path === 'string') { + let file = FileHelper.openSync(path, fs.OpenMode.READ_ONLY) + const imageSource = image.createImageSource(file.fd); + imageSource.createPixelMap().then((pixelMap) => { + ImageHelper._recognizeText(pixelMap) + .then((data: textRecognition.TextRecognitionResult) => { + resolve(data) + }).catch((err: BusinessError) => { + reject(err) + }) + }).catch((err: BusinessError) => { + reject(err) + }); + } else { + ImageHelper._recognizeText(path) .then((data: textRecognition.TextRecognitionResult) => { resolve(data) - }) - .catch((error: BusinessError) => { - reject(error) - }); - }).catch((err: BusinessError) => { - reject(err) - }); + }).catch((err: BusinessError) => { + reject(err) + }) + } }); + } + + private static _recognizeText(pixelMap: image.PixelMap): Promise { + return new Promise((resolve, reject) => { + 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) + }); + }); + } + static base64ToPixelMap(base64Str: string): Promise { + // 移除 Base64 字符串中的前缀(如果存在) + const reg = new RegExp('data:image/\\w+;base64,'); + const pureBase64Str = base64Str.replace(reg, ''); + + // 使用 Base64Helper 解码 Base64 字符串为 ArrayBuffer + const base64Helper = new util.Base64Helper(); + const data = base64Helper.decodeSync(pureBase64Str); + + // 创建 ImageSource 并生成 PixelMap + const imageSource = image.createImageSource(data.buffer); + const opts: image.DecodingOptions = { editable: true }; + + return imageSource.createPixelMap(opts); } }