最近在写截图功能,参考文档 链接点击
但是发现,截图后,图片是倒的。
目前我已解决,分享解决方案。
设原图为a,所截图为b
经过分析,发现a的所有像素沿着x轴做一次对称处理后即得b图。
故,a图第i行像素点=b图第(b.height - i -1)行像素点
全部代码和注释如下:
let rt = new cc.RenderTexture();
rt.initWithSize(captureNode.width, captureNode.height);
this.captureCamera.targetTexture = rt;
this.captureCamera.render(captureNode);
//readPixels 会返回一个大小为 (长 x 高 x 4) 的 Uint8Array
//每个像素点是一个RGBA,即4个Unit8
let data = rt.readPixels();
let length = data.length;
let data2 = new Uint8Array(length);
let width = captureNode.width;
let height = captureNode.height;
//原图第i行=新图第length-i-1行
for(let i=0;i<length;i++){
let row02 = parseInt(i/(4*width) + "");
let row01 = height-row02-1;
let index = row01*width*4+i%(4*width);
data2[i] = data[index];
}
//存储所截图片
let filePath = jsb.fileUtils.getWritablePath() + 'Image.png';
jsb.saveImageData(data2, captureNode.width, captureNode.height, filePath);