Reputation: 63
For example, I am taking a screen shot of a very small portion of the desktop (pretend the coordinates cannot be saved when screen shot taken). Next, a screen shot of full screen is taken. How do I find the location of the small screen shot within the big screen shot?
Is this even possible in Java?
public void RobotScreenCoordinateFinder()
{
Robot robot = new Robot();
robot.createScreenCapture(screenRect);
}
Upvotes: 1
Views: 912
Reputation: 742
My idea is to use this:
BufferedImage screen=robot.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));c
BufferedImage capture=robot.createScreenCapture(screenRect);//assuming that screenRect is your capture
boolean screenMatches=false;
int screenX=0;
int screenY=0;
for(int i=0;i<screen.getWidth()-capture.getWidth();i++)
{
for(int j=0;j<screen.getHeight()-capture.getHeight();j++)
{
boolean matches=true;
for(int x=0;x<capture.getWidth();x++)
{
for(int y=0;y<capture.getHeight();y++)
{
if(screen.getRGB(i+x,j+y)!=capture.getRGB(x,y))
{
matches=false;
break;
}
}
if(!matches)break;
}
if(matches)
{
screenMatches=true;
screenX=i;
screenY=j;
break;
}
}
if(screenMatches)break;
}
//now if the capture is in the screen, screenMatches is true and the coordinate of the left up corner is stored in screenX and screenY
if(screenMatches)
{
System.out.println("Found match with coordinates "+screenX+","+screenY);
}
Upvotes: 1