2012-03-31 64 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; 
      } 
     } 
    }