0

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.

Leonid Glanz
  • 1,261
  • 2
  • 16
  • 36
Shiladittya Chakraborty
  • 4,270
  • 8
  • 45
  • 94
  • what do you mean too slow ? Too slow in production mode ? I haven't looked but you can search for the fastest algorithm to do that in pure JS HTML5 Canvas and it should be probably applicable to `GWT` because it only provides a thin wrapper around the native HTML5 Canvas API – Ümit Jun 27 '15 at 08:20
  • Its working slow in development mode but after compilation its working fine. Even I also got another library for comparison image. Thanx for your comment. – Shiladittya Chakraborty Jun 30 '15 at 07:44
  • Are you using the old `DevMode` ? If so I would really recommend to switch to `SuperDevMode` because `DevMode` is going away and `SuperDevMode` speed is almost the same as production mode. – Ümit Jun 30 '15 at 07:59
  • How to switch in superdevmode? can you please explain? – Shiladittya Chakraborty Jun 30 '15 at 08:30
  • Check out the [official](http://www.gwtproject.org/articles/superdevmode.html) article. You can also search here at [stackoverflow](http://stackoverflow.com/questions/11356714/getting-started-with-the-superdevmode). – Ümit Jun 30 '15 at 08:58
  • @Ümit my canvas size 1520 *800. Right now I am using canvaspixelarray for get the color from canavas but its working slow. It's taking time 316ms in production mode. So is there any other way to do this? Because I have multiple canvas in the same size so this is the huge data. – Shiladittya Chakraborty Jul 15 '15 at 05:23

0 Answers0