Преглед изворни кода

feat(ai): 优化 AI 文档识别功能

- 新增搜索框和列表展示功能
- 实现拍照和相册选择功能
- 优化文字识别逻辑,支持 Base64 图片识别
-移除 MineView 中的 AI识别相关代码
- 重构 ImageHelper 中的 recognizeText 方法
徐勤民 пре 3 недеља
родитељ
комит
791e3b74fc
1 измењених фајлова са 55 додато и 20 уклоњено
  1. 55 20
      src/main/ets/utils/ImageHelper.ets

+ 55 - 20
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<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);
   }
 }