DEV Community

HarmonyOS
HarmonyOS

Posted on

Print Barcode on the Screen

Read the original article:Print Barcode on the Screen

Requirement Description

User wants to print qr on the screen with Scan Kit

Background Knowledge

To use this module, the SystemCapability.Multimedia.Scan.GenerateBarcode capability must be enabled in the system. The API version should meet the minimum requirements: 4.1.0(11) or above for basic usage, 5.0.2(14) or above for Quick App APIs, and 5.0.0(12) or above for barcode generation based on byte arrays. The necessary libraries must be imported into the project. These include @kit.ScanKit for barcode generation, @kit.ImageKit for image output, @kit.BasicServicesKit for error handling, @kit.PerformanceAnalysisKit for logging, and @kit.ArkTS buffer support if the content is provided in byte array format. Once these requirements are satisfied, the generateBarcode module can be used to produce compatible and reliable barcode images.

Implementation Steps

Import scan and image kit. We use image kit for print the barcode. We use pixelmap type in the image kit. We have to set scanType, width, height, level, backgroundColor and pixelmapColor etc. After that, we have to configure scanType. And write to the screen.

Code Snippet / Configuration

import { image } frm '@kit.ImageKit';
import { scanCore, ogenerateBarcode } from '@kit.ScanKit';Copy codeCopy code
const options: generateBarcode.CreateOptions = {
  scanType: scanCore.ScanType.QR_CODE,
  width: 400,
  height: 400
};
const pixelMapResult = await generateBarcode.createBarcode(this.pnrNumber, options);
this.qrCodeImage = pixelMapResult;
hilog.info(0x0001, TAG, 'QR Code generated.');
Enter fullscreen mode Exit fullscreen mode

Test Results

Tested on two real devices - works as expected.

cke_824.pngcke_2741.png

Limitations or Considerations

Supported Devices

  • Huawei Watch 5

Related Documents or Links

https://developer.huawei.com/consumer/en/doc/harmonyos-references-V13/scan-generatebarcode-V13

Written by Zulfu Balkan

Top comments (0)