2013-03-28 274 views
1

例如,我正在拍摄桌面很小一部分的屏幕截图(假设屏幕截图时不能保存坐标)。接下来,拍摄全屏幕截图。如何在大屏幕截图中找到小屏幕截图的位置?在大屏幕截图中获取小屏幕截图位置的坐标

这甚至可能在Java?

public void RobotScreenCoordinateFinder() 
{ 
    Robot robot = new Robot(); 

    robot.createScreenCapture(screenRect); 
} 
+0

查找字符串搜索是如何工作的e.g克努特莫里斯普拉特和博耶 - 穆尔算法中,并且将其扩展两张2 - 尺寸。 – AlexWien

+1

检查*** [这个](http://en.wikipedia.org/wiki/Template_matching)***页 –

回答

1

我的想法是这样使用:

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); 
} 
+1

这是一个简单的蛮力搜索,但它应该工作 – AlexWien

+0

它的工作原理,我测试了它。 – D180

+0

谢谢这有助于很多! – user2220444