HarmonyOS Resource类型图片如何转换为ImageBitmap?(目前需要把图片放在ets目录下)

应用需要做图片裁剪,使用ImageBitmap加载图片数据,但是ImageBitmap只支持string和pixelMap的参数,如果我想使用$获取资源文件,无法直接使用,请问如何处理。

HarmonyOS
2024-10-21 10:21:05
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
FengTianYa

可参考的文档如下:

https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-components-canvas-imagebitmap-V5

https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-image-V5#getimageinfo7

代码参考:

Canvas(this.context)  
  .width('100%')  
  .height('100%')  
  .backgroundColor('#ffff00')  
  .onReady(() => {  
    getContext(this).resourceManager.getMediaContent($r('app.media.product001')).then((data) => {  
      let arrayBuffer = data.buffer.slice(data.byteOffset, data.byteLength + data.byteOffset)  
      let imageSource: image.ImageSource = image.createImageSource(arrayBuffer);  
      imageSource.getImageInfo((err, value) => {  
        if (err) {  
          return;  
        }  
        let opts: image.DecodingOptions = {  
          editable: true,  
          desiredSize: {  
            height: value.size.height,  
            width: value.size.width  
          }  
        };  
        imageSource.createPixelMap(opts, (err, pixelMap) => {  
          // 这个回调的pixelMap就是当前图片的pixelMap  
          this.context.drawImage(pixelMap, 0, 0, 500, 500, 0, 0, 400, 200)  
        })  
      })  
    })  
  })
  • 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.
分享
微博
QQ
微信
回复
2024-10-21 15:15:11
相关问题
HarmonyOS 图片转换ImageBitmap
1036浏览 • 1回复 待解决
HarmonyOS 如何将base64数据转换为图片
1455浏览 • 1回复 待解决
HarmonyOS Resource怎么进行转换类型
1404浏览 • 1回复 待解决
canvas怎么绘制资源目录下图片
1490浏览 • 1回复 待解决