目录

Generating a Barcode Image from a Byte Array

The barcode image generation capability can convert a byte array into a barcode image of a custom format.

Use Cases

The barcode image generation capability can convert a byte array into a barcode image of a custom format.

For example, you can use the capability to convert a byte array into a QR code of a transportation card.

Constraints

Flowchart

  1. A user sends a barcode image generation request to your atomic service and inputs required information, such as the barcode type and the width and height of the barcode image.

  2. Your atomic service calls the createBarcode API of Scan Kit to start barcode image generation.

  3. Scan Kit converts the input byte array into a barcode image and sends it to your atomic service.

  4. Your atomic service sends the barcode image generation result to the user.

API Description

Generates a barcode image from a byte array and returns the result using a promise. For details about the API, please refer to API Reference.

API Description
createBarcode(content: ArrayBuffer, options: CreateOptions): Promise<image.PixelMap> Generates a barcode image. This API returns the generated barcode image of the image.PixelMap type, which can be rendered as an image using the Image component. This API returns the result asynchronously through a promise.

Development Procedure

The barcode image generation capability directly generates the required barcode image based on fixed parameters and optional parameters that are passed.

The following describes how to call the createBarcode API to generate a barcode image:

  1. Import the barcode image generation API module. This module provides parameters and methods for generating a barcode image.

    // Import the image and error code modules required for generating a barcode image.
    import { scanCore, generateBarcode } from '@kit.ScanKit';
    import { BusinessError } from '@kit.BasicServicesKit';
    import { image } from '@kit.ImageKit';
    import { hilog } from '@kit.PerformanceAnalysisKit';
    import { buffer } from '@kit.ArkTS';
  2. Call the createBarcode API to generate a barcode image.

    • Obtain the generated barcode image using a promise.
      const TAG: string = 'Create barcode';
      
      @Entry
      @Component
      struct Index {
        @State pixelMap: image.PixelMap | undefined = undefined;
      
        build() {
          Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
            Button('generateBarcode Promise').onClick(() => {
              this.pixelMap = undefined;
              let content: string =
                '0177C10DD10F7768600202312110000063458FD14112345678FFFFD381012610b746365409210201b66636540ad0200020000000000110e617003201000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006645fbec664358ECF657CB40693c92da';
              let contentBuffer: ArrayBuffer = buffer.from(content, 'hex').buffer; // Convert a character string that contains hexadecimal characters into an ArrayBuffer.
              let options: generateBarcode.CreateOptions = {
                scanType: scanCore.ScanType.QR_CODE,
                height: 400,
                width: 400
              };
              try {
                // Generate a barcode image. If the operation is successful, a PixelMap image is returned.
                generateBarcode.createBarcode(contentBuffer, options).then((pixelMap: image.PixelMap) => {
                  this.pixelMap = pixelMap;
                  hilog.info(0x0001, TAG, 'Succeeded in creating barCode.');
                }).catch((err: BusinessError) => {
                  hilog.error(0x0001, TAG, `Failed to createBarCode. Code: ${err.code}, message: ${err.message}`);
                });
              } catch (err) {
                hilog.error(0x0001, TAG,
                  `Failed to createBarcode by Promise with options. Code: ${err.code}, message: ${err.message}`);
              }
            })
            // Display the obtained barcode image.
            if (this.pixelMap) {
              Image(this.pixelMap).width(300).height(300).objectFit(ImageFit.Contain)
            }
          }
          .width('100%')
          .height('100%')
        }
      }

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.