Thursday 2 January 2014

Fast screen capture in Java - Example

 Read pixels from screen in Java


If you want to fast read pixels from screen you will need Java class called Robot.
The main purpose of class Robot is to generate native system inputs (mouse, keyboard inputs) for the purpose of test automation. 

If you need to read pixels from screen you can use Robot method called getPixelColor(int x, int y) x and y represents coordinates of pixels you want to grab. Method getPixelColor(...) returns object Color so if you need to now RGB you must use color.getBlue(), color.getGreen(), color.getRed();

This method is good if you want to read 2-3 pixels but if you need to reed a 10x10 pixels from screen i don't recommend using it. Main reason is because getPixelColor is slow, first you read pixel that you create object Color and then you can work with that pixel.

If you want to read 10x10 pixels from screen or more you can use Robot's method called createScreenCapture(Rectangle r); that method returns BufferedImage. First you may think why is he taking Rectangle and returns a image? Method createScreenCapture(...) need to know from which coordinates he must start (x,y) and how much weight and height go right and down.

So you get something like this:
Robot robot = new Robot();
BufferedImage image = robot.createScreenCapture(new Rectangle(100, 100, 10, 10));

So you can easy capture 10x10 pixels from screen in one line of code, if you want to use getPixelColor in a loop to do that you will need 2 or 3 second for that. (that's a lot!!!)

When you have BufferedImage you can easy save it like "bmp":
ImageIO.write(image, "bmp", new File("Image.bmp"));

If you want to work with pixels you get in BufferedImage you can do it like this:
boolean fail = false;
for (int x = 0; !fail  && x < image.getHeight();  x++) {
                for (int y = 0; !fail  &&  y <
image.getWidth(); y++) {
                    if ( image.getRGB(x, y) != color.getRGB() ) {
                        System.out.println("Pixels are different stop!");
                        fail = true;
                        }
                    }
           }

}
color.getRGB() returns just one color if you need to check are two pictures the same you can use:
if ( image.getRGB(x, y) != image2.getRGB(x, y) 
So if pixels on x, y position on image and image2 are different we stop and write message for that, if we don't get that message fail will stay on false and we can after all this add this:
if (!fail) {
  System.out.println("Pictures are equal!");
}

Robot is powerful class, you can click mouse, keyboard or read pixels from screen.
If you want to learn more about Robot class you can read Java documentation on this link.

If you have any questions or need help with coding post a comment bellow.
And if you like this post please share it. :)

1 comment: