I am working on GWT canvas which is similar to HTML 5 canvas.
My goal is to get the pixel color from the canvas.
I found one way to do this by using CanvasPixelArray
which store the image data.
Image data store the pixel information.
I am using following code to get the pixel color from canvas :
CanvasPixelArray imageData = canvas.getRendererCanvas().getContext2d().getImageData(0, 0, canvas.getWidth(), canvas.getHeight()).getData();
int length = imageData.getLength() / 4;
int index = 0, r, g, b, a;
for (int i = 0; i < length; i++) {
index = 4 * i;
r = imageData.get(index); //red
g = imageData.get(++index); //green
b = imageData.get(++index); //blue
a = imageData.get(++index); //alpha
if (r == 255 || g == 255 || b == 255) { // pixel is white
System.out.println(r+","+g+","+b+","+a);
}
}
But the major problem is that it's working too slow and down the performance. This is the main issue otherwise above code working fine.
So what is the best way performance wise to get the color information from canvas.
Any help is highly appreciated. Thank you.