Here is my code:
public static void decodeThis(BufferedImage im, String outputFile)
throws FileNotFoundException{
int w = im.getWidth();
int h=im.getHeight();
int[] arr=im.getRGB(0, 0, w, h, null, 0, w);
int[] eightBit=new int[8];
int counter=0;
String[] aString=new String[arr.length];
PrintStream out=new PrintStream(new File(outputFile));
double value=0;
int intVal=0;
while (counter<arr.length){
for (int j=0;j<eightBit.length;j++){
eightBit[j] = arr[j+counter] & 1; //Extracts least significant bit, puts into eightBit array.
}
value=0;
for (int x=0;x<eightBit.length;x++){
value+=eightBit[x]*(Math.pow(2,x));
}
intVal=(int)value;
out.print((char)intVal);
counter=counter+8;
}
}
What I want to do is read, extract, and convert into a character 8 bits from arr while there are still pixels left to read in arr. For some reason when I run the commented for loop, I get an ArrayIndexOutOfBoundsException. How do I fix this? Thanks in advance!
The while(counter<arr.length)
loop runs counter
to the length of arr
, and the inner loop accesses arr
using j+counter
; that's guaranteed to run off the end of arr
if arr
is not an exact multiple of 8.
And since arr
is the number of bits in the image, it's highly unlikely to be an exact multiple of 8.
更改循环条件。
while (counter <= arr.length - 8)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.