我正在尝试制作两张相差 6 秒的屏幕截图,看看网站上是否有一些变化。
但是我的代码告诉我,屏幕截图总是不同的,即使我在屏幕上没有任何变化的情况下测试它。
我究竟做错了什么?
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" );