【HarmonyOS NEXT 】鸿蒙detectBarcode (图像识码)

本模块提供本地图片识码和图像数据识码能力,支持对图像中的条形码、二维码、多功能码进行识别,并获得码类型、码值、码位置信息。

**起始版本:**4.1.0(11)

导入模块

复制代码
import { detectBarcode } from '@kit.ScanKit';

InputImage

待识别的图片信息。

**系统能力:**SystemCapability.Multimedia.Scan.ScanBarcode

**起始版本:**4.1.0(11)

名称 类型 只读 可选 说明
uri string 否 否 图片路径,例如file://media/Photo/x/xxx.jpg。

说明

推荐使用picker获取图片路径。

复制代码
javascript 复制代码
import { photoAccessHelper } from '@kit.MediaLibraryKit';
import { hilog } from '@kit.PerformanceAnalysisKit';

let PhotoSelectOptions = new photoAccessHelper.PhotoSelectOptions();
PhotoSelectOptions.MIMEType = photoAccessHelper.PhotoViewMIMETypes.IMAGE_TYPE;
PhotoSelectOptions.maxSelectNumber = 1;
PhotoSelectOptions.isPhotoTakingSupported = false;
PhotoSelectOptions.isEditSupported = false;
let photoPicker = new photoAccessHelper.PhotoViewPicker();
photoPicker.select(PhotoSelectOptions).then((result: photoAccessHelper.PhotoSelectResult) => {
  if (!result || (result.photoUris && result.photoUris.length === 0)) {
    hilog.error(0x0001, 'picker', 'Failed to get PhotoSelectResult by promise.');
    return;
  }
  hilog.info(0x0001, 'picker', `Succeeded in getting PhotoSelectResult by promise, result is ${JSON.stringify(result)}`); 
})

detectBarcode.decode

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

通过配置参数调用图片识码,使用Promise异步回调返回识码结果。

**系统能力:**SystemCapability.Multimedia.Scan.ScanBarcode

**起始版本:**4.1.0(11)

参数:

参数名 类型 必填 说明
inputImage InputImage 是 待识别的图片信息。
options scanBarcode.ScanOptions 否 启动图片识码参数。

返回值:

类型 说明
Promise<Array<scanBarcode.ScanResult>> Promise对象,返回识码结果对象。

错误码:

以下错误码的详细介绍请参见ArkTS API错误码。

错误码ID 错误信息
401 Parameter error.
1000500001 Internal error.

示例:

复制代码
javascript 复制代码
import { scanCore, scanBarcode, detectBarcode } from '@kit.ScanKit';
import { picker } from '@kit.CoreFileKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';

// 定义识码参数options
let options: scanBarcode.ScanOptions = { scanTypes: [scanCore.ScanType.ALL], enableMultiMode: true, enableAlbum: true }
// 通过picker拉起图库并选择图片
let photoOption = new picker.PhotoSelectOptions();
photoOption.MIMEType = picker.PhotoViewMIMETypes.IMAGE_TYPE;
photoOption.maxSelectNumber = 1;
let photoPicker = new picker.PhotoViewPicker();
photoPicker.select(photoOption).then((result) => {
  // 定义识码参数inputImage,其中uri为picker选择图片
  let inputImage: detectBarcode.InputImage = { uri: result.photoUris[0] }
  // 调用图片识码接口
  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}`);
  });
})

detectBarcode.decode

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

通过配置参数调用图片识码,使用Callback异步回调返回识码结果。

**系统能力:**SystemCapability.Multimedia.Scan.ScanBarcode

**起始版本:**4.1.0(11)

参数:

参数名 类型 必填 说明
inputImage InputImage 是 待识别的图片信息。
options scanBarcode.ScanOptions 是 启动图片识码参数。
callback AsyncCallback<Array<scanBarcode.ScanResult>> 是 回调函数,当图片识码成功,err为undefined,data为获取到的识码结果Array<scanBarcode.ScanResult>,否则为错误对象。

错误码:

以下错误码的详细介绍请参见ArkTS API错误码。

错误码ID 错误信息
401 Parameter error.
1000500001 Internal error.

示例:

复制代码
javascript 复制代码
import { picker } from '@kit.CoreFileKit';
import { scanCore, scanBarcode, detectBarcode } from '@kit.ScanKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';

// 定义识码参数options
let options: scanBarcode.ScanOptions = { scanTypes: [scanCore.ScanType.ALL], enableMultiMode: true, enableAlbum: true }
// 通过选择模式拉起photoPicker界面,用户可以选择一个图片
let photoOption = new picker.PhotoSelectOptions();
photoOption.MIMEType = picker.PhotoViewMIMETypes.IMAGE_TYPE;
photoOption.maxSelectNumber = 1;
let photoPicker = new picker.PhotoViewPicker();
photoPicker.select(photoOption).then((result) => {
  // 定义识码参数inputImage,其中uri为picker选择图片
  let inputImage: detectBarcode.InputImage = { uri: result.photoUris[0] }
  // 调用图片识码接口
  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)}`);
  });
})

detectBarcode.decode

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

图片识码,使用Callback异步回调返回识码结果。

**系统能力:**SystemCapability.Multimedia.Scan.ScanBarcode

**起始版本:**4.1.0(11)

参数:

参数名 类型 必填 说明
inputImage InputImage 是 待识别的图片信息。
callback AsyncCallback<Array<scanBarcode.ScanResult>> 是 回调函数,当图片识码成功,err为undefined,data为获取到的识码结果Array<scanBarcode.ScanResult>,否则为错误对象。

错误码:

以下错误码的详细介绍请参见ArkTS API错误码。

错误码ID 错误信息
401 Parameter error.
1000500001 Internal error.

示例:

javascript 复制代码
import { scanBarcode, detectBarcode } from '@kit.ScanKit';
import { picker } from '@kit.CoreFileKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';

// 通过picker拉起图库并选择图片
let photoOption = new picker.PhotoSelectOptions();
photoOption.MIMEType = picker.PhotoViewMIMETypes.IMAGE_TYPE;
photoOption.maxSelectNumber = 1;
let photoPicker = new picker.PhotoViewPicker();
photoPicker.select(photoOption).then((result) => {
  // 定义识码参数inputImage,其中uri为picker选择图片
  let inputImage: detectBarcode.InputImage = { uri: result.photoUris[0] }
  // 调用图片识码接口
  detectBarcode.decode(inputImage, (error: BusinessError, result: Array<scanBarcode.ScanResult>) => {
    if (error && error.code) {
      hilog.error(0x0001, '[Scan Sample]', `Failed to get ScanResult by callback. Code: ${error.code}, message: ${error.message}`);
      return;
    }
    hilog.info(0x0001, '[Scan Sample]', `Succeeded in getting ScanResult by callback, result is ${JSON.stringify(result)}`);
  });
})

ByteImage

待识别的图像数据。

**系统能力:**SystemCapability.Multimedia.Scan.ScanBarcode

**起始版本:**5.0.0(12)

名称 类型 只读 可选 说明
byteBuffer ArrayBuffer 否 否 图像数据。
width number 否 否 图像宽度,单位:px。
height number 否 否 图像高度,单位:px。
format ImageFormat 否 否 图像数据类型。

示例:

javascript 复制代码
import { detectBarcode } from '@kit.ScanKit';

// YUV图像的buffer, height, width数据,可通过相机预览流数据获取,比如获取宽高都是1080时
let byteImg: detectBarcode.ByteImage = {
  byteBuffer: buffer,
  width: 1080,
  height: 1080,
  format: detectBarcode.ImageFormat.NV21
};

ImageFormat

枚举,图像数据类型。

**系统能力:**SystemCapability.Multimedia.Scan.Barcode

**起始版本:**5.0.0(12)

名称 值 说明
NV21 0 图像格式为NV21。

DetectResult

识别结果。

**系统能力:**SystemCapability.Multimedia.Scan.ScanBarcode

**起始版本:**5.0.0(12)

名称 类型 只读 可选 说明
scanResults Array<scanBarcode.ScanResult> 否 否 扫码结果。
zoomValue number 否 否 相机可变焦距比,通过setZoomRatio控制相机实现变焦功能。 说明 1、使用Camera Kit getZoomRatio接口获取相机当前变焦比zoomRatio。 2、使用Camera Kit setZoomRatio接口设置targetRatio,目标值为zoomRatio * zoomValue。

detectBarcode.decodeImage

decodeImage(image: ByteImage, options?: scanBarcode.ScanOptions): Promise<DetectResult>

通过配置参数调用图像数据识码能力,使用Promise异步回调返回识码结果。

**系统能力:**SystemCapability.Multimedia.Scan.ScanBarcode

**起始版本:**5.0.0(12)

参数:

参数名 类型 只读 必填 说明
image ByteImage 否 是 待识别的图像数据。
options scanBarcode.ScanOptions 否 否 启动图像数据识码参数。

返回值:

类型 说明
Promise<DetectResult> Promise对象,返回图像数据识码结果对象。

错误码:

以下错误码的详细介绍请参见ArkTS API错误码。

错误码ID 错误信息
401 Parameter error.
1000500001 Internal error.

示例:

javascript 复制代码
import { scanCore, scanBarcode, detectBarcode } from '@kit.ScanKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';

// 优先获取图像的YuvByteBuffer, YuvHeight, YuvWidth数据,比如获取宽高都是1080时
let byteImg: detectBarcode.ByteImage = {
  byteBuffer: YuvByteBuffer,
  width: 1080,
  height: 1080,
  format: detectBarcode.ImageFormat.NV21
};
let options: scanBarcode.ScanOptions = {
  scanTypes: [scanCore.ScanType.ALL],
  enableMultiMode: true,
  enableAlbum: false
};
detectBarcode.decodeImage(byteImg, options).then((result: detectBarcode.DetectResult) => {
  hilog.info(0x0001, '[Scan Sample]', 'Succeeded in getting DetectResult by promise by promise with options, result is ${JSON.stringify(result)}');
}).catch((error: BusinessError) => {
  hilog.error(0x0001, '[Scan Sample]', `Failed to get DetectResult by promise with options. Code: ${error.code}, message: ${error.message}`);
})

说明

不支持并行调用。

内容来源 HarmonyOS NEXT API12 官方文档

相关推荐
威哥爱编程12 小时前
HarmonyOS 7 自由多窗实战:supportWindowMode + 断点布局,大屏多任务主动接管
harmonyos·arkts
威哥爱编程12 小时前
HarmonyOS 7 应用接续实战:continuable + onContinue 跨设备无缝流转,手机编辑平板接着搞
harmonyos·arkts
威哥爱编程12 小时前
HarmonyOS 7 视觉 AI 进阶实战:人脸检测 + 通用文字识别(OCR)两步接入
harmonyos
李游Leo19 小时前
HarmonyOS 7 API 26 升级适配实战:API Change Assistant、targetSDKVersion 与兼容性回归
harmonyos
OH_TPC19 小时前
HarmonyOS APP开发---"智泊"智能停车App,需要用到这个库
harmonyos
用户1179104883320 小时前
AI粘贴多行需求为什么会误执行?hmharness安全粘贴拆解 GitHub:swsgbl/hmharness
ai编程·harmonyos
用户09340777351420 小时前
HarmonyOS WPS Open SDK 实践:从 HAR 集成到 OpenFileRequest 最小闭环
typescript·harmonyos
威哥爱编程1 天前
HarmonyOS 7 Fast Kit 算法加速实战:四大核心能力,把算力从系统里"借"出来
harmonyos·arkts
威哥爱编程1 天前
HarmonyOS 7 游戏快启实战:Graphics Accelerate Kit 内存镜像秒级启动 + 预启动,把读条变成"秒进"
harmonyos·arkts
木子雨廷1 天前
第 19 天|Preferences:轻量缓存与状态恢复
harmonyos