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); } }