【发布时间】:2014-08-31 05:47:48
【问题描述】:
我正在尝试使用这些方法在屏幕上的矩形区域中查找颜色。然而,有时屏幕上有数百万像素,而我目前每秒只能实现大约 35 次 getColor 迭代。我的代码中一定有什么东西导致它运行得非常慢。
我怎样才能比这更快地扫描我的屏幕?理想情况下,我希望在不到一秒的时间内扫描整个屏幕的颜色,而不是现在的 8 小时:P
这是我的两种方法。
public static int getColor(int x, int y){
try{
return(robot.getPixelColor(x, y).getRGB() * -1);
}catch(Exception e){
System.out.println("getColor ERROR");
return 0;
}
}
//returns first instance of color,
//Begins top left, works way down to bottom right
public static Point findColor(Box searchArea, int color){
System.out.println("Test");
if(searchArea.x1 > searchArea.x2){
int temp = searchArea.x1;
searchArea.x1 = searchArea.x2;
searchArea.x2 = temp;
}
if(searchArea.y1 > searchArea.y2){
int temp = searchArea.y1;
searchArea.y1 = searchArea.y2;
searchArea.y2 = temp;
}
for(int i = searchArea.x1;i <=searchArea.x2; i++){
for(int j = searchArea.y1;j<=searchArea.y2;j++){
if(getColor(i, j) == color){
return new Point(i, j);
}
System.out.println(i + " " + j);
}
}
return new Point(-1, -1);
}
【问题讨论】:
-
可能是因为它是递归函数?
-
呃哦......我刚刚尝试了一个 for 循环到一百万,打印每个数字,但仍然需要整整一分钟才能完成。如果我的计算机 (AMD FX-6300) 甚至无法足够快地处理空白循环,我该怎么做?
-
not 8 hours as it stands now- 真的吗? -
@ScaryWombat,我的方法永远不会在上面的代码中调用自身......是的,35 次迭代/秒 = 1 000 000 次迭代/8 小时
-
robot.getPixelColor是瓶颈。获取屏幕截图并处理生成的BufferedImage可能会更快
标签: java performance awt awtrobot