-1
public static int[] getPixelData(BufferedImage img, int x, int y)

{

int argb = img.getRGB(x, y);

int rgb[] = new int[] 

{

((argb >> 16) & 0xfe), //red

(argb >>  8) & 0xfe, //green

(argb      ) & 0xfe  //blue

};

System.out.println("rgb: " + Integer.toBinaryString(rgb[0]) + " " + Integer.toBinaryString(rgb[1]) + " " +Integer.toBinaryString(rgb[2]));





return rgb;


}

o/p

rgb: 10111000 11010010 11110110 rgb: 10111010 11011010 11111100 rgb: 10101110 11010100 11110010 rgb: 10100010 11010000 11100110 rgb: 10100000 11010100 11100000 rgb: 10011110 11010110 11010100 rgb: 10011110 11011000 11001110 rgb: 10011110 11011000 11001110

这是例外

线程“主”java.lang.ArrayIndexOutOfBoundsException 中的异常:坐标超出范围!在 sun.awt.image.ByteInterleavedRaster.getDataElements(Unknown Source) at java.awt.image.BufferedImage.getRGB(Unknown Source) at get_pixel_value.getPixelData(get_pixel_value.java:52) at get_pixel_value.get_pixel_info(get_pixel_value.java:28)在 Hash_Function2.main(Hash_Function2.java:26)

我怎么能删除这个异常

4

2 回答 2

2

k 的值应该小于 3 而不是 rgb.length 因为你定义

int[][] pixelData = new int[img.getHeight() * img.getWidth()]  [3];

或者如果 rgb.length 正确,则需要相应地更改数组大小。

于 2013-01-01T14:08:08.520 回答
0

尝试这个

img = ImageIO .read(new File(IMG));
int[][] pixelData = new int[img.getHeight() * img.getWidth()][];
for(int i = 0; i < img.getHeight(); i++) {
    for(int j = 0; j < img.getWidth(); j++) {
        pixelData[i*img.getWidth()+j] = getPixelData(img, i, j);

    }
}

...

public static int[] getPixelData(BufferedImage img, int x, int y) {
    int argb = img.getRGB(x, y);
    int rgb[] = new int[] {
        ((argb >> 16) & 0xff), //red 
        (argb >> 8) & 0xff, //green
        (argb ) & 0xff //blue
    }
    return rgb;
}

但您也可以直接从 BufferedImage 获取 rgb 数组。还有第二种 getRGB 方法。 http://docs.oracle.com/javase/7/docs/api/java/awt/image/BufferedImage.html

于 2013-01-01T14:25:32.940 回答