华为HarmonyOS帮助应用快速构建强大的扫码能力 -- 3 识别本地图片

基本概念

图片识码能力支持对图库中的码图进行扫描识别,并获取信息。

场景介绍

图片识码能力支持对图库中的条形码、二维码、MULTIFUNCTIONAL CODE进行识别,并获得码类型、码值、码位置信息。该能力可用于一图单码和一图多码的识别,比如条形码、付款码等。

业务流程

  1. 用户向开发者的应用发起图片识码请求。
  2. 应用通过调用Scan Kit的decode接口启动图片识码。
  3. Scan Kit通过回调返回图片识码结果。
  4. 应用向用户返回扫码结果。

接口说明

接口返回值有两种返回形式:Callback和Promise回调。下表中为启动图片识码Callback和Promise形式接口,Callback和Promise只是返回值方式不一样,功能相同。具体API说明详见接口文档

接口名

描述

decode(inputImage: InputImage, options?: scanBarcode.ScanOptions): Promise<Array<scanBarcode.ScanResult>>

启动图片识码,通过InputImage传入图片信息,通过ScanOptions进行识码参数设置(options为可选参数),使用Promise异步回调返回识码结果。

decode(inputImage: InputImage, options: scanBarcode.ScanOptions, callback: AsyncCallback<Array<scanBarcode.ScanResult>>): void

启动图片识码,通过InputImage传入图片信息,通过ScanOptions进行识码参数设置,使用Callback异步回调返回识码结果。

decode(inputImage: InputImage, callback: AsyncCallback<Array<scanBarcode.ScanResult>>): void

启动图片识码,通过InputImage传入图片信息,使用Callback异步回调返回识码结果。

开发步骤

图片识码接口支持识别图库中的条形码,二维码以及MULTIFUNCTIONAL CODE,并返回图片中码图的值,类型以及码的位置信息(码图最小外接矩形左上角和右下角的坐标)。

以下示例为调用图片识码的detectBarcode.decode接口获取码图信息。

  1. 导入图片识码接口和相关接口模块,该接口提供了图片识码参数和方法,导入方法如下。
     
      
    1. // 导入图片识码需要的日志和picker模块
    2. import { scanCore, scanBarcode, detectBarcode } from '@kit.ScanKit';
    3. import { photoAccessHelper } from '@kit.MediaLibraryKit';
    4. import { hilog } from '@kit.PerformanceAnalysisKit';
    5. import { BusinessError } from '@kit.BasicServicesKit';
  2. 调用detectBarcode.decode接口解析码图。
    • 通过Promise回调函数得到扫码结果,InputImage对象中uri参数推荐通过picker方式获取。
       
          
      1. @Entry
      2. @Component
      3. struct DetectPage {
      4. build() {
      5. Column() {
      6. Button('Promise with options')
      7. .backgroundColor('#0D9FFB')
      8. .fontSize(20)
      9. .fontColor('#FFFFFF')
      10. .fontWeight(FontWeight.Normal)
      11. .align(Alignment.Center)
      12. .type(ButtonType.Capsule)
      13. .width('90%')
      14. .height(40)
      15. .margin({ top: 5, bottom: 5 })
      16. .onClick(() => {
      17. // 定义识码参数options
      18. let options: scanBarcode.ScanOptions = {
      19. scanTypes: [scanCore.ScanType.ALL],
      20. enableMultiMode: true,
      21. }
      22. // 通过picker拉起图库的图片
      23. let photoOption = new photoAccessHelper.PhotoSelectOptions();
      24. photoOption.MIMEType = photoAccessHelper.PhotoViewMIMETypes.IMAGE_TYPE;
      25. photoOption.maxSelectNumber = 1;
      26. let photoPicker = new photoAccessHelper.PhotoViewPicker();
      27. photoPicker.select(photoOption).then((result) => {
      28. // 定义识码参数inputImage,其中uri为picker选择图片
      29. let inputImage: detectBarcode.InputImage = { uri: result.photoUris[0] };
      30. try {
      31. // 调用图片识码接口
      32. detectBarcode.decode(inputImage, options).then((result: Array<scanBarcode.ScanResult>) => {
      33. hilog.info(0x0001, '[Scan Sample]',
      34. `Succeeded in getting ScanResult by promise with options, result is ${JSON.stringify(result)}`);
      35. }).catch((error: BusinessError) => {
      36. hilog.error(0x0001, '[Scan Sample]',
      37. `Failed to get ScanResult by promise with options. Code: ${error.code}, message: ${error.message}`);
      38. });
      39. } catch (error) {
      40. hilog.error(0x0001, '[Scan Sample]',
      41. `Failed to detectBarcode. Code: ${error.code}, message: ${error.message}`);
      42. }
      43. })
      44. });
      45. }
      46. .width('100%')
      47. .height('100%')
      48. .alignItems(HorizontalAlign.Center)
      49. .justifyContent(FlexAlign.Center)
      50. }
      51. }
    • 通过Callback回调函数得到扫码结果,InputImage对象中uri参数推荐通过picker方式获取。
       
          
      1. @Entry
      2. @Component
      3. struct DetectPage {
      4. build() {
      5. Column() {
      6. Button('Callback with options')
      7. .backgroundColor('#0D9FFB')
      8. .fontSize(20)
      9. .fontColor('#FFFFFF')
      10. .fontWeight(FontWeight.Normal)
      11. .align(Alignment.Center)
      12. .type(ButtonType.Capsule)
      13. .width('90%')
      14. .height(40)
      15. .margin({ top: 5, bottom: 5 })
      16. .onClick(() => {
      17. // 定义识码参数options
      18. let options: scanBarcode.ScanOptions = {
      19. scanTypes: [scanCore.ScanType.ALL],
      20. enableMultiMode: true,
      21. enableAlbum: true
      22. }
      23. // 通过选择模式拉起photoPicker界面,用户可以选择一个图片
      24. let photoOption = new photoAccessHelper.PhotoSelectOptions();
      25. photoOption.MIMEType = photoAccessHelper.PhotoViewMIMETypes.IMAGE_TYPE;
      26. photoOption.maxSelectNumber = 1;
      27. let photoPicker = new photoAccessHelper.PhotoViewPicker();
      28. photoPicker.select(photoOption).then((result) => {
      29. // 定义识码参数inputImage,其中uri为picker选择图片
      30. let inputImage: detectBarcode.InputImage = { uri: result.photoUris[0] };
      31. try {
      32. // 调用图片识码接口
      33. detectBarcode.decode(inputImage, options,
      34. (error: BusinessError, result: Array<scanBarcode.ScanResult>) => {
      35. if (error && error.code) {
      36. hilog.error(0x0001, '[Scan Sample]',
      37. `Failed to get ScanResult by callback with options. Code: ${error.code}, message: ${error.message}`);
      38. return;
      39. }
      40. hilog.info(0x0001, '[Scan Sample]',
      41. `Succeeded in getting ScanResult by callback with options, result is ${JSON.stringify(result)}`);
      42. });
      43. } catch (error) {
      44. hilog.error(0x0001, '[Scan Sample]',
      45. `Failed to detectBarcode. Code: ${error.code}, message: ${error.message}`);
      46. }
      47. })
      48. });
      49. }
      50. .width('100%')
      51. .height('100%')
      52. .alignItems(HorizontalAlign.Center)
      53. .justifyContent(FlexAlign.Center)
      54. }
      55. }

模拟器开发

支持模拟器开发,使用指导请参见使用模拟器运行应用/服务

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

青瓷代码世界

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值