feat(ai): 优化 AI 文档识别功能
- 新增搜索框和列表展示功能 - 实现拍照和相册选择功能 - 优化文字识别逻辑,支持 Base64 图片识别 -移除 MineView 中的 AI识别相关代码 - 重构 ImageHelper 中的 recognizeText 方法
这个提交包含在:
父节点
f60d218218
当前提交
791e3b74fc
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
正在加载...
在新工单中引用
屏蔽一个用户