2012-03-31 39 views
0

我試圖在32 x 32黑白圖像(位圖或PNG)中存儲一個32 x 32布爾數組,然後將其映射到布爾[32] [32]數組,黑色像素爲true,白色是假的。如何將圖像轉換爲Java(Android)中的布爾數組?

這是存儲動畫幀以顯示在虛擬32 x 32顯示器上。以下是我在下面的內容。

Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), R.raw.f1); 
bmp.compress(Bitmap.CompressFormat.PNG, 100, o_stream); 
byte[] byteArray = o_stream.toByteArray(); 

什麼我的ByteArray做,使之布爾[32] [32]數組或我要對所有這一切錯擺在首位?

+0

你爲什麼不只是使用字節數組32×32,代表的最低字節範圍爲黑色的一個字節值和最大的最大字節範圍內的字節值白色?但是,這取決於圖像使用的類型和顏色模型。因此,由於此映像實現,最終可能不是32x32字節的數組輸入。 – ecle 2012-03-31 13:20:22

回答

0

雖然我從來沒有對圖像做任何事情(所以我不知道這是否與接近圖像的黑白應該做的事情接近),但我想你需要一個規則來決定是否像素更接近黑色或接近白色。但我很好奇,一個字節怎麼能代表一種顏色?即使它是RGB,你至少需要三個字節,不是嗎?

+0

我同意你的看法,因爲每個圖像格式都有不同的顏色模型策略,可能需要多個字節來表示顏色。在包含alpha通道的RGBA的情況下,它將變成四個字節來表示RGBA顏色。 – ecle 2012-03-31 13:24:30

+0

具體使用黑色和白色1位32 x 32位圖或png,我正在生成的位圖。 – Dennis 2012-03-31 13:48:10

+0

我是否正確理解你,圖片已經是黑色和白色?在這種情況下,轉換應該非常簡單,只需循環byte [] []並逐字節地檢查它是否等於1或0(或任何黑白表示)並指定布爾值[] [ ]爲基礎的真或假。 – 2012-03-31 13:50:00

0
if(src!= null){ 
ByteArrayOutputStream os=new ByteArrayOutputStream(); 
src.compress(android.graphics.Bitmap.CompressFormat.PNG, 100,(OutputStream) os); 
byte[] byteArray = os.toByteArray(); 
//Log.d("byte=",""+byteArray.length);//returns length. 
//str = Base64.encodeToString(byteArray,Base64.DEFAULT);//returns string 
} 

where src is the bitmap.... 
0

如果您只是想將一個布爾數組編碼爲位圖以節省存儲空間,爲什麼要使用圖像?這是很多額外的開銷。爲什麼不只是自己創建的位圖,如下所示:

Boolean[][] booleanArray = ... // this is your Boolean array 
    int[] bits = new int[32]; // One int holds 32 bits 
    for (int i = 0; i < 32; i++) { 
     for (int j = 0; j < 32; j++) { 
      if (booleanArray[i][j]) { 
       // Set bit at the corresponding position in the bits array 
       bits[i] |= 1 << j; 
      } 
     } 
    } 
    // Now you have the data in a int array which you can write to a file 
    // using DataOutputStream. The file would contain 128 bytes. 

    // To recreate the Boolean[32][32] from the int array, do this: 
    Boolean[][] booleanArray = new Boolean[32][32]; 
    int[] bits = ... // This is the data you read from the file using DataInputStream 
    for (int i = 0; i < 32; i++) { 
     for (int j = 0; j < 32; j++) { 
      if ((bits[i] & (1 << j)) != 0) { 
       // Set corresponding Boolean 
       booleanArray[i][j] = true; 
      } 
     } 
    } 
相關問題