Read the original article:How to create barcode in wearable device
Context
The barcode image generation capability can convert a string into a barcode image of a custom format.
Description
This capability can convert a string into a 1D code or 2D code of a custom format.
You can use this capability to convert a string into a contact barcode or a phone clone barcode, for example, converting string "HUAWEI" into a barcode.
Solution / Approach
@Entry
@Component
struct Index {
@State pixelMap: image.PixelMap | undefined = undefined
build() {
Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
Button('generateBarcode Callback').onClick(() => {
// Set parameters for generating a barcode image, for example, a QR code image.
let content = 'huawei';
let options: generateBarcode.CreateOptions = {
scanType: scanCore.ScanType.QR_CODE,
height: 400,
width: 400
}
try {
// Return a PixelMap-format barcode image.
generateBarcode.createBarcode(content, options, (error: BusinessError, pixelMap: image.PixelMap) => {
if (error) {
hilog.error(0x0001, '[generateBarcode]',
`Failed to get PixelMap by callback with options. Code: ${error.code}, message: ${error.message}`);
return;
}
this.pixelMap = pixelMap;
})
} catch (error) {
hilog.error(0x0001, '[generateBarcode]',
`Failed to createBarcode by callback with options. Code: ${error.code}, message: ${error.message}`);
}
})
// Display the obtained barcode image.
if (this.pixelMap) {
Image(this.pixelMap).width(300).height(300).objectFit(ImageFit.Contain)
}
}
.width('100%')
.height('100%')
}
}
Key Takeaways
- The barcode image generation capability supports mobile phones, tablets, smart wearables (supported since 5.1.0(18)), 2-in-1 devices (supported since 5.1.1(19)), and TVs (supported since 5.1.1(19)).
- This capability can generate barcodes in 13 formats, and the generation parameters vary depending on the barcode format. The table below describes restrictions for each barcode format.
Top comments (0)