We use essential cookies for the website to function, as well as analytics cookies for analyzing and creating statistics of the website performance. To agree to the use of analytics cookies, click "Accept All". You can manage your preferences at any time by clicking "Cookie Settings" on the footer. More Information.

Only Essential Cookies
Accept All

Recognizing Barcodes in Image Data

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.

Use Cases

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.

Flowchart

  1. A user sends a barcode recognition request to your atomic service.

  2. Your atomic service calls the decodeImage API of Scan Kit to recognize the barcode image.

  3. Scan Kit returns the barcode recognition result.

  4. Your atomic service sends the barcode scanning result to the user.

API Description

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.

Expand
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.

Development Procedure

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.

  1. 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';
  2. 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)
      }
    }

Emulator-based Development

This capability does not support Emulator-based development currently, and the error message "The capability is not supported on the emulator at this time." will be displayed if you attempt to perform Emulator-based development.