请教社区老师,canvas的问题?canvasGetImageData
老师们好,我是在网上找了canvas将图片变成黑白照片的演示,但是不知道哪里错了,试了半天一点效果都调不出来
分别是这两个教程:
http://www.duanlonglong.com/qdjy/416.html
https://blog.csdn.net/u011539729/article/details/103069753
说的是要用canvasGetImageData和canvasPutImageData
wx.canvasGetImageData({
canvasId:'canvasID',
x:0,
y:0,
width: this.data.width,
height: this.data.height,
success(res) {
console.log(res.width,res.height)
ayy = res.data
let pixels = res.data.length; /*图像的总通道*/
for (let i = 0; i < pixels; i += 4) {
// 3.将每个像素的所有通道的值按权平均,和中性灰的色值进行对比
// 当前的r,g,b色值
let r = ayy[i]
let g = ayy[i + 1]
let b = ayy[i + 2]
// 计算结果值
val = parseInt((r + g + b) / 3)
// 黑白后的r,g,b色值
ayy[i] = val;
ayy[i + 1] = val;
ayy[i + 2] = val;
}
console.log('end')
wx.canvasPutImageData({
canvasId: 'canvasID',
x: 0,
y: 0,
width: t.data.width,
height: t.data.height,
data: ayy,
success (res) {}
})
}
})