getSubImage()触发RasterFormatException

时间:2012-07-23 21:18:19

标签: java image

该程序应该截取屏幕截图(haystack),然后在屏幕截图中查找子图像(针)。然后它输出针的位置。它使用for循环遍历每个维度。它从左 - >右和顶 - >底。在第36行和第37行中,从getSubImage()方法中抛出了RasterFormatException。

import java.awt.AWTException;
import java.awt.HeadlessException;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Test {
    public static void main(String[] args) throws HeadlessException, AWTException,   IOException, ClassNotFoundException {

        BufferedImage haystack = new Robot().createScreenCapture(new Rectangle(
                Toolkit.getDefaultToolkit().getScreenSize()));

        Point p = findNeedle (ImageIO.read(new File ("needle.png")));

        System.out.println(p.getX() + ", " + p.getY());
    }

    static Point findNeedle(BufferedImage needle) throws HeadlessException, AWTException {
        BufferedImage haystack = new Robot().createScreenCapture(new Rectangle(
                Toolkit.getDefaultToolkit().getScreenSize()));

        for (int i = 0; i < haystack.getHeight(); i++) {
            for (int j = 0; j < haystack.getWidth(); j++) {


                if (haystack.getSubimage(j, i, haystack.getWidth() - needle.getWidth(),
                        haystack.getHeight() - needle.getHeight()) == needle) {
                    return new Point(j, i);
                }
            }
        }
        return null;
    }
}

这是错误。

Exception in thread "main" java.awt.image.RasterFormatException: (x + width) is outside raster
    at sun.awt.image.IntegerInterleavedRaster.createWritableChild(IntegerInterleavedRaster.java:467)
    at java.awt.image.BufferedImage.getSubimage(BufferedImage.java:1173)
    at test.Test.findNeedle(Test.java:36)
    at test.Test.main(Test.java:20)"

1 个答案:

答案 0 :(得分:0)

你总是得到subImage,宽度为 haystack.getWidth() - needle.getWidth(),高度为 haystack.getHeight() - needle.getHeight()
但你循环遍历干草堆图像的整个宽度和高度。

说干草堆是100x100像素,针是10x10像素。因此子图像将是90x90像素。

在for循环期间,前10次将起作用,因为10 + 90 = 100.但是第11次,你得到一个问题:11 + 90 = 101.
由于101大于haystack图像,因此它会尝试创建一个超出haystack图像范围的subImage。

修正:

public class Test
{

public static void main(String[] args) throws HeadlessException, AWTException, IOException, ClassNotFoundException
{

    BufferedImage haystack = new Robot().createScreenCapture(new Rectangle(
            Toolkit.getDefaultToolkit().getScreenSize()));

    Point p = findNeedle(ImageIO.read(new File("needle.png")), haystack);

    System.out.println(p.getX() + ", " + p.getY());
}

static Point findNeedle(BufferedImage needle, BufferedImage haystack) throws HeadlessException, AWTException
{
    for (int i = 0; i <= haystack.getWidth() - needle.getWidth(); i++)
    {
        for (int j = 0; j <= haystack.getHeight() - needle.getHeight(); j++)
        {
            if (haystack.getSubimage(i, j, needle.getWidth(), needle.getHeight()) == needle)
            {
                return new Point(j, i);
            }
        }
    }
    return null;
}

}

错误(我没有第一次发现)是,子图像的两个其他图像的大小不同,而它应该只是针的大小。 请注意,使用needle == subImage不起作用 虽然图像的内容可能相同,但两个图像对象并不相等。