The barcode image generation capability can convert a byte array into a barcode image of a custom format.
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.
The barcode image generation capability supports phones, tablets, wearables, PCs/2-in-1 devices, and TVs. Specifically, wearables are supported since API version 5.1.0(18), and PCs/2-in-1 devices and TVs are supported since API version 5.1.1(19).
If the data that Scan Kit recognizes from a barcode image generated based on a byte array is garbled characters, a dedicated decoder is required to decode the byte array. Such cases include barcode recognition of subway turnstiles.

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.
Your atomic service calls the createBarcode API of Scan Kit to start barcode image generation.
Scan Kit converts the input byte array into a barcode image and sends it to your atomic service.
Your atomic service sends the barcode image generation result to the user.
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. |
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:
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'; Call the createBarcode API to generate a barcode image.
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%')
}
}