在屏幕上快速采集图像并读取像素

时间:2011-07-19 18:19:14

标签: java bufferedimage screen-capture getpixel awtrobot

我正在尝试在屏幕上获取一小部分图像并读取任何像素以比较其他像素。获取屏幕图像的代码是:

Rectangle captureSize = new Rectangle(x, y, height, width);
BufferedImage image = robot.createScreenCapture(captureSize);

并且,为了逐像素地读取我使用

  for (int y = 0; y < image.getHeight(); y = y + 1) {
        for (int x = 0; x < image.getWidth(); x = x + 1) {
            color = image.getRGB(x, y);
            // Some methods etc
        {
{

然而,当我跑的时候,我很震惊。由于createScreenCapture大约需要<40秒,并且每个像素使用getRGB大约需要 350 ms ,这对于创建60 fps的应用程序效率非常低。顺便说一句,我的图像 800x400 像素大小。我没试过

rgbArray = image.getRGB(startX, startY, w, h, rgbArray,offset, scansize) ;

方法,因为我不知道它的效率如何,重新排序我的代码会有点困难。所以,任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

使用

rgbArray = image.getRGB(startX, startY, w, h, rgbArray,offset, scansize) ;

从数组中读取像素值要比获取每个像素值的方法调用要快得多,并且对getRGB的单次调用获取数组的速度并不慢。