Android bitmap转BMP格式图片实现

2 下载量 45 浏览量 更新于2024-09-01 收藏 83KB PDF 举报
"在Android应用开发中,将Bitmap对象转换为BMP格式的图片文件是一项常见的需求。然而,Android SDK并没有直接提供支持BMP格式的`compress()`方法。本示例代码将展示如何通过自定义实现来将Bitmap保存为BMP格式的文件。" 在Android开发中,处理图像时可能会遇到需要将Bitmap对象存储为不同格式的需求,例如JPEG、PNG或BMP。JPEG和PNG格式因为Android SDK提供了内置的支持,因此可以直接使用`Bitmap.compress()`方法轻松完成。但当需要保存为BMP(位图)格式时,由于SDK中并未直接提供此功能,开发者需要自行实现写入BMP文件的逻辑。 以下是一个简单的示例,展示了如何在Android应用中将Bitmap转换为BMP文件: ```java package com.test.bitmap; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.Bitmap.CompressFormat; import android.os.Bundle; import android.os.Environment; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; public class MainActivity extends Activity { ImageView img; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button btn = (Button) findViewById(R.id.sd); img = (ImageView) findViewById(R.id.img1); btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 获取Bitmap Bitmap bitmap = getBitmapFromImageView(img); // 保存为BMP文件 saveBitmapAsBMP(bitmap, "myBMP.bmp"); } }); } // 从ImageView获取Bitmap private Bitmap getBitmapFromImageView(ImageView imageView) { // 实现逻辑,这里省略 } // 将Bitmap保存为BMP文件 private void saveBitmapAsBMP(Bitmap bitmap, String fileName) { try { FileOutputStream out = new FileOutputStream( Environment.getExternalStorageDirectory().getPath() + File.separator + fileName); writeBitmapToBMP(bitmap, out); out.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } // 写Bitmap到BMP文件 private void writeBitmapToBMP(Bitmap bitmap, FileOutputStream out) throws IOException { int width = bitmap.getWidth(); int height = bitmap.getHeight(); // BMP文件头信息 byte[] header = new byte[54]; // 文件类型标识 header[0] = 'B'; header[1] = 'M'; // 文件大小(包括文件头) long fileSize = 54 + (width * height * 3); // RGB三通道 header[2] = (byte) (fileSize & 0xFF); header[3] = (byte) ((fileSize >> 8) & 0xFF); header[4] = (byte) ((fileSize >> 16) & 0xFF); header[5] = (byte) ((fileSize >> 24) & 0xFF); // 保留字段 header[6] = 0; header[7] = 0; // 数据偏移量,从文件头到数据区的字节数 header[10] = (byte) (54 & 0xFF); header[11] = (byte) ((54 >> 8) & 0xFF); header[12] = (byte) ((54 >> 16) & 0xFF); header[13] = (byte) ((54 >> 24) & 0xFF); // 图像信息头大小 header[14] = (byte) (40 & 0xFF); header[15] = (byte) ((40 >> 8) & 0xFF); // 图像宽度 header[18] = (byte) (width & 0xFF); header[19] = (byte) ((width >> 8) & 0xFF); header[20] = (byte) ((width >> 16) & 0xFF); header[21] = (byte) ((width >> 24) & 0xFF); // 图像高度 header[22] = (byte) (height & 0xFF); header[23] = (byte) ((height >> 8) & 0xFF); header[24] = (byte) ((height >> 16) & 0xFF); header[25] = (byte) ((height >> 24) & 0xFF); // 颜色平面数量,始终为1 header[26] = 1; // 每像素位数,BMP默认为24位 header[28] = 24; // 压缩方式,0表示无压缩 header[30] = 0; // 图像大小,不压缩时等于宽度*高度*位深度/8 long imageSize = width * height * 3; header[34] = (byte) (imageSize & 0xFF); header[35] = (byte) ((imageSize >> 8) & 0xFF); header[36] = (byte) ((imageSize >> 16) & 0xFF); header[37] = (byte) ((imageSize >> 24) & 0xFF); // X像素每米 header[38] = (byte) (2835 & 0xFF); // 72 dpi header[39] = (byte) ((2835 >> 8) & 0xFF); // Y像素每米 header[40] = (byte) (2835 & 0xFF); header[41] = (byte) ((2835 >> 8) & 0xFF); // 颜色表大小,24位无需颜色表 header[42] = 0; header[43] = 0; // 重要颜色数,24位颜色时为0 header[44] = 0; header[45] = 0; out.write(header); // 写像素数据 for (int y = height - 1; y >= 0; y--) { for (int x = 0; x < width; x++) { int color = bitmap.getPixel(x, y); out.write((color & 0xFF)); // B out.write(((color >> 8) & 0xFF)); // G out.write(((color >> 16) & 0xFF)); // R } } } } ``` 这段代码首先创建一个54字节的BMP文件头,并填充必要的信息,如文件大小、数据偏移量、图像尺寸、位深度等。然后,它按照BMP格式的顺序(从下往上,从左往右)写入像素数据。注意,BMP格式的像素数据是按BGR顺序排列的,而不是Android中的ARGB。 在实际应用中,可能需要根据具体需求进行调整,比如处理Alpha通道、添加色彩表或者处理其他BMP版本的细节。但这个基础示例可以作为一个起点,帮助理解如何手动将Bitmap转换为BMP格式。