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

HarmonyOS
2024-11-28 15:20:08
浏览
收藏 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();
    
    • 1.
    • 2.
    • 3.
    • 4.
    • 5.
    • 6.
    • 7.
    • 8.
    • 9.
    • 10.
    • 11.
    • 12.
    • 13.
    • 14.
    • 15.
    • 16.
    • 17.
    • 18.
    • 19.
    • 20.
    • 21.
    • 22.
    • 23.
    • 24.
    • 25.
    • 26.
    • 27.
  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('');
        })
      });
    }
    
    • 1.
    • 2.
    • 3.
    • 4.
    • 5.
    • 6.
    • 7.
    • 8.
    • 9.
    • 10.
    • 11.
    • 12.
    • 13.
    • 14.
    • 15.
    • 16.
分享
微博
QQ
微信
回复
2024-11-28 16:20:03
相关问题