Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 將 Bitmap 保存為 BMP 圖片的完整實現

編輯:關於 Android 程式設計

近期項目需要實現圖片另存為功能,要求支持 jpg、png、bmp 三種格式。jpg 和 png 相對簡單,直接調用 Bitmap.compress() 方法即可搞定;但 BMP 格式 Android 並未給出良好支持,我在網上折騰了好幾天也沒找到滿意的答案,最終通過分析 BMP 文件結構才解決了問題。

 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.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) { // TODO Auto-generated method stub View view = v.getRootView(); view.setDrawingCacheEnabled(true); view.buildDrawingCache(); Bitmap bitmap = view.getDrawingCache(); if (bitmap != null) { // ByteArrayOutputStream bos = new ByteArrayOutputStream(); // bitmap.compress(CompressFormat.PNG, 90, bos); 只能轉成PNG、JPEG // byte[] data = bos.toByteArray(); // img.setImageBitmap(BitmapFactory.decodeByteArray(data, 0, // data.length)); int w = bitmap.getWidth(), h = bitmap.getHeight(); int[] pixels=new int[w*h]; bitmap.getPixels(pixels, 0, w, 0, 0, w, h); // ByteBuffer dst = ByteBuffer.allocate(bitmap.getRowBytes() // * h); // bitmap.copyPixelsToBuffer(dst); // IntBuffer dst=IntBuffer.allocate(w*h); // bitmap.copyPixelsToBuffer(dst); byte[] rgb = addBMP_RGB_888(pixels,w,h); byte[] header = addBMPImageHeader(rgb.length); byte[] infos = addBMPImageInfosHeader(w, h); byte[] buffer = new byte[54 + rgb.length]; System.arraycopy(header, 0, buffer, 0, header.length); System.arraycopy(infos, 0, buffer, 14, infos.length); System.arraycopy(rgb, 0, buffer, 54, rgb.length); try { FileOutputStream fos = new FileOutputStream(Environment .getExternalStorageDirectory().getPath() + "/hello.bmp"); fos.write(buffer); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }); } //BMP文件頭 private byte[] addBMPImageHeader(int size) { byte[] buffer = new byte[14]; buffer[0] = 0x42; buffer[1] = 0x4D; buffer[2] = (byte) (size >> 0); buffer[3] = (byte) (size >> 8); buffer[4] = (byte) (size >> 16); buffer[5] = (byte) (size >> 24); buffer[6] = 0x00; buffer[7] = 0x00; buffer[8] = 0x00; buffer[9] = 0x00; buffer[10] = 0x36; buffer[11] = 0x00; buffer[12] = 0x00; buffer[13] = 0x00; return buffer; } //BMP文件信息頭 private byte[] addBMPImageInfosHeader(int w, int h) { byte[] buffer = new byte[40]; buffer[0] = 0x28; buffer[1] = 0x00; buffer[2] = 0x00; buffer[3] = 0x00; buffer[4] = (byte) (w >> 0); buffer[5] = (byte) (w >> 8); buffer[6] = (byte) (w >> 16); buffer[7] = (byte) (w >> 24); buffer[8] = (byte) (h >> 0); buffer[9] = (byte) (h >> 8); buffer[10] = (byte) (h >> 16); buffer[11] = (byte) (h >> 24); buffer[12] = 0x01; buffer[13] = 0x00; buffer[14] = 0x18; buffer[15] = 0x00; buffer[16] = 0x00; buffer[17] = 0x00; buffer[18] = 0x00; buffer[19] = 0x00; buffer[20] = 0x00; buffer[21] = 0x00; buffer[22] = 0x00; buffer[23] = 0x00; buffer[24] = (byte) 0xE0; buffer[25] = 0x01; buffer[26] = 0x00; buffer[27] = 0x00; buffer[28] = 0x02; buffer[29] = 0x03; buffer[30] = 0x00; buffer[31] = 0x00; buffer[32] = 0x00; buffer[33] = 0x00; buffer[34] = 0x00; buffer[35] = 0x00; buffer[36] = 0x00; buffer[37] = 0x00; buffer[38] = 0x00; buffer[39] = 0x00; return buffer; } private byte[] addBMP_RGB_888(int[] b,int w, int h) { int len = b.length; System.out.println(b.length); byte[] buffer = new byte[w*h * 3]; int offset=0; for (int i = len-1; i>=w; i-=w) { //DIB文件格式最後一行為第一行,每行按從左到右順序 int end=i,start=i-w+1; for(int j=start;j<=end;j++){ buffer[offset]=(byte)(b[j]>>0); buffer[offset+1]=(byte)(b[j]>>8); buffer[offset+1]=(byte)(b[j]>>16); offset += 3; } } return buffer; } }

然而按這種方式保存後,圖片與原圖相比會出現明顯的顏色偏差。於是我下載了 UltraEdit,分別打開保存前與保存後的 bmp 文件,對比分析其內部結構。

BMP 文件由文件頭(14 字節)和位圖信息頭(40 字節)共 54 字節的頭部信息,加上後續的像素數據組成。文件頭各字節含義如下:

1)1-2 字節:圖像文件標識,0x4d42 即 'BM',表示 Windows 支持的 BMP 格式(注意 bfType 為 0x4D42 而非 0x424D)。
2)3-6 字節:整個文件大小。
3)7-8 字節:保留,必須設為 0。
4)9-10 字節:保留,必須設為 0。
5)11-14 字節:從文件頭到位圖數據的偏移量(14+40+4×(2^biBitCount))。

位圖信息頭

6)15-18:信息頭長度。
7)19-22:位圖寬度(像素)。
8)23-26:位圖高度(像素)。
9)27-28:位面數,恆為 1。
10)29-30:每像素位數,如 1(單色)、4(16 色)、8(256 色)、16(高彩色)、24(真彩色)、32(增強真彩色)。
11)31-34:壓縮方式,0 表示不壓縮。
12)35-38:位圖數據大小,須為 4 的倍數。
13)39-42:水平分辨率(像素/米)。
14)43-46:垂直分辨率(像素/米)。
15)47-50:使用的顏色索引數,設為 0 表示使用全部調色板。
16)51-54:重要顏色索引數,設為 0 表示都重要。

為了驗證顏色偏差是否源自頭部信息,我做了一個對比實驗:先用 UltraEdit 把保存前圖片的頭部 54 字節複製粘貼到保存後圖片的頭部,保存後重新打開,發現圖片並無變化;再反向操作一遍,結果同樣沒有變化。由此可見,文件頭並不是造成顏色偏差的原因。

問題只可能出在存儲像素信息的 addBMP_RGB_888 方法上。仔細閱讀代碼後發現,原作者寫錯了一處:

 buffer[offset+1]=(byte)(b[j]>>16); 

這裡應該改為:

 buffer[offset+2]=(byte)(b[j]>>16); 

也就是偏移量應該是 +2 而非 +1。修正這個 bug 後重新運行,保存的圖片顏色便恢復正常,與原圖完全一致。

總結:網上很多看似常見的問題,往往找不到真正適合自己的解答,這次也是反覆對比文件結構才定位到根源。這裡要感謝分享相關代碼的博主,同時也提醒大家:使用他人代碼時一定要多思考、多驗證,不要盲信。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved