The image-based barcode recognition capability is supported since API version 26.0.0.
The image-based barcode recognition capability can scan and recognize a barcode image from NV21 pixel-format images to retrieve information.
This capability can recognize 1D codes, QR codes, and multifunctional codes from NV21 pixel-format images and retrieve information such as the code type, value, position information, and expected image zoom ratio. It can be used to recognize one or multiple barcodes, such as 1D codes and payment codes, on an image.
Recognizes a barcode image in the image data and returns the recognition result using a promise. For details about the API, please refer to API Reference.
| API | Description |
|---|---|
| decodeImage(image: ByteImage, options?: scanBarcode.ScanOptions): Promise<DetectResult> | Starts image-based barcode recognition and returns the recognition result by passing the image data of the ByteImage type. This API returns the result asynchronously through a promise. |
The image-based barcode recognition capability can recognize 1D codes, QR codes, and multifunctional codes from NV21 pixel-format images and return information such as the code type, value, position information (coordinates of the upper left and lower right corners of the minimum bounding rectangle of a barcode image, and coordinates of four corner points of the QR code), and expected image zoom ratio.
The following describes how to call the decodeImage API to obtain barcode image information.
Import the image-based barcode recognition API module and other relevant API modules. This API module provides parameters and methods for recognizing barcodes in images.
import { detectBarcode, scanBarcode, scanCore } from '@kit.ScanKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { hilog } from '@kit.PerformanceAnalysisKit'; Call the decodeImage API to parse image data.
@Entry
@Component
struct DetectImagePage {
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(() => {
// Define scanning parameters.
let options: scanBarcode.ScanOptions = {
scanTypes: [scanCore.ScanType.ALL],
enableMultiMode: true
};
// Obtain the data in NV21 pixel format; for example, read the image data from the resources/rawfile directory.
this.getUIContext()
.getHostContext()?.resourceManager
.getRawFileContent('qrcode_NV21_image_1080X1920.yuv')
.then((imageData: Uint8Array) => {
let byteImage: detectBarcode.ByteImage = {
byteBuffer: imageData.buffer,
width: 1080,
height: 1920,
format: detectBarcode.ImageFormat.NV21
};
try {
// Call the image-based barcode recognition API.
detectBarcode.decodeImage(byteImage, 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 detectBarcode. Code: ${err.code}, message: ${err.message}`);
}
})
.catch((err: BusinessError) => {
hilog.error(0x0001, '[Scan Sample]',
`Failed to read image. Code: ${err.code}, message: ${err.message}`);
});
})
}
.width('100%')
.height('100%')
.alignItems(HorizontalAlign.Center)
.justifyContent(FlexAlign.Center)
}
}