I am trying to compare two images by checking for matching pixels in Java. This is normally no problem on my main desktop however when I port my code over to my laptop it does not work at all. My laptop is using a resolution of 2736 x 1824 however I think that is scaled up since when I take a screen shot the resulting image always has a size of 1368 x 912. Anyways I need a way to take screen shots of my desktop and compare it to a another smaller desktop image (I take them with the snipping tool). I also noticed that the bit depth of both images are different however they are also different on my desktop so I ruled that out.
Anyways as a little recap I assume snipping tool is taking the picture at 2736 x 1824 (just a section of the screen though) while robot.createScreenCapture is taking the screen shot at 1368 x 912. Is there a way I can take a screen shot at 2736 x 1824 in java so I can correctly get the pixel's RGB values?
private int xCenterPixel;
private int yCenterPixel;
private BufferedImage image;
private Robot robot;
public boolean checkImage(String path) {
try {
robot = new Robot();
} catch (AWTException awtException) {
awtException.printStackTrace();
System.exit(-1);
}
File smallImage = new File(path);
BufferedImage large = robot.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
final BufferedImage small = getInitialImage(smallImage);
final boolean containsImage = containsImage(large, small);
System.out.println("Does contain: " + containsImage);
return containsImage;
}
private BufferedImage getInitialImage(File file) {
try {
image = ImageIO.read(file);
image = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
image = ImageIO.read(file);
} catch (IOException e) {
e.printStackTrace();
}
return image;
}
private boolean containsImage(BufferedImage large, BufferedImage small) {
final int smallColor = small.getRGB(0, 0);
for(int xLarge = 0; xLarge < large.getWidth(); xLarge++) {
for(int yLarge = 0; yLarge < large.getHeight(); yLarge++) {
final int largeColor = large.getRGB(xLarge, yLarge);
if(largeColor == smallColor) {
if(checkPixels(large, small, xLarge, yLarge)) {
xCenterPixel = xLarge + (small.getWidth() / 2);
yCenterPixel = yLarge + (small.getHeight() / 2);
return true;
}
}
}
}
return false;
}
Also if I can figure out how to take a picture in 2736 x 1824 I can just divide my resulting centerX and centerY by 2 to convert to 1368 x 912 which is what the rest of my program uses. Also this is not all of my code this is just the parts leading up the issue specifically the (largeColor == smallColor) part. if anyone has any suggestions please let me know!