First of all I am aware there are standard methods of achieving this (readAsDataURL and drawImage), but unfortunately they are not usable for this specific use case.
I am reading an image using the filereader API as an arraybuffer like so:
var reader = new fileReader();
reader.onload = function(e){
var byteArray = new Uint8ClampedArray(e.target.result);
//do stuff to this array
}
reader.readAsArrayBuffer(file);
I am then creating a clampedarray with this returned data.
What I want to be able to do is now draw this pixel array directly to a canvas using putImageData
Currently I am doing the following:
var byteArray = new Uint8ClampedArray(e.target.result);
var imgdata = ctx.createImageData(img.width, img.height);
var canvdata = imgdata.data;
for (var i = 0; i < canvdata.length; i += 4) {
canvdata[i] = byteArray[i];
canvdata[i + 1] = byteArray[i + 1];
canvdata[i + 2] = byteArray[i + 2];
canvdata[i + 3] = byteArray[i + 3];
}
ctx.putImageData(imgdata, 0, 0);
Using this method, although the data gets drawn to the canvas, it appears like garbled snow or noise and is not a correct representation of the image.
This leads me to believe that I am constructing the imagedata data incorrectly in my loop or perhaps my initial method of getting the pixel array is incorrect.
To summarize, I would like to be able to take an arraybuffer (of a jpeg) retrieved via the html5 fileReader API and then create a canvas compatible imageData array, so that it can later be pushed into a canvas using putImageData
Thanks in advance.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…