|
@@ -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<textRecognition.TextRecognitionResult> {
|
|
|
+ static recognizeText(path: string | image.PixelMap): Promise<textRecognition.TextRecognitionResult> {
|
|
|
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<textRecognition.TextRecognitionResult> {
|
|
|
+ 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<image.PixelMap> {
|
|
|
+ // 移除 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);
|
|
|
}
|
|
|
}
|