Get pixel from bitmap
I need to process pixeldata from a 1000x1000px *.bmp image (~1MiB) in javascript
开发者_StackOverflow At the moment i am a bit stuck, because the page freezes when i try to dump the data to the console. the important code so far:var img = new Image();
img.src = 'image.bmp';
context.drawImage(img, 0, 0);
console.log(context.getImageData(0, 0, canvas.height, canvas.width));
i guess this is a performance issue, but is there a better way to access the pixel data? i dont really need to read it all at once, reading the pixels one by one would also be fine.
EDIT:
here is the updated code, it will populate a 2d-array with the red-value of the picture (i am dealing with a black/white picture, so thats enough)
var img = new Image();
img.src = 'image.bmp';
context.drawImage(img, 0, 0);
var imgData = context.getImageData(0, 0, canvas.height, canvas.width);
var pixel = new Array();
for(i=0;i<canvas.height;i++){
pixel[i] = new Array();
for(j=0;j<canvas.width;j++){
pixel[i][j] = imgData.data[i*canvas.width+j*4];
}
}
//now pixel[y][x] contains the red-value of the pixel at xy in img
no performance issues :) only quirk is that rows/columns are reversed
var data = context.getImageData(0, 0, canvas.height, canvas.width);
var count = 0;
var tmr = null;
var length = data.length;
(pix = function() {
var r = data[count + 0];
var g = data[count + 1];
var b = data[count + 2];
var a = data[count + 3];
var rgba = 'rgba(' + r + ' ,' + g + ' ,' + b + ' ,' + a + ')';
console.log(rgba);
if((count += 4) >= length) {
clearTimeout(tmr);
return;
}
tmr = setTimeout(pix, 1000/30); //at 30 fps
})();
Try creating a canvas of 1px X 1px, move the image and then read the imageData
精彩评论