HarmonyOS NEXT 使用PixelMap深拷贝案例

本文档详细介绍了如何在 HarmonyOS 中使用 PixelMap 的深拷贝功能,通过 readPixelsToBuffer 实现从源 PixelMap 到目标 PixelMap 的数据复制。在关于ToAppear方法中,首先获取图片资源,然后创建源和目标 PixelMap,并利用 copyPixelMap 函数进行拷贝。同时,还涉及到根据比例裁剪 PixelMap 和保存为 JPEG 文件的操作。
摘要由CSDN通过智能技术生成

// TODO 知识点:通过readPixelsToBuffer实现PixelMap的深拷贝,其中readPixelsToBuffer输出为BGRA_8888
await pm.readPixelsToBuffer(buffer);
// TODO 知识点:readPixelsToBuffer输出为BGRA_8888,此处createPixelMap需转为RGBA_8888
const opts: image.InitializationOptions = {
editable: true,
pixelFormat: image.PixelMapFormat.RGBA_8888,
size: { height: imageInfo.size.height, width: imageInfo.size.width }
};
return await image.createPixelMap(buffer, opts);
}

2. 初始化时,通过深拷贝从原始PixelMap创建目标PixelMap。源码参考[ImageDepthCopy.ets]( )。

 

async aboutToAppear(): Promise {
const context: Context = getContext(this);
// 获取resourceManager资源管理
const resourceMgr: resourceManager.ResourceManager = context.resourceManager;
// 获取rawfile中的图片资源
const fileData: Uint8Array = await resourceMgr.getRawFileContent(ImageCropConstants.RAWFILE_PICPATH);
// 获取图片的ArrayBuffer
const buffer = fileData.buffer.slice(fileData.byteOffset, fileData.byteLength + fileData.byteOffset);
// 保存用于恢复原图的imageSource
this.imageSource = image.createImageSource(buffer);
// TODO 知识点: 创建源图片的用于深拷贝的PixelMap,因readPixelsToBuffer输出为BGRA_8888。故此处desiredPixelFormat需为BGRA_8888
const decodingOptions: image.DecodingOptions = {
editable: false, // 是否可编辑。当取值为false时,图片不可二次编辑。
desiredPixelFormat: image.PixelMapFormat.BGRA_8888,
}
// 保存用于深拷贝的pixelMap
this.pixelMapSrc = await this.imageSource.createPixelMap(decodingOptions);
//

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值