学习笔记(三十八):识别本地图片

基本概念

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

场景介绍

图片识码能力支持对图库中的条形码、二维码、MULTIFUNCTIONAL CODE进行识别,并获得码类型、码值、码位置信息

该能力可用于一图单码和一图多码的识别,比如条形码、付款码等

使用示例

1、导入图片识码接口和相关接口模块

import { scanCore, scanBarcode, detectBarcode } from '@kit.ScanKit';
import { photoAccessHelper } from '@kit.MediaLibraryKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';

2、调用detectBarcode.decode接口解析码图。

  • 通过Promise回调函数得到扫码结果,InputImage对象中uri参数推荐通过picker方式获取
// 导入图片识码需要的日志和picker模块
import { scanCore, scanBarcode, detectBarcode } from '@kit.ScanKit';
import { photoAccessHelper } from '@kit.MediaLibraryKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
@Entry
@Component
struct MainPage {
  pageInfo: NavPathStack = new NavPathStack()
  // 选择并识别本地图片内容
  chooseLocalPicAndScan(){
    // 定义识码参数options
    let options: scanBarcode.ScanOptions = {
      scanTypes: [scanCore.ScanType.ALL], // 识别的barcode类型
      enableMultiMode: true,
    }
    // 通过picker拉起图库的图片
    let photoOption = new photoAccessHelper.PhotoSelectOptions();
    photoOption.MIMEType = photoAccessHelper.PhotoViewMIMETypes.IMAGE_TYPE;
    photoOption.maxSelectNumber = 1; // 最大选择图片数量
    let photoPicker = new photoAccessHelper.PhotoViewPicker();
    photoPicker.select(photoOption).then((result) => {
      // 定义识码参数inputImage,其中uri为picker选择图片
      let inputImage: detectBarcode.InputImage = { uri: result.photoUris[0] };
      try {
        // 调用图片识码接口
        detectBarcode.decode(inputImage, options).then((result: Array<scanBarcode.ScanResult>) => {
          hilog.info(0x0001, '[Scan Sample]',
            `Succeeded in getting ScanResult by promise with options, result is ${JSON.stringify(result)}`);
        }).catch((error: BusinessError) => {
          hilog.error(0x0001, '[Scan Sample]',
            `Failed to get ScanResult by promise with options. Code: ${error.code}, message: ${error.message}`);
        });
      } catch (error) {
        hilog.error(0x0001, '[Scan Sample]',
          `Failed to detectBarcode. Code: ${error.code}, message: ${error.message}`);
      }
    })
  }
  build() {
    Navigation(this.pageInfo) {
      Column() {
        Button('识别本地图片')
          .onClick(()=>{
              this.chooseLocalPicAndScan()
          })
          .margin(10)
      }
      .width('100%')
    }
    .height('100%')
  }
}
  • 通过Callback回调函数得到扫码结果,InputImage对象中uri参数推荐通过picker方式获取
// 启动页
// 导入图片识码需要的日志和picker模块
import { scanCore, scanBarcode, detectBarcode } from '@kit.ScanKit';
import { photoAccessHelper } from '@kit.MediaLibraryKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';

@Entry
@Component
struct MainPage {
  pageInfo: NavPathStack = new NavPathStack()

  // 选择并识别本地图片内容
  chooseLocalPicAndScan() {
    // 定义识码参数options
    let options: scanBarcode.ScanOptions = {
      scanTypes: [scanCore.ScanType.ALL],
      enableMultiMode: true,
      enableAlbum: true
    }
    // 通过选择模式拉起photoPicker界面,用户可以选择一个图片
    let photoOption = new photoAccessHelper.PhotoSelectOptions();
    photoOption.MIMEType = photoAccessHelper.PhotoViewMIMETypes.IMAGE_TYPE;
    photoOption.maxSelectNumber = 1;
    let photoPicker = new photoAccessHelper.PhotoViewPicker();
    photoPicker.select(photoOption).then((result) => {
      // 定义识码参数inputImage,其中uri为picker选择图片
      let inputImage: detectBarcode.InputImage = { uri: result.photoUris[0] };
      try {
        // 调用图片识码接口
        detectBarcode.decode(inputImage, options,
          (error: BusinessError, result: Array<scanBarcode.ScanResult>) => {
            if (error && error.code) {
              hilog.error(0x0001, '[Scan Sample]',
                `Failed to get ScanResult by callback with options. Code: ${error.code}, message: ${error.message}`);
              return;
            }
            hilog.info(0x0001, '[Scan Sample]',
              `Succeeded in getting ScanResult by callback with options, result is ${JSON.stringify(result)}`);
          });
      } catch (error) {
        hilog.error(0x0001, '[Scan Sample]',
          `Failed to detectBarcode. Code: ${error.code}, message: ${error.message}`);
      }
    })
  }


  build() {
    Navigation(this.pageInfo) {
      Column() {
        Text('首页')
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
        Button('跳转第一页')
          .onClick(() => {
            this.pageInfo.pushPathByName("FirstPage", null)
          })
          .margin({ top: 100 })
        Button('跳转第二页')
          .onClick(() => {
            this.pageInfo.pushPathByName("SecondPage", null)
          })
          .margin(10)
        Button('识别本地图片')
          .onClick(() => {
            this.chooseLocalPicAndScan()
          })
          .margin(10)
      }
      .width('100%')
    }
    .height('100%')
  }
}

相关文档:

识别本地图片

 
 
posted @ 2024-11-18 23:31  听着music睡  阅读(14)  评论(0编辑  收藏  举报