从API版本26.0.0开始,新增支持图片识码能力。
图片识码能力支持对图库中的码图进行扫描识别,并获取信息。
接口返回值有两种返回形式: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,并返回图片中码类型,码值、码位置(码图最小外接矩形左上角和右下角的坐标)等信息。
以下示例为调用图片识码的decode()接口获取码图信息。
导入图片识码接口和相关接口模块,该接口提供了图片识码参数和方法,导入方法如下。
// 导入图片识码需要的日志和PhotoViewPicker模块
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 DetectPage {
build() {
Column() {
Button('Promise with options')
.backgroundColor('#0D9FFB')
.fontSize(20)
.fontColor($r('sys.color.comp_background_list_card'))
.fontWeight(FontWeight.Normal)
.align(Alignment.Center)
.type(ButtonType.Capsule)
.width('90%')
.height(40)
.margin({ top: 5, bottom: 5 })
.onClick(() => {
// 定义识码参数options
let options: scanBarcode.ScanOptions = {
scanTypes: [scanCore.ScanType.ALL],
enableMultiMode: 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 detectBarcode. Code: ${err.code}, message: ${err.message}`);
}
}).catch((err: BusinessError) => {
hilog.error(0x0001, '[Scan Sample]',
`Failed to select a photo. Code: ${err.code}, message: ${err.message}`);
});
})
}
.width('100%')
.height('100%')
.alignItems(HorizontalAlign.Center)
.justifyContent(FlexAlign.Center)
}
}@Entry
@Component
struct DetectPage {
build() {
Column() {
Button('Callback with options')
.backgroundColor('#0D9FFB')
.fontSize(20)
.fontColor($r('sys.color.comp_background_list_card'))
.fontWeight(FontWeight.Normal)
.align(Alignment.Center)
.type(ButtonType.Capsule)
.width('90%')
.height(40)
.margin({ top: 5, bottom: 5 })
.onClick(() => {
// 定义识码参数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 detectBarcode. Code: ${err.code}, message: ${err.message}`);
}
}).catch((err: BusinessError) => {
hilog.error(0x0001, '[Scan Sample]',
`Failed to select a photo. Code: ${err.code}, message: ${err.message}`);
});
})
}
.width('100%')
.height('100%')
.alignItems(HorizontalAlign.Center)
.justifyContent(FlexAlign.Center)
}
}