Draw image from pixel array on canvas with putImageData

前端 未结 2 1047
不思量自难忘°
不思量自难忘° 2021-02-14 12:50

I am working on a project that can encrypt an image and redraw the decrypted image on canvas. As I am still pretty new to coding and programming, I am currently having issues re

2条回答
  •  长情又很酷
    2021-02-14 13:46

    Assuming imgd is simply an Array containing all byte values, you still need to convert the array to ImageData.

    var imgd = [27,32,26,28,33,27,30,35,29,31.....]
    
    // first, create a new ImageData to contain our pixels
    var imgData = ctx.createImageData(160, 120); // width x height
    var data = imgData.data;
    
    // copy img byte-per-byte into our ImageData
    for (var i = 0, len = 160 * 120 * 4; i < len; i++) {
        data[i] = imgd[i];
    }
    
    // now we can draw our imagedata onto the canvas
    ctx.putImageData(imgData, 0, 0);
    

提交回复
热议问题