HarmonyOS鸿蒙Next中image.createPixelMap方法与pixelMap.readPixelsToBuffer方法咨询
HarmonyOS鸿蒙Next中image.createPixelMap方法与pixelMap.readPixelsToBuffer方法咨询
目前遇到了这样一种情况:
前提:iconMap是一个PixelMap对象,通过以下代码
let bufSize = iconMap.getPixelBytesNumber();
let iconBuffer: ArrayBuffer = new ArrayBuffer(bufSize);
await iconMap.readPixelsToBuffer(iconBuffer);
let sqr = Math.sqrt(bufSize / 4);
let opts: image.InitializationOptions = { editable: true, pixelFormat: 4, size: { height: sqr, width: sqr } }
let resultMap = await image.createPixelMap(iconBuffer, opts);
最后将resultMap赋值给Image组件,但是组件显示的图片色值发生了变化,这个是怎么回事,我需要怎么修改才能使图片展示和原图像一样呢?
更多关于HarmonyOS鸿蒙Next中image.createPixelMap方法与pixelMap.readPixelsToBuffer方法咨询的实战教程也可以访问 https://www.itying.com/category-93-b0.html
需要将图片前后处理的格式保持一致即可:
async createCanvasPixelMap() {
let resourceMgr: resourceManager.ResourceManager = (getContext(this) as common.UIAbilityContext).resourceManager;
let startIconRawDesc = await resourceMgr.getRawFd('startIcon.png');
let imageSrc: image.ImageSource = image.createImageSource(startIconRawDesc);
let iconMap: image.PixelMap = await imageSrc.createPixelMap({desiredPixelFormat:image.PixelMapFormat.BGRA_8888});
this.defaultIcon = iconMap;
console.log(`${this.defaultIcon.getPixelBytesNumber()}`)
let iconBufSize = iconMap.getPixelBytesNumber();
console.log(`${Math.sqrt(4)}`)
let sqr = Math.sqrt(iconBufSize / 4)
let opts: image.InitializationOptions = { editable: true, pixelFormat: 4, size: { height: sqr, width: sqr } }
let iconBuffer: ArrayBuffer = new ArrayBuffer(iconBufSize);
await iconMap.readPixelsToBuffer(iconBuffer)
this.icon = await image.createPixelMap(iconBuffer, opts);
imageSrc.release();
}
更多关于HarmonyOS鸿蒙Next中image.createPixelMap方法与pixelMap.readPixelsToBuffer方法咨询的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
我也遇到了这个问题。看了其他的贴子总结下来就是官方的接口问题。他那个readPixelsToBuffer读出来是(r,g,b,t)的,createPixelMap又是按(r,b,g,t)存的。我用的比较笨的解决方法。就是将读出的buffer转为pixel对象后在读一次buffer,然后再把这个buffer转为pixel对象就是正确的了。
async createCanvasPixelMap() {
let resourceMgr: resourceManager.ResourceManager = (getContext(this) as common.UIAbilityContext).resourceManager;
let startIconRawDesc = await resourceMgr.getRawFd('startIcon.png');
let imageSrc: image.ImageSource = image.createImageSource(startIconRawDesc);
// let iconMap: image.PixelMap = await imageSrc.createPixelMap();
let iconMap: image.PixelMap = await imageSrc.createPixelMap({desiredPixelFormat:image.PixelMapFormat.BGRA_8888});
this.defaultIcon = iconMap;
console.log(`${this.defaultIcon.getPixelBytesNumber()}`)
let iconBufSize = iconMap.getPixelBytesNumber();
console.log(`${Math.sqrt(4)}`)
let sqr = Math.sqrt(iconBufSize / 4)
let opts: image.InitializationOptions = { editable: true, pixelFormat: 4, size: { height: sqr, width: sqr } }
let iconBuffer: ArrayBuffer = new ArrayBuffer(iconBufSize);
await iconMap.readPixelsToBuffer(iconBuffer)
let tmp = await image.createPixelMap(iconBuffer, opts);
awit tmp.readPixelsToBuffer(iconBuffer);
this.icon = await image.createPixelMap(iconBuffer, opts);
// this.icon = iconMap;
imageSrc.release();
}
你照着我这样加2行试试
在HarmonyOS鸿蒙Next中,image.createPixelMap方法用于从ImageSource对象创建一个PixelMap对象,该对象表示图像的像素数据。PixelMap是鸿蒙系统中用于处理图像像素的核心类,支持对图像像素进行读取和操作。
pixelMap.readPixelsToBuffer方法则用于将PixelMap中的像素数据读取到指定的ArrayBuffer或Buffer中。通过该方法,可以将图像像素数据以原始格式(如RGBA)提取到内存中,便于进一步处理或分析。
这两个方法通常在图像处理、渲染或分析场景中使用,结合使用可以实现从图像源到像素数据的高效转换和操作。
在HarmonyOS鸿蒙Next中,image.createPixelMap方法用于从图像源(如资源文件或网络)创建PixelMap对象,该对象代表图像数据。而pixelMap.readPixelsToBuffer方法则用于将PixelMap中的像素数据读取到指定的缓冲区中,以便进一步处理或分析。这两个方法结合使用,可以实现图像的加载和像素数据的读取操作,适用于图像处理和渲染等场景。

