feat(app): 添加图片文字识别功能
- 在 ImageHelper 中添加 recognizeText 方法,用于提取图片中的文字
这个提交包含在:
父节点
e8f0a74636
当前提交
eae80e23ea
@ -1,4 +1,8 @@
|
||||
import { image } from '@kit.ImageKit';
|
||||
import { FileHelper } from './FileHelper';
|
||||
import fs from '@ohos.file.fs';
|
||||
import { BusinessError } from '@kit.BasicServicesKit';
|
||||
import { textRecognition } from '@kit.CoreVisionKit';
|
||||
|
||||
export class ImageHelper {
|
||||
private constructor() {
|
||||
@ -102,4 +106,37 @@ export class ImageHelper {
|
||||
}
|
||||
return compressedImageData;
|
||||
}
|
||||
|
||||
/**
|
||||
* 提取图片中文字
|
||||
* @param path
|
||||
* @returns
|
||||
*/
|
||||
static recognizeText(path: string): 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)
|
||||
.then((data: textRecognition.TextRecognitionResult) => {
|
||||
resolve(data)
|
||||
})
|
||||
.catch((error: BusinessError) => {
|
||||
reject(error)
|
||||
});
|
||||
}).catch((err: BusinessError) => {
|
||||
reject(err)
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
正在加载...
在新工单中引用
屏蔽一个用户