例如,我正在截取一小部分桌面的屏幕截图(假设截屏时无法保存坐标)。接下来,拍摄全屏屏幕截图。如何在大屏幕截图中找到小屏幕截图的位置?
这在Java中甚至可能吗?
public void RobotScreenCoordinateFinder()
{
Robot robot = new Robot();
robot.createScreenCapture(screenRect);
}
例如,我正在截取一小部分桌面的屏幕截图(假设截屏时无法保存坐标)。接下来,拍摄全屏屏幕截图。如何在大屏幕截图中找到小屏幕截图的位置?
这在Java中甚至可能吗?
public void RobotScreenCoordinateFinder()
{
Robot robot = new Robot();
robot.createScreenCapture(screenRect);
}
我的想法是使用这个:
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);
}