【发布时间】:2012-02-29 06:57:16
【问题描述】:
好吧,我整天都在为此绞尽脑汁。试图弄清楚如何在 Android 中将位图从画布转换为 1bpp(每像素位)位图文件并以物理方式保存它。
到目前为止,我已经遍历了位图并创建了一个 int[] 的结果像素值作为 1 或 0。但是,我的下一个问题是我该怎么办?
我尝试做的是类似
int[] bits = // populated earlier
byte[] bmp = new byte[bits.length / 8];
int byteindex = 0;
int bitindex = 0;
for (int i=0; i<bits.length; i++) {
if (bits[i] == 1)
// set to 1
else
// set to 0
if (bitindex++ == 8) {
bitindex = 0;
byteindex++;
}
}
OutputStream out = new FileOutputStream("/mnt/sdcard/dynbmp.bmp");
out.write(bmp);
out.close();
我从中得到了一个文件,但它显然不是一个有效的 bmp 文件。谁知道那是什么。您必须原谅我缺乏位字节和成像知识,但我在哪里搞砸了?我的想法完全错误吗?我错过了一些标题信息吗?
【问题讨论】:
-
你已经覆盖了 Android 方面。现在,阅读 BMP 文件结构,并为您的数据重现它。这不仅仅是文件中的一堆字节。 en.wikipedia.org/wiki/BMP_file_format