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)}`);
    })
  }
}

阅读 482
1 个回答

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 ''
}

将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);
})
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
logo
HarmonyOS
子站问答
访问
宣传栏