2

i am getting an ArrayIndexoutOfBoundsException while trying to read pixels from image . the code returns the pixel values for some images and not for other images..i tried searching the net, and most of them referred to reading from 0 to n...

The code is given below..any help would be truly appreciated..

i tried writing saving the image in bi,and it gets saved..so bi is never getting null value.. and my image dimension is always 125*150.. i tried printing the values in inputFace,but in those images that doesn't give pixel values,i am not even getting any output while printing... Doesn't array get initialized with 0 as soon as memory is allocated??

And thanks in advance

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;

    }
4

1 回答 1

1

您没有考虑每个像素的波段数 - 每个像素由多个波段(通道,例如 TYPE_INT_ARGB 图像的红色、绿色、蓝色、Alpha 通道)组成,具体取决于图像类型。您分配的数组大小必须为(像素宽度 * 像素高度 * 波段数):

int numBands = bi.getData().getNumBands();
inputFace = new double[imageWidth * imageHeight * numBands];

这将为您提供一个数组,其中包含每个像素的每个通道的所有值。

于 2013-09-13T18:31:44.533 回答