HarmonyOS Next鸿蒙开发:ImageBitmap

180 阅读1分钟

了解下ImageBitmap ,这个做图片和自定义一些组件或者做一些游戏可能需要用到,还有通过base64转成图片也需要用到imageBitmap, ImageBitmap 不支持读取resource下的资源,也不支持直接读取网络http的图片,可以读取SD卡下的资源,但是前缀有要求,datashare://路径前缀,file://data/storage路径前缀的字符串

读取手机里面的图片,参考demo如下:

Canvas(this.context)
          .width('100%')
          .height('100%')
          .backgroundColor('#ffff00')
          .onReady(async () => {
            const img = await image.createImageSource("file://data/storage/el2/base/haps/entry/files/picture2.jpg").createPixelMap()
            const imgInfo = await img.getImageInfo()
            console.log(JSON.stringify(imgInfo.size))
            this.context.drawImage(img, 0, 0)
            this.img.close()
          })

读取工程项目下的文件,将资源读取到imagebitmap,通过drawImage 画图将图片放在画布上。

 static img:ImageBitmap = new ImageBitmap('/common/images/bullet.png')
 
   context.drawImage(
          GamePlayer.img,
          GameInfo.my.my_dan[i],
          GameInfo.my.my_dan[i+1]-10,
         25,
         25
        )