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
这是例外
线程“main”中的异常java.lang.ArrayIndexOutOfBoundsException: 协调出界! at 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)
我怎么能删除这个异常
答案 0 :(得分:2)
自定义
以来,k的值应小于3而不是rgb.lengthint[][] pixelData = new int[img.getHeight() * img.getWidth()] [3];
或者如果rgb.length正确,则需要相应地更改数组大小。
答案 1 :(得分: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