var that = this
var ctx = wx.createCanvasContext('canvasNew', that);
wx.getImageInfo({
src: 'https://xxxxxxxxxxxxxxxxxxxxxx.png',
success: function (res) {
ctx.drawImage(res.path,0,0,res.width, res.height);
ctx.draw(false, ()=>{
wx.canvasGetImageData({
canvasId: 'canvasNew',
x: 0,
y: 0,
width: res.width,
height: res.height,
success(result) {
let data = result.data;
for (let i = 0; i < result.width * result.height;i++){
let R = data[i * 4 + 0];
let G = data[i * 4 + 1];
let B = data[i * 4 + 2];
var avg = (R * 0.3 + G * 0.59 + B * 0.11)
if (avg < 128) {
data[i * 4 + 0] = 2 * avg;
data[i * 4 + 1] = 0;
data[i * 4 + 2] = 0;
} else {
data[i * 4 + 0] = 255;
data[i * 4 + 1] = 2 * avg - 255;
data[i * 4 + 2] = 2 * avg - 255;
}
}
wx.canvasPutImageData({
canvasId: 'canvasNew',
x: 0,
y: 0,
width: res.width,
data: data,
success(res) {
console.log(res)
},
fail(e) {
}
}, that)
},
fail(e){
console.log(e)
}
}, that)
})
}
})