#鸿蒙通关秘籍#如何在HarmonyOS NEXT中实现基于Core Vision Kit的OCR功能?

HarmonyOS
20h前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
CodeCrusader

在HarmonyOS NEXT中,实现OCR功能需要以下步骤:

  1. 使用Core Vision Kit中的textRecognition模块进行文字识别。

    import { textRecognition } from '@kit.CoreVisionKit';
    
    export class ImageOCRUtil {
      async recognizeText(image: PixelMap | undefined, resultCallback: Function) {
        if (!image) {
          console.error('OCR', 'the image is not existed');
          return;
        }
    
        let visionInfo = {
          pixelMap: image
        };
    
        let textConfiguration = {
          isDirectionDetectionSupported: false
        };
    
        textRecognition.recognizeText(visionInfo, textConfiguration, (error, data) => {
          if (error.code == 0) {
            let recognitionRes = data.toString();
            resultCallback(recognitionRes);
          }
        });
      }
    }
    
    export default new ImageOCRUtil();
    
  2. 从相册中选择图片,使用Core File Kitpicker模块获取图片URI,然后进行图片识别。

    import { picker } from '@kit.CoreFileKit';
    
    async openAlbum(): Promise<string> {
      return new Promise((resolve, reject) => {
        let photoPicker = new picker.PhotoViewPicker;
        photoPicker.select({
          MIMEType: picker.PhotoViewMIMETypes.IMAGE_TYPE,
          maxSelectNumber: 1
        }).then((res) => {
          resolve(res.photoUris[0]);
        }).catch((err) => {
          console.error('OCR', `Failed to get photo uri, code: ${err.code}, message: ${err.message}`);
          resolve('');
        })
      });
    }
    
分享
微博
QQ
微信
回复
19h前
相关问题