簡體   English   中英

不確定如何在while循環中執行此操作

[英]Not sure how to do this while loop

這是我的代碼:

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

}

我想做的是從arr中讀取,提取並轉換為8位字符,同時在arr中還有要讀取的像素。 由於某種原因,當我運行帶注釋的for循環時,我得到了ArrayIndexOutOfBoundsException。 我該如何解決? 提前致謝!

while(counter<arr.length)循環與arr的長度counter ,而內部循環使用j+counter訪問arr 如果arr不是8的整數倍,則可以保證在arr的末尾運行。

而且由於arr是圖片中的位數,因此極不可能是8的精確倍數。

更改循環條件。

while (counter <= arr.length - 8)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM