HarmonyOS 如何将jpeg Uint8Array 转为ui展示image?

如题:HarmonyOS 如何将jpeg Uint8Array 转为ui展示image?

阅读 733
1 个回答

参考此文档 https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-image-V5\#writebuffertopixels7

或者另一种 demo

fn1() {
  let ctx = getContext(this) as common.UIAbilityContext;
  const resourceMgr: resourceManager.ResourceManager = ctx.resourceManager;
  // 'ic_poster_style_personal_info2.png'
  resourceMgr.getRawFileContent('ic_poster_style_personal_info2.png').then((fileData: Uint8Array) => {
    console.log("Succeeded in getting RawFileContent")
    // 获取图片的ArrayBuffer
    const buffer = fileData.buffer.slice(0);
    const imageSource = image.createImageSource(buffer);
        this.pixelMap= await imageSource.createPixelMap(decodingOptions)
      }
    }).catch((error: BusinessError) => {
      console.error('Failed to pack the image. And the error is: ' + error);
    })
  })
}


Image(this.pixelMap)
  .border({ width: 1, color: Color.Blue })
  .width(200).height(200)
  .objectFit(ImageFit.None)
  .backgroundColor(Color.White)
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进