Welcome to Dream.In.Code
Become a Java Expert!

Join 150,417 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,059 people online right now. Registration is fast and FREE... Join Now!




Problem with Robot

 
Reply to this topicStart new topic

Problem with Robot, equals methods on BufferedImage and Raster

pbl
3 Apr, 2008 - 02:46 PM
Post #1

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
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


User is offlineProfile CardPM
+Quote Post

pbl
RE: Problem With Robot
3 Apr, 2008 - 05:57 PM
Post #2

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
Did so more investigations in the Java source code

BufferedImage.equals()
and
Raster.equals()

end up extending java.lang.Object.equals() and none of the itermediate classes

java.lang.Object.equals() is basically

CODE

    public boolean equals(Object obj) {
    return (this == obj);


which will never be true
so I guess I will have to pass thrue the pixels decoding... not very efficient
any better idea ?

User is offlineProfile CardPM
+Quote Post

1lacca
RE: Problem With Robot
4 Apr, 2008 - 12:28 AM
Post #3

code.rascal
Group Icon

Joined: 11 Aug, 2005
Posts: 3,822



Thanked: 12 times
My Contributions
Depending on the size of the area you could try int[] BufferedImage.getRGB ( you can skip the Raster creation and comparing arrays are quite fast) or Robot.getPixelColor if the area in question is small.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 08:15PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month