2011-11-16 20 views
-1

這裏是我的代碼:不知道如何做到這一點,而循環

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; 
} 

}

我想要做的是從編曲,同時也有閱讀,提取和轉換成8位字符是什麼還有剩餘的像素在arr中讀取。出於某種原因,當我運行註釋循環時,我得到一個ArrayIndexOutOfBoundsException。我該如何解決?提前致謝!

+0

你能粘貼例外嗎? – OscarRyz

回答

2

while(counter<arr.length)循環運行counter到的arr長度,以及內循環使用j+counter訪問arr;有保證流掉的arr結束時,如果arr不是8

的整數倍並且由於arr是圖像中的比特數,這是極不可能是8

0

的整數倍改變循環條件。

while (counter <= arr.length - 8) 
相關問題