2

I'm trying to get a small section of image on the screen and read any pixel to compare the other pixels.The code to get screen image is:

Rectangle captureSize = new Rectangle(x, y, height, width);
BufferedImage image = robot.createScreenCapture(captureSize);

And, to read pixel by pixel I used

  for (int y = 0; y < image.getHeight(); y = y + 1) {
        for (int x = 0; x < image.getWidth(); x = x + 1) {
            color = image.getRGB(x, y);
            // Some methods etc
        {
{

However, when I ran it I was shocked. Because createScreenCapture took about 40 ms and using getRGB of each pixel took around 350 ms which is very inefficient to create an application for 60 fps. By the way, my image is 800x400 pixels size. I didn't try

rgbArray = image.getRGB(startX, startY, w, h, rgbArray,offset, scansize) ;

method because I don't know how efficient it is and to reorder my code would be a bit difficult. So, any help would be appreciated.

Jason Plank
  • 2,336
  • 5
  • 31
  • 40
cihad turhan
  • 21
  • 1
  • 4
  • This might be alate, but check my answer here: http://stackoverflow.com/a/9470843/408286 – mota Feb 28 '12 at 17:30

1 Answers1

1

Use

rgbArray = image.getRGB(startX, startY, w, h, rgbArray,offset, scansize) ;

It will be much faster to read the pixel values from the array than to do the method call to get each pixel value, and the single call to getRGB to fetch the array is not slow.

antlersoft
  • 14,636
  • 4
  • 35
  • 55
  • I think that if I use `rgbArray = image.getRGB(startX, startY, w, h, rgbArray,offset, scansize);` method it will transform all image data to an array (1) and I read it from an array (2) which seems more extensive work because it does two works. Therefore, I'm confused if it's fast. **Edit:** I looked how it works and realized it uses colorModel and raster. Therefore it seems faster : `colorModel.getRGB(raster.getDataElements(x,y,data));` – cihad turhan Jul 19 '11 at 18:55
  • I tried `image.getRGB(startX, startY, w, h, rgbArray,offset, scansize);` method but it also finishes it's work in **370 ms**. Isn't there another way to search pixels? – cihad turhan Jul 19 '11 at 23:15
  • A single call to image.getRGB() takes 370 ms? What platform are you on? – antlersoft Jul 20 '11 at 19:43