鸿蒙AI功能开发【hiai引擎框架-通用文字识别】 底子视觉服务 ...

打印 上一主题 下一主题

主题 1865|帖子 1865|积分 5605

先容

本示例展示了使用hiai引擎框架提供的通用文字识别能力。
本示例模仿了在应用里,选择一张图片,识别其文字信息并展示出来(可复制)。
必要使用hiai引擎框架通用文字识别接口@kit.CoreVisionKit.d.ts。
效果预览


使用阐明:

  • 在手机的主屏幕,点击”ocrDemo“,启动应用。
  • 点击“选择图片”按钮,在图库中选择图片,大概通过相机照相。
  • 点击“开始识别”按钮,识别图本信息,文本展示并可复制。
详细实现

本示例展示的控件在@kit.CoreVisionKit.d.ts定义了文字识别API:
  1. recognizeText(visionInfo: VisionInfo, callback: AsyncCallback): void
复制代码
业务使用时,必要先辈行import导入textRecognition。 调用通用文字识别接口,并传入想要识别的图片,接收处理惩罚返回的结果(文字信息)。参考:
  1. import { textRecognition } from '@kit.CoreVisionKit'
  2. import { image } from '@kit.ImageKit';
  3. import { hilog } from '@kit.PerformanceAnalysisKit';
  4. import { BusinessError } from '@kit.BasicServicesKit';
  5. import { picker, fileIo } from '@kit.CoreFileKit';
  6. @Entry
  7. @Component
  8. struct Index {
  9.   private imageSource: image.ImageSource | undefined = undefined;
  10.   @State chooseImage: PixelMap | undefined = undefined;
  11.   @State dataValues: string = '';
  12.   build() {
  13.     Column() {
  14.       Image(this.chooseImage)
  15.         .objectFit(ImageFit.Fill)
  16.         .height('60%')
  17.       Text(this.dataValues)
  18.         .copyOption(CopyOptions.LocalDevice)
  19.         .height('15%')
  20.         .margin(10)
  21.         .width('60%')
  22.       Button('选择图片')
  23.         .type(ButtonType.Capsule)
  24.         .fontColor(Color.White)
  25.         .alignSelf(ItemAlign.Center)
  26.         .width('80%')
  27.         .margin(10)
  28.         .onClick(() => {
  29.           // 拉起图库,获取图片资源
  30.           this.selectImage();
  31.         })
  32.       Button('开始识别')
  33.         .type(ButtonType.Capsule)
  34.         .fontColor(Color.White)
  35.         .alignSelf(ItemAlign.Center)
  36.         .width('80%')
  37.         .margin(10)
  38.         .onClick(async () => {
  39.           this.textRecognitionTest();
  40.         })
  41.     }
  42.     .width('100%')
  43.     .height('100%')
  44.     .justifyContent(FlexAlign.Center)
  45.   }
  46.   private async textRecognitionTest() {
  47.     if (!this.chooseImage) {
  48.       return;
  49.     }
  50.     // 调用文本识别接口
  51.     let visionInfo: textRecognition.VisionInfo = {
  52.       pixelMap: this.chooseImage
  53.     };
  54.     let textConfiguration: textRecognition.TextRecognitionConfiguration = {
  55.       isDirectionDetectionSupported: false
  56.     };
  57.     textRecognition.recognizeText(visionInfo, textConfiguration, (error: BusinessError, data: textRecognition.TextRecognitionResult) => {
  58.       // 识别成功,获取对应的结果
  59.       if (error.code == 0) {
  60.         let recognitionString = data.toString();
  61.         hilog.info(0x0000, 'OCRDemo', `Succeeded in recognizing text:${recognitionString}`);
  62.         // 将结果更新到Text中显示
  63.         this.dataValues = data.value;
  64.       }
  65.       if(this.chooseImage && this.imageSource) {
  66.         this.chooseImage.release();
  67.         this.imageSource.release();
  68.       }
  69.     });
  70.   }
  71.   private async selectImage() {
  72.     let uri = await this.openPhoto();
  73.     if (uri === undefined) {
  74.       hilog.error(0x0000, 'OCRDemo', "Failed to get uri.");
  75.       return;
  76.     }
  77.     this.loadImage(uri);
  78.   }
  79.   private openPhoto(): Promise<string> {
  80.     return new Promise<string>((resolve, reject) => {
  81.       let photoPicker = new picker.PhotoViewPicker();
  82.       photoPicker.select({
  83.         MIMEType: picker.PhotoViewMIMETypes.IMAGE_TYPE,
  84.         maxSelectNumber: 1
  85.       }).then((res: picker.PhotoSelectResult) => {
  86.         resolve(res.photoUris[0]);
  87.       }).catch((err: BusinessError) => {
  88.         hilog.error(0x0000, 'OCRDemo', `Failed to get photo image uri. code:${err.code},message:${err.message}`);
  89.         resolve('');
  90.       })
  91.     })
  92.   }
  93.   private loadImage(name: string) {
  94.     setTimeout(async () => {
  95.       let fileSource = await fileIo.open(name, fileIo.OpenMode.READ_ONLY);
  96.       this.imageSource = image.createImageSource(fileSource.fd);
  97.       this.chooseImage = await this.imageSource.createPixelMap();
  98.     }, 100)
  99.   }
  100. }
复制代码
以上就是本篇文章所带来的鸿蒙开发中一小部门技术讲解;想要学习完整的鸿蒙全栈技术。可以在末端找我可全部拿到!
下面是鸿蒙的完整学习路线,展示如下:

除此之外,根据这个学习鸿蒙全栈学习路线,也附带一整套完整的学习【文档+视频】,内容包含如下
   内容包含了:(ArkTS、ArkUI、Stage模型、多端部署、分布式应用开发、音频、视频、WebGL、OpenHarmony多媒体技术、Napi组件、OpenHarmony内核、鸿蒙南向开发、鸿蒙项目实战)等技术知识点。帮助大家在学习鸿蒙路上快速成长!
  鸿蒙【北向应用开发+南向体系层开发】文档

鸿蒙【底子+实战项目】视频

鸿蒙面经


为了制止大家在学习过程中产生更多的时间本钱,对比我把以上内容全部放在了↓↓↓想要的可以自拿喔!谢谢大家观看!

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

篮之新喜

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表