Java 获取像素区域并转换为图像 - 如何才能更有效?

发布于 2024-12-16 19:14:45 字数 708 浏览 3 评论 0原文

我有一些代码将抓取屏幕上的像素区域并将它们转换为 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 秒,这实在是太慢了!

I have some code which will grab an area of pixels on the screen and turn them into a BufferedImage object. The thing is - it is MASSIVELY slow, so I am looking for support in increasing its speed!

The code is as follows:

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;


}

and I am passing it : getScreenPortion(new Point(1081,824),new Point(1111,844)); which means I am trying to get an areas approximately 30x20 - yet it is taking in the region of 7 seconds which is horrendously slow!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怎樣才叫好 2024-12-23 19:14:45

修复了它 - 我现在改为使用:

Rectangle screenRect = new Rectangle(topleft.x, topleft.y, width, height);
BufferedImage grid = robot.createScreenCapture(screenRect);

Fixed it - I now instead use:

Rectangle screenRect = new Rectangle(topleft.x, topleft.y, width, height);
BufferedImage grid = robot.createScreenCapture(screenRect);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文