我在尝试从图像中读取像素时遇到ArrayIndexoutOfBoundsException。代码返回一些图像的像素值,而不是其他图像。我尝试搜索网络,其中大部分都是从0到n读取......
下面给出了代码。真正感谢任何帮助..
我尝试用bi保存图像,然后保存。所以bi永远不会得到空值。 我的图像尺寸总是125 * 150 .. 我尝试在inputFace中打印值,但在那些不提供像素值的图像中,我甚至没有在打印时获得任何输出... 一旦分配了内存,数组是否会初始化为0?
并提前致谢
private double[] getImageData(String imageFileName) {
BufferedImage bi = null;
double[] inputFace = null;
try{
bi = ImageIO.read(new File(imageFileName));
}catch(IOException ioe){
ioe.printStackTrace();
}
if (bi != null){
int imageWidth = bi.getWidth();
int imageHeight = bi.getHeight();
inputFace = new double[imageWidth * imageHeight];
bi.getData().getPixels(0, 0, imageWidth, imageHeight,inputFace);
}
else
{
System.out.println("Null in bi");
}
return inputFace;
}
答案 0 :(得分:1)
您没有考虑每个像素的波段数 - 每个像素由多个波段(通道,例如红色,绿色,蓝色,Alpha,用于TYPE_INT_ARGB的图像)组成,具体取决于图像类型。您要分配的数组大小必须是(像素宽度*像素高度*带数):
int numBands = bi.getData().getNumBands();
inputFace = new double[imageWidth * imageHeight * numBands];
这将为您提供一个数组,其中包含每个像素的每个通道的所有值。