2013-03-28 205 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