我有一些代码可以抓取屏幕上的像素区域并将它们转换为 BufferedImage 对象。问题是 - 它非常慢,所以我正在寻求支持以提高其速度!
代码如下:
public BufferedImage getScreenPortion(Point topleft,Point bottomright){
int width = bottomright.x - topleft.x;
int height = bottomright.y - topleft.y;
BufferedImage bi = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
for(int p=0;p<height;p++){
for(int i= 0;i<width;i++){
Color pixel = robot.getPixelColor(topleft.x+i, topleft.y+p);
bi.setRGB(i, p, pixel.getRGB());
}
}
return bi;
}
我正在传递它:getScreenPortion(new Point(1081,824),new Point(1111,844));
这意味着我正在尝试获得大约30x20的区域 - 但它是耗时约 7 秒,速度慢得可怕!
最佳答案
修复了它 - 我现在改用:
Rectangle screenRect = new Rectangle(topleft.x, topleft.y, width, height);
BufferedImage grid = robot.createScreenCapture(screenRect);
关于Java 获取像素区域并转换为图像 - 如何才能更有效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8175392/