Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/341.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 BuffereImage.getRGB中的坐标超出边界_Java_Indexoutofboundsexception_Bufferedimage - Fatal编程技术网

Java BuffereImage.getRGB中的坐标超出边界

Java BuffereImage.getRGB中的坐标超出边界,java,indexoutofboundsexception,bufferedimage,Java,Indexoutofboundsexception,Bufferedimage,我正试图通过这种方法将这个PNG图像从BuffereImage转换为双数组 public double[][] bufferedToArray(File pngImage) { double[][] imageMatrix= null; try { final BufferedImage image = ImageIO.read(pngImage); int height= image.getHeight(); in

我正试图通过这种方法将这个PNG图像从BuffereImage转换为双数组

public double[][] bufferedToArray(File pngImage)
{       
    double[][] imageMatrix= null;
    try {

        final BufferedImage image = ImageIO.read(pngImage);
        int height= image.getHeight();
        int width= image.getWidth();
        imageMatrix= new double[height][width];

        System.out.println("Matriz Máximo i: " + imageMatrix.length +
                "Matriz Máximo j: " + imageMatrix[0].length );

        for(int i=0;i<height;i++){
            for(int j=0;j<width;j++){

                //System.out.println("i atual: "+i+" j atual: "+j);
                imageMatrix[i][j] = image.getRGB(i, j); //The error is in this line.
                //System.out.println("matrizImagem["+i+"]["+j+"] Inserido");

            }           
        }                       

    } catch (IOException e) {       
        e.printStackTrace();
    }
    return imageMatrix; 
}
public double[][]bufferedToArray(文件pngImage)
{       
double[]imageMatrix=null;
试一试{
最终缓冲区图像=图像IO.read(pngImage);
int height=image.getHeight();
int width=image.getWidth();
imageMatrix=新的双[高][宽];
System.out.println(“Matriz Máximo i:”+imageMatrix.length+
Matriz Máximo j:“+imageMatrix[0].长度);

for(int i=0;i我认为错误在于i和j变量变得大于高度和宽度。请尝试将for循环的条件更改为:


I你的代码中的问题很简单,你混淆了
I
j
的意思,以及
getRGB(x,y)
期望的参数。你的代码(意外地)只适用于方形图像

更改行:

imageMatrix[i][j] = image.getRGB(i, j); //The error is in this line.
致:

这将解决问题


然而,(正如VGR在他的评论中指出的那样),使用更有意义的变量名是一种很好的做法。因为
j
是你的X坐标(在宽度上迭代),而
i
是你的Y坐标(在高度上迭代),可能最好这样命名,
x
y
。这将使错误更容易发现,尤其是在查阅文档时。

而不是
i
j
,分别使用
y
x
作为循环变量。然后阅读。问题应该会变得清楚@JamesB它“克服<实例”是什么意思?它在已经超出范围的循环迭代中添加了另一个循环迭代。
imageMatrix[i][j] = image.getRGB(j, i);