Emrah Karakoc
Emrah Karakoc

Reputation: 253

Compare two screenshots doesn't work correctly-java

i am trying to make two screenshots with 6 seconds difference, to see if there is some changes on the website.

But my code says me that the screenshots are always different, even if i test it without any changing on the screen.

what am i doing wrong?

Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension screensize = toolkit.getScreenSize();
Rectangle rectangle = new Rectangle(0,0,screensize.width,screensize.height);

Robot robot = new Robot();
BufferedImage image1 = robot.createScreenCapture(rectangle); 
System.out.println("screenshot "+i+"");
Thread.sleep(6000);
BufferedImage image2 = robot.createScreenCapture(rectangle);
System.out.println("screenshot "+(i+10)+"");

int x1 = image1.getWidth();
    int x2 = image2.getWidth();
    if ( x1 != x2 ) {
        System.out.println( "Widths are different: " + x1 + " != " + x2 );
        return;
    }

    int y1 = image1.getHeight();
    int y2 = image2.getHeight();
    if ( y1 != y2 ) {
        System.out.println( "Heights are different: " + y1 + " != " + y2 );
        return;
    }

    for ( int x = 0; x < x1; x++ ) {
        for ( int y = 0; y < y1; y++ ){
            int p1 = image1.getRGB( x, y );
            int p2 = image2.getRGB( x, y );
            if ( p1 != p2 ) {
                System.out.println("Pixel is different at x/y " + x + "/" + y + ": " + p1 + " != " + p2 );
                return;
            }
        }
    }

    System.out.println( "Images are identical" );

Upvotes: 2

Views: 1057

Answers (1)

Michael Laffargue
Michael Laffargue

Reputation: 10304

I tried your code and my pixel is different because of a blinking cursor in Eclipse Console.

Then I had a problem with an animated icon (process explorer in task bar)

Finally it said Image identical.

Note : Mouse isn't part of the thing :

Creates an image containing pixels read from the screen. This image does not include the mouse cursor.

Upvotes: 5

Related Questions