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

介绍

本示例展示了使用hiai引擎框架提供的通用文字识别能力。

本示例模拟了在应用里,选择一张图片,识别其文字信息并展示出来(可复制)。

需要使用hiai引擎框架通用文字识别接口@kit.CoreVisionKit.d.ts。

效果预览

使用说明:

  1. 在手机的主屏幕,点击"ocrDemo",启动应用。
  2. 点击"选择图片"按钮,在图库中选择图片,或者通过相机拍照。
  3. 点击"开始识别"按钮,识别图本信息,文本展示并可复制。

具体实现

本示例展示的控件在@kit.CoreVisionKit.d.ts定义了文字识别API:

复制代码
recognizeText(visionInfo: VisionInfo, callback: AsyncCallback): void

业务使用时,需要先进行import导入textRecognition。 调用通用文字识别接口,并传入想要识别的图片,接收处理返回的结果(文字信息)。参考:

复制代码
import { textRecognition } from '@kit.CoreVisionKit'
import { image } from '@kit.ImageKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { picker, fileIo } from '@kit.CoreFileKit';

@Entry
@Component
struct Index {
  private imageSource: image.ImageSource | undefined = undefined;
  @State chooseImage: PixelMap | undefined = undefined;
  @State dataValues: string = '';

  build() {
    Column() {
      Image(this.chooseImage)
        .objectFit(ImageFit.Fill)
        .height('60%')

      Text(this.dataValues)
        .copyOption(CopyOptions.LocalDevice)
        .height('15%')
        .margin(10)
        .width('60%')

      Button('选择图片')
        .type(ButtonType.Capsule)
        .fontColor(Color.White)
        .alignSelf(ItemAlign.Center)
        .width('80%')
        .margin(10)
        .onClick(() => {
          // 拉起图库,获取图片资源
          this.selectImage();
        })

      Button('开始识别')
        .type(ButtonType.Capsule)
        .fontColor(Color.White)
        .alignSelf(ItemAlign.Center)
        .width('80%')
        .margin(10)
        .onClick(async () => {
          this.textRecognitionTest();
        })
    }
    .width('100%')
    .height('100%')
    .justifyContent(FlexAlign.Center)
  }

  private async textRecognitionTest() {
    if (!this.chooseImage) {
      return;
    }
    // 调用文本识别接口
    let visionInfo: textRecognition.VisionInfo = {
      pixelMap: this.chooseImage
    };
    let textConfiguration: textRecognition.TextRecognitionConfiguration = {
      isDirectionDetectionSupported: false
    };
    textRecognition.recognizeText(visionInfo, textConfiguration, (error: BusinessError, data: textRecognition.TextRecognitionResult) => {
      // 识别成功,获取对应的结果
      if (error.code == 0) {
        let recognitionString = data.toString();
        hilog.info(0x0000, 'OCRDemo', `Succeeded in recognizing text:${recognitionString}`);
        // 将结果更新到Text中显示
        this.dataValues = data.value;
      }
      if(this.chooseImage && this.imageSource) {
        this.chooseImage.release();
        this.imageSource.release();
      }
    });
  }

  private async selectImage() {
    let uri = await this.openPhoto();
    if (uri === undefined) {
      hilog.error(0x0000, 'OCRDemo', "Failed to get uri.");
      return;
    }
    this.loadImage(uri);
  }

  private openPhoto(): Promise<string> {
    return new Promise<string>((resolve, reject) => {
      let photoPicker = new picker.PhotoViewPicker();
      photoPicker.select({
        MIMEType: picker.PhotoViewMIMETypes.IMAGE_TYPE,
        maxSelectNumber: 1
      }).then((res: picker.PhotoSelectResult) => {
        resolve(res.photoUris[0]);
      }).catch((err: BusinessError) => {
        hilog.error(0x0000, 'OCRDemo', `Failed to get photo image uri. code:${err.code},message:${err.message}`);
        resolve('');
      })
    })
  }

  private loadImage(name: string) {
    setTimeout(async () => {
      let fileSource = await fileIo.open(name, fileIo.OpenMode.READ_ONLY);
      this.imageSource = image.createImageSource(fileSource.fd);
      this.chooseImage = await this.imageSource.createPixelMap();
    }, 100)
  }
}

以上就是本篇文章所带来的鸿蒙开发中一小部分技术讲解;想要学习完整的鸿蒙全栈技术。可以在结尾找我可全部拿到!

下面是鸿蒙的完整学习路线 ,展示如下:

除此之外,根据这个学习鸿蒙全栈学习路线,也附带一整套完整的学习【文档+视频】,内容包含如下

内容包含了:(ArkTS、ArkUI、Stage模型、多端部署、分布式应用开发、音频、视频、WebGL、OpenHarmony多媒体技术、Napi组件、OpenHarmony内核、鸿蒙南向开发、鸿蒙项目实战)等技术知识点。帮助大家在学习鸿蒙路上快速成长!

鸿蒙【北向应用开发+南向系统层开发】文档

鸿蒙【基础+实战项目】视频

鸿蒙面经

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

相关推荐
盐焗西兰花2 分钟前
鸿蒙学习实战之路-数据持久化键值型数据库KV-Store全攻略
数据库·学习·harmonyos
CodeLinghu2 小时前
路由:Agent能够根据条件动态决定工作流的下一步
人工智能·microsoft·ai·llm
磊少工作室_CTO2 小时前
鸿蒙Next —— 状态管理实践
harmonyos·mvvm·客户端
视觉&物联智能2 小时前
【杂谈】-人工智能:助力护士回归人文关怀,而非取而代之
人工智能·深度学习·ai·aigc·agi
undsky_3 小时前
n8n 重构镜像,开启无限可能
人工智能·ai·aigc·ai编程
paopao_wu3 小时前
声音克隆与情感合成:Dify接入IndexTTS2
人工智能·ai·tts
万俟淋曦3 小时前
【论文速递】2025年第37周(Sep-07-13)(Robotics/Embodied AI/LLM)
人工智能·ai·机器人·大模型·论文·robotics·具身智能
阿杰学AI3 小时前
AI核心知识61——大语言模型之Embedding (简洁且通俗易懂版)
人工智能·机器学习·ai·语言模型·自然语言处理·embedding·词向量
御承扬3 小时前
鸿蒙原生系列之动画效果(转场动画)
华为·harmonyos·转场动画
yenggd4 小时前
企业总部-分支-门点-数据中心使用骨干网SRv6 BE互联互通整体架构配置案例
运维·网络·计算机网络·华为·架构