HarmonyOS 关于码图生成generateBarcode.createBarcode的问题

根据文档示例:

https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/scan-generatebarcode-V5#section1841142919352

使用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)}`);
    })
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.

HarmonyOS 关于码图生成generateBarcode.createBarcode的问题 -鸿蒙开发者社区

HarmonyOS
2024-12-23 16:42:26
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
put_get

generateBarcode.createBarcodetg生成二维码pixelMap对象,再将pixelMap转为base64字符串,示例参考:

let options: generateBarcode.CreateOptions = {
  scanType: scanCore.ScanType.QR_CODE,
  height: 300,
  width: 300
}
// 码图生成接口,成功返回PixelMap格式图片
generateBarcode.createBarcode(url, options, (error: BusinessError, pixelMap: image.PixelMap) => {
  if (error) {
    QDLogUtils.error(`createBarcode error ${JSON.stringify(error)}`);
  } else {
    QDStringUtils.pixelMapToBase64String(pixelMap).then((str) => {
      this.toSuccessCallback(`data:image/jpeg;base64,${str}`)
    })
  }
})

async pixelMapToBase64String(pixelMap: image.PixelMap): Promise<string> {
  const imagePackerApi: image.ImagePacker = image.createImagePacker();
  let packOpts: image.PackingOption = { format: 'image/jpeg', quality: 30 };
  try {
  let readBuffer = await imagePackerApi.packing(pixelMap, packOpts)
  let bufferArr = new Uint8Array(readBuffer)
  let str = new util.Base64Helper().encodeToStringSync(bufferArr)
  return str
} catch (err) {
  QDLogUtils.error(`pixelMapToBase64String err = ${err}`)
}
return ''
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.

将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:

const imagePackerApi = image.createImagePacker();
let packOpts: image.PackingOption = { format: "image/jpeg", quality: 100 };


imagePackerApi.packing(this.imagesource, packOpts).then(async (data: ArrayBuffer) => {
  // data 为打包获取到的文件流,写入文件保存即可得到一张图片

  if (data) {
    const imageSource: image.ImageSource = image.createImageSource(data);
    let decodingOptions: image.DecodingOptions = {
      editable: true,
      desiredPixelFormat: 3,
    }

    let buf: buffer.Buffer = buffer.from(data);
    let baseStr: string = buf.toString('base64', 0, buf.length);

    console.log(JSON.stringify(baseStr))
    //送显
    this.imgStr = "data:image/jpeg;base64," + baseStr;
  }
}).catch((error: BusinessError) => {
  console.error('Failed to pack the image. And the error is: ' + error);
})
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
分享
微博
QQ
微信
回复
2024-12-23 20:09:10
相关问题
HarmonyOS 关于柱状,环形绘制
870浏览 • 1回复 待解决
HarmonyOS 关于怎么还原设计问题
910浏览 • 1回复 待解决
HarmonyOS 二维生成demo
1120浏览 • 2回复 待解决
HarmonyOS APP可以生成二维,扫安装
1539浏览 • 1回复 待解决
Scan Kit无法识别多个
2402浏览 • 1回复 待解决
HarmonyOS 关于图片浏览大
836浏览 • 1回复 待解决
如何生成镂空遮罩?
708浏览 • 1回复 待解决
HarmonyOS 二维生成失败
754浏览 • 1回复 待解决
AI生成能力中文字符乱码
10426浏览 • 1回复 待解决
HarmonyOS直达问题
798浏览 • 1回复 待解决
HarmonyOS 启动设置问题
850浏览 • 1回复 待解决