detectBarcode模块提供图像识码能力,支持通过本地图片路径或字节数组两种方式输入图像,对图像中的条形码、二维码、MULTIFUNCTIONAL CODE进行识别。适用于需要从相册图片或其他图像数据(例如相机预览流)中提取码图信息的场景。
为便于开发者快速上手,建议参考官方提供的示例工程。
起始版本: 4.1.0(11)
待识别的图片信息。
模型约束: 此接口仅可在Stage模型下使用。
元服务API: 从API版本26.0.0开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Multimedia.Scan.ScanBarcode
起始版本: 4.1.0(11)
| 名称 | 类型 | 只读 | 可选 | 说明 |
|---|---|---|---|---|
| uri | string | 否 | 否 | 图片路径,例如file://media/Photo/x/xxx.jpg。 |
示例:
可通过PhotoViewPicker获取图片路径。
import { photoAccessHelper } from '@kit.MediaLibraryKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
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((data: photoAccessHelper.PhotoSelectResult) => {
if (!data || (data.photoUris && data.photoUris.length === 0)) {
hilog.error(0x0001, 'picker', 'Failed to get PhotoSelectResult by promise');
return;
}
hilog.info(0x0001, 'picker', `Succeeded in getting PhotoSelectResult by promise.`);
}).catch((err: BusinessError) => {
hilog.error(0x0001, 'picker', `Failed to get PhotoSelectResult by promise. Code: ${err.code}`);
}); decode(inputImage: InputImage, options?: scanBarcode.ScanOptions): Promise<Array<scanBarcode.ScanResult>>
通过配置参数调用图片识码返回识码结果。使用Promise异步回调。
模型约束: 此接口仅可在Stage模型下使用。
元服务API: 从API版本26.0.0开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Multimedia.Scan.ScanBarcode
起始版本: 4.1.0(11)
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| inputImage | InputImage | 是 | 待识别的图片信息。 |
| options | scanBarcode.ScanOptions | 否 | 启动图片识码参数。 默认值: 参考ScanOptions的默认值。 |
返回值:
| 类型 | 说明 |
|---|---|
| Promise<Array<scanBarcode.ScanResult>> | Promise对象,返回识码结果对象数组。 |
错误码:
以下错误码的详细介绍请参见通用错误码和ArkTS API错误码。
| 错误码ID | 错误信息 |
|---|---|
| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
| 1000500001 | Internal error. |
示例:
import { scanCore, scanBarcode, detectBarcode } from '@kit.ScanKit';
import { photoAccessHelper } from '@kit.MediaLibraryKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
// 定义识码参数options
let options: scanBarcode.ScanOptions = { scanTypes: [scanCore.ScanType.ALL], enableMultiMode: true, enableAlbum: true };
// 通过PhotoViewPicker拉起图库并选择图片
let photoOption = new photoAccessHelper.PhotoSelectOptions();
photoOption.MIMEType = photoAccessHelper.PhotoViewMIMETypes.IMAGE_TYPE;
photoOption.maxSelectNumber = 1;
let photoPicker = new photoAccessHelper.PhotoViewPicker();
photoPicker.select(photoOption).then((data) => {
// 定义识码参数inputImage,其中uri为PhotoViewPicker选择的图片路径
let inputImage: detectBarcode.InputImage = { uri: data.photoUris[0] };
try {
// 调用图片识码接口
detectBarcode.decode(inputImage, options).then((data: Array<scanBarcode.ScanResult>) => {
hilog.info(0x0001, '[Scan Sample]',
`Succeeded in getting ScanResult by promise with options, result length: ${data.length}`);
}).catch((err: BusinessError) => {
hilog.error(0x0001, '[Scan Sample]',
`Failed to get ScanResult by promise with options. Code: ${err.code}, message: ${err.message}`);
});
} catch (err) {
hilog.error(0x0001, '[Scan Sample]',
`Failed to detect Barcode. Code: ${err.code}, message: ${err.message}`);
}
}).catch((err: BusinessError) => {
hilog.error(0x0001, 'picker', `Failed to get PhotoSelectResult by promise. Code: ${err.code}.`);
}); decode(inputImage: InputImage, options: scanBarcode.ScanOptions, callback: AsyncCallback<Array<scanBarcode.ScanResult>>): void
通过配置参数调用图片识码返回识码结果。使用callback异步回调。
模型约束: 此接口仅可在Stage模型下使用。
元服务API: 从API版本26.0.0开始,该接口支持在元服务中使用。
系统能力: 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. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
| 1000500001 | Internal error. |
示例:
import { photoAccessHelper } from '@kit.MediaLibraryKit';
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 };
// 通过PhotoViewPicker拉起图库并选择图片
let photoOption = new photoAccessHelper.PhotoSelectOptions();
photoOption.MIMEType = photoAccessHelper.PhotoViewMIMETypes.IMAGE_TYPE;
photoOption.maxSelectNumber = 1;
let photoPicker = new photoAccessHelper.PhotoViewPicker();
photoPicker.select(photoOption).then((data) => {
// 定义识码参数inputImage,其中uri为PhotoViewPicker选择的图片路径
let inputImage: detectBarcode.InputImage = { uri: data.photoUris[0] };
try {
// 调用图片识码接口
detectBarcode.decode(inputImage, options, (err: BusinessError, data: Array<scanBarcode.ScanResult>) => {
if (err && err.code) {
hilog.error(0x0001, '[Scan Sample]',
`Failed to get ScanResult by callback with options. Code: ${err.code}, message: ${err.message}`);
return;
}
hilog.info(0x0001, '[Scan Sample]',
`Succeeded in getting ScanResult by callback with options, result length: ${data.length}`);
});
} catch (err) {
hilog.error(0x0001, '[Scan Sample]',
`Failed to detect Barcode. Code: ${err.code}, message: ${err.message}`);
}
}).catch((err: BusinessError) => {
hilog.error(0x0001, 'picker', `Failed to get PhotoSelectResult by promise. Code: ${err.code}`);
}); decode(inputImage: InputImage, callback: AsyncCallback<Array<scanBarcode.ScanResult>>): void
图片识码返回识码结果。使用callback异步回调。
模型约束: 此接口仅可在Stage模型下使用。
元服务API: 从API版本26.0.0开始,该接口支持在元服务中使用。
系统能力: 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. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
| 1000500001 | Internal error. |
示例:
import { scanBarcode, detectBarcode } from '@kit.ScanKit';
import { photoAccessHelper } from '@kit.MediaLibraryKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
// 通过PhotoViewPicker拉起图库并选择图片
let photoOption = new photoAccessHelper.PhotoSelectOptions();
photoOption.MIMEType = photoAccessHelper.PhotoViewMIMETypes.IMAGE_TYPE;
photoOption.maxSelectNumber = 1;
let photoPicker = new photoAccessHelper.PhotoViewPicker();
photoPicker.select(photoOption).then((data) => {
// 定义识码参数inputImage,其中uri为PhotoViewPicker选择的图片路径
let inputImage: detectBarcode.InputImage = { uri: data.photoUris[0] };
try {
// 调用图片识码接口
detectBarcode.decode(inputImage, (err: BusinessError, data: Array<scanBarcode.ScanResult>) => {
if (err && err.code) {
hilog.error(0x0001, '[Scan Sample]',
`Failed to get ScanResult by callback. Code: ${err.code}, message: ${err.message}`);
return;
}
hilog.info(0x0001, '[Scan Sample]',
`Succeeded in getting ScanResult by callback, result length: ${data.length}`);
});
} catch (err) {
hilog.error(0x0001, '[Scan Sample]',
`Failed to detect Barcode. Code: ${err.code}, message: ${err.message}`);
}
}).catch((err: BusinessError) => {
hilog.error(0x0001, 'picker', `Failed to get PhotoSelectResult by promise. Code: ${err.code}`);
}); 待识别的图像数据。
模型约束: 此接口仅可在Stage模型下使用。
元服务API: 从API版本26.0.0开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Multimedia.Scan.ScanBarcode
起始版本: 5.0.0(12)
| 名称 | 类型 | 只读 | 可选 | 说明 |
|---|---|---|---|---|
| byteBuffer | ArrayBuffer | 否 | 否 | 图像数据。存放图像的字节数组。 |
| width | number | 否 | 否 | 图像宽度,单位:px。 |
| height | number | 否 | 否 | 图像高度,单位:px。 |
| format | ImageFormat | 否 | 否 | 图像数据(byteBuffer)的类型。目前仅支持NV21像素格式。 |
示例:
示例中的buffer可通过相机预览流数据获取,参见双路预览。
import { detectBarcode } from '@kit.ScanKit';
// 图像的buffer, height, width数据,可通过相机预览流数据获取,比如获取宽高是1920*1080时
let byteImg: detectBarcode.ByteImage = {
byteBuffer: buffer,
width: 1920,
height: 1080,
format: detectBarcode.ImageFormat.NV21
}; 枚举,图像数据类型。
模型约束: 此接口仅可在Stage模型下使用。
元服务API: 从API版本26.0.0开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Multimedia.Scan.ScanBarcode
起始版本: 5.0.0(12)
| 名称 | 值 | 说明 |
|---|---|---|
| NV21 | 0 | 图像的像素格式为NV21。 |
识别结果。decodeImage接口的返回值,包含码的识别结果以及相机变焦建议。
模型约束: 此接口仅可在Stage模型下使用。
元服务API: 从API版本26.0.0开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Multimedia.Scan.ScanBarcode
起始版本: 5.0.0(12)
| 名称 | 类型 | 只读 | 可选 | 说明 |
|---|---|---|---|---|
| scanResults | Array<scanBarcode.ScanResult> | 否 | 否 | 扫码结果。 |
| zoomValue | number | 否 | 否 | 期望图像放大倍数,在值大于1时,可通过setZoomRatio控制相机进行变焦放大图像。 说明: 1. 使用Camera Kit getZoomRatio接口获取相机当前变焦比zoomRatio。 2. 使用Camera Kit setZoomRatio接口设置targetRatio,目标值为zoomRatio * zoomValue。 |
decodeImage(image: ByteImage, options?: scanBarcode.ScanOptions): Promise<DetectResult>
通过配置参数调用图像数据识码能力返回识码结果。使用Promise异步回调。
模型约束: 此接口仅可在Stage模型下使用。
元服务API: 从API版本26.0.0开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Multimedia.Scan.ScanBarcode
起始版本: 5.0.0(12)
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| image | ByteImage | 是 | 待识别的图像数据。 |
| options | scanBarcode.ScanOptions | 否 | 启动图像数据识码参数。 默认值: 参考ScanOptions的默认值。 |
返回值:
| 类型 | 说明 |
|---|---|
| Promise<DetectResult> | Promise对象,返回图像数据识码结果对象。 |
错误码:
以下错误码的详细介绍请参见通用错误码和ArkTS API错误码。
| 错误码ID | 错误信息 |
|---|---|
| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
| 1000500001 | Internal error. |
示例:
示例中的buffer可通过相机预览流数据获取,参见双路预览。
import { scanCore, scanBarcode, detectBarcode } from '@kit.ScanKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
// 图像的buffer, height, width数据,可通过相机预览流数据获取,比如获取宽高是1920*1080时
let byteImg: detectBarcode.ByteImage = {
byteBuffer: buffer,
width: 1920,
height: 1080,
format: detectBarcode.ImageFormat.NV21
};
let options: scanBarcode.ScanOptions = {
scanTypes: [scanCore.ScanType.ALL],
enableMultiMode: true,
enableAlbum: false
};
try {
detectBarcode.decodeImage(byteImg, options).then((data: detectBarcode.DetectResult) => {
hilog.info(0x0001, '[Scan Sample]',
`Succeeded in getting DetectResult by promise with options, result length: ${data.scanResults.length}, zoomValue: ${data.zoomValue}`);
}).catch((err: BusinessError) => {
hilog.error(0x0001, '[Scan Sample]',
`Failed to get DetectResult by promise with options. Code: ${err.code}, message: ${err.message}`);
});
} catch (err) {
hilog.error(0x0001, '[Scan Sample]', `Failed to decode Image. Code: ${err.code}, message: ${err.message}`);
}