#鸿蒙通关秘籍#在HarmonyOS NEXT中如何通过缓冲区数组创建ImageSource并解码为PixelMap?

HarmonyOS
2024-12-03 10:51:38
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
CDN风绘空

首先,通过资源管理器获取资源文件的ArrayBuffer,再基于该缓冲区数组创建ImageSource对象,随后进行解码。步骤如下:

const context : Context = getContext(this);
const resourceMgr : resourceManager.ResourceManager = context.resourceManager;

resourceMgr.getRawFileContent('test.jpg').then((fileData : Uint8Array) => {
    console.log("Succeeded in getting RawFileContent");

    const buffer = fileData.buffer.slice(0);
    const imageSource : image.ImageSource = image.createImageSource(buffer);

    let decodingOptions : image.DecodingOptions = {
        editable: true,
        desiredPixelFormat: 3,
    }

    imageSource.createPixelMap(decodingOptions).then((pixelMap : image.PixelMap) => {
        pixelMap.rotate(90);
        pixelMap.scale(0.5, 0.5);

        console.log("Succeeded in creating PixelMap");
    }).catch((err : BusinessError) => {
        console.error("Failed to create PixelMap");
    });

}).catch((err : BusinessError) => {
    console.error("Failed to get RawFileContent");
});
  • 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.

在这个方法中,通过resourceManager获取图片的数据缓冲区,再利用image模块创建ImageSource,并设置解码参数后解码为PixelMap对象,最后对图像进行旋转和缩放操作。

分享
微博
QQ
微信
回复
2024-12-03 13:46:27
相关问题
HarmonyOS 生成xml时缓冲区大小问题
1224浏览 • 1回复 待解决