Hello all,
I need to know if a part of the screen as been refreshed.
So using the AWT.Robot to get the BufferedImage of part of the screen.
In the main() test method I fetch the same part of the screen in a BufferedImage.
If I test for equals() on the BufferedImage they are not equals.
If I test for equality between the Raster derived from these BufferedImage they are not equal (I guess I can expect so)
If I compare the pixels of these Rasters they are equals.
What do I miss ?
For performance reasons do not want to regenerate the Raster and then the pixels every n seconds.
Thanks
java
import java.awt.*;
import java.awt.image.*;
/** Used to handle a screen area an enabled comparaisons with another ScreenImageArea */
public class ScreenImageArea {
// the Robot common to all instances
private static Robot robot;
// Rectangle to monitor
private Rectangle rectangle;
// the bufferedImage obtained from the robot
private BufferedImage bi;
// the Raster of the bufferedImage
private Raster raster;
// the pixels of the area
private int[] pixel;
// build a Robot common to all instances
static {
try {
robot = new Robot();
}
catch(AWTException e) {
throw new IllegalStateException("Unable to create Robot: AWTException: " + e);
}
}
/** Constructor we specify the area to BufferedImage/Raster */
ScreenImageArea(int x, int y, int width, int height) {
// the area monitored
rectangle = new Rectangle(x, y, width, height);
// get the BufferedImage for the first time
bi = robot.createScreenCapture(rectangle);
// build the Raster for the first time
raster = bi.getData();
// build the array to get the array of trio of pixel color
pixel = new int[width*height*3];
// get the pixels for the first time
pixel = raster.getPixels(0, 0, width, height, pixel);
}
/** Method to compare the BufferedImages */
boolean equalsBi(ScreenImageArea x) {
return bi.equals(x.bi);
}
/** Method to compare the Raster */
boolean equalsRaster(ScreenImageArea x) {
return raster.equals(x.raster);
}
/** Method to compare the pixels */
boolean equalsPixels(ScreenImageArea x) {
if(pixel.length != x.pixel.length)
return false;
for(int i = 0; i < pixel.length; i++) {
if(pixel[i] != x.pixel[i])
return false;
}
return true;
}
/** Method to compare the Raster */
public static void main(String[] arg) {
// ok lets test the whole thing
// using the same area of the screen so everything should be equals
ScreenImageArea area1 = new ScreenImageArea(0, 0, 10, 10);
ScreenImageArea area2 = new ScreenImageArea(0, 0, 10, 10);
// lets compare the BufferedImage
System.out.println("Comparing BufferedImage: " + area1.equalsBi(area2));
// the raster
System.out.println("Comparing Raster: " + area1.equalsRaster(area2));
// the pixels
System.out.println("Comparing Pixels: " + area1.equalsPixels(area2));
}
}
and the output is:
Comparing BufferedImage: false
Comparing Raster: false
Comparing Pixels: true