根据文档示例:
使用generateBarcode.createBarcode生成码图的成功回调是空的对象类型,现在需要返回二维码/条形码图片
demo代码如下:
import { image } from '@kit.ImageKit';
import { scanCore, generateBarcode } from '@kit.ScanKit';
import { BusinessError } from '@kit.BasicServicesKit';
const TAG = 'Index';
@Entry
@Component
struct Index {
// 以QR码为例,码图生成参数
@State content: string = 'https://mu-mobile.ceair.com/mu-weex/#/';
@State options: generateBarcode.CreateOptions = {
scanType: scanCore.ScanType.QR_CODE,
height: 200,
width: 200
}
@State result: string = ''
build() {
Row() {
Text(this.result).fontSize(20)
}
}
onPageShow() {
generateBarcode.createBarcode(this.content, this.options, (error: BusinessError, result: image.PixelMap) => {
if (error) {
console.log(`二维码失败========${error.message}`);
return;
}
this.result = JSON.stringify(result);
console.log(`二维码成功返回========${JSON.stringify(result) + typeof result}`);
console.log(`this.content========${this.content}`);
console.log(`this.options========${JSON.stringify(this.options)}`);
})
}
}
generateBarcode.createBarcodetg生成二维码pixelMap对象,再将pixelMap转为base64字符串,示例参考:
将resource中图片转base64:
1、resource中图片转imagesource:
参考地址:
https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/image-decoding-V5\#ZH-CN\_TOPIC\_0000001847052324\_\_%E5%BC%80%E5%8F%91%E7%A4%BA%E4%BE%8B-%E5%AF%B9%E8%B5%84%E6%BA%90%E6%96%87%E4%BB%B6%E4%B8%AD%E7%9A%84%E5%9B%BE%E7%89%87%E8%BF%9B%E8%A1%8C%E8%A7%A3%E7%A0%81
2、imagesource先编码,再转base64: