Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java getSubImage()触发RasterFormatException_Java_Image - Fatal编程技术网

Java getSubImage()触发RasterFormatException

Java getSubImage()触发RasterFormatException,java,image,Java,Image,这个程序应该拍摄一个屏幕截图(干草堆),然后在屏幕截图中寻找一个子图像(针)。然后输出针的位置。它使用for循环遍历每个维度。它从左->右和顶部->底部开始。在第36行和第37行中,从getSubImage()方法中抛出RasterFormatException 您总是获得宽度为haystack.getWidth()-pinder.getWidth()和高度为haystack.getHeight()-pinder.getHeight()的子图像,但您循环了整个haystack图像的宽度和高度。

这个程序应该拍摄一个屏幕截图(干草堆),然后在屏幕截图中寻找一个子图像(针)。然后输出针的位置。它使用for循环遍历每个维度。它从左->右和顶部->底部开始。在第36行和第37行中,从getSubImage()方法中抛出RasterFormatException


您总是获得宽度为haystack.getWidth()-pinder.getWidth()和高度为haystack.getHeight()-pinder.getHeight()的子图像
,但您循环了整个haystack图像的宽度和高度。

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

在for循环期间,前10次将起作用,因为10+90=100。但是第11次,你遇到了一个问题:11+90=101。
由于101比haystack图像大,因此它尝试创建一个子图像,该子图像不在haystack图像的范围内

修复方法:

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;
}
公共类测试
{
publicstaticvoidmain(字符串[]args)抛出HeadlessException、AWTException、IOException、ClassNotFoundException
{
BuffereImage haystack=new Robot()。创建屏幕捕获(新矩形(
getDefaultToolkit().getScreenSize());
点p=findNeedle(ImageIO.read(新文件(“needle.png”)),haystack;
System.out.println(p.getX()+“,”+p.getY());
}
静态点findNeedle(BuffereImage针、BuffereImage干草堆)抛出无头异常、AWTException
{

对于(int i=0;i“在光栅之外”不要解释什么?堆栈跟踪显示
test.test.findspectors()
,但我看不到在代码中调用它(或方法).@JonLin谢谢。我已经修好了。@davidbuzatto我现在明白了。中心仍然在干草堆中,而两侧没有。你真的想比较两幅图像吗?你还需要将I和j设置为针的高度和宽度。这样针的左侧和顶部就不会脱落。@Marv看看这个s图片:。在那里我画了3个代码的搜索区域(你的、我的和我的,以及你的建议)。绿色区域是有效的搜索区域,红色区域是您将获得异常的区域,蓝色区域是从未被搜索过的区域。谢谢。我现在明白了。我以前的印象是,I和j代表子图像的中心。但是,即使进行了更改,代码仍会输出相同的错误。@Marv好的,我应该有现在修复了。(至少我使用的一些虚拟图像可以使用)。要检查这两个图像是否相同,您需要检查所有像素的颜色是否相同,例如。
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)"
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;
}