【发布时间】:2018-05-16 00:27:42
【问题描述】:
我正在尝试通过此方法将此 PNG 图像从 BufferedImage 转换为双精度数组。
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;
}
即使我将数组定义为图像高度和宽度的确切大小,当它几乎完成循环时,我也会得到边界错误。我不知道为什么。
“坐标超出范围! 在 sun.awt.image.ByteInterleavedRaster.getDataElements(未知来源) 在 java.awt.image.BufferedImage.getRGB(Unknown Source)"
【问题讨论】:
-
循环变量分别使用
y和x而不是i和j。然后阅读documentation for getRGB。问题应该很清楚了。
标签: java indexoutofboundsexception bufferedimage