1. 程式人生 > >Android如何把圖片轉為深度為32位格式為.bmp的圖片

Android如何把圖片轉為深度為32位格式為.bmp的圖片

前幾天公司的產品要求做個手機拍的照片轉深度為32位,格式為.bmp格式的圖片,仔細研究了下,通過調系統相機根據照片存的路徑以及FileInputStream獲得照片的bitmap,拿到這個bitmap後把資料放到下面的方法裡,可獲取bmp格式的圖片,深度為32位的。

/** 
     * 將Bitmap存為 .bmp格式圖片 
     * @param bitmap 
     */  
    public void saveBmp(Bitmap bitmap,long name) {  
        if (bitmap == null)  
            return;  
        // 點陣圖大小  
        int nBmpWidth = bitmap.getWidth();  
        int nBmpHeight = bitmap.getHeight();  
        // 影象資料大小  
       // int bufferSize = nBmpHeight * (nBmpWidth * 3 + nBmpWidth % 4);  
        int bufferSize = nBmpHeight * (nBmpWidth * 4 + nBmpWidth % 5);
        try {  
            // 儲存檔名  
           // String filename = savePhoto();  
            String filename =  savePicturePath("HanvonDataBmp");
            File file = new File(filename);  
            if (!file.exists()) {  
                file.createNewFile();  
            }  
            FileOutputStream fileos = new FileOutputStream(filename);  
            // bmp檔案頭  
            int bfType = 0x4d42;  
            long bfSize = 14 + 40 + bufferSize;  
            int bfReserved1 = 0;  
            int bfReserved2 = 0;  
            long bfOffBits = 14 + 40;  
            // 儲存bmp檔案頭  
            writeWord(fileos, bfType);  
            writeDword(fileos, bfSize);  
            writeWord(fileos, bfReserved1);  
            writeWord(fileos, bfReserved2);  
            writeDword(fileos, bfOffBits);  
            // bmp資訊頭  
            long biSize = 40L;  
            long biWidth = nBmpWidth;  
            long biHeight = nBmpHeight;  
            int biPlanes = 1;  
           // int biBitCount = 24;  
            int biBitCount = 32;

            long biCompression = 0L;  
            long biSizeImage = 0L;  
            long biXpelsPerMeter = 0L;  
            long biYPelsPerMeter = 0L;  
            long biClrUsed = 0L;  
            long biClrImportant = 0L;  
            // 儲存bmp資訊頭  
            writeDword(fileos, biSize);  
            writeLong(fileos, biWidth);  
            writeLong(fileos, biHeight);  
            writeWord(fileos, biPlanes);  
            writeWord(fileos, biBitCount);  
            writeDword(fileos, biCompression);  
            writeDword(fileos, biSizeImage);  
            writeLong(fileos, biXpelsPerMeter);  
            writeLong(fileos, biYPelsPerMeter);  
            writeDword(fileos, biClrUsed);  
            writeDword(fileos, biClrImportant);  
            // 畫素掃描  
            byte bmpData[] = new byte[bufferSize];  
            //int wWidth = (nBmpWidth * 3 + nBmpWidth % 4);  
            int wWidth = (nBmpWidth * 4 + nBmpWidth % 5);
            for (int nCol = 0, nRealCol = nBmpHeight - 1; nCol < nBmpHeight; ++nCol, --nRealCol)  
                for (int wRow = 0, wByteIdex = 0; wRow < nBmpWidth; wRow++, wByteIdex += 4) {  
                    int clr = bitmap.getPixel(wRow, nCol);  
                    bmpData[nRealCol * wWidth + wByteIdex] = (byte) Color.blue(clr);  
                    bmpData[nRealCol * wWidth + wByteIdex + 1] = (byte) Color.green(clr);  
                    bmpData[nRealCol * wWidth + wByteIdex + 2] = (byte) Color.red(clr); 
                    bmpData[nRealCol * wWidth + wByteIdex+3] = (byte) Color.alpha(0xff);
                }  

            fileos.write(bmpData);  
            fileos.flush();  
            fileos.close();  

        } catch (FileNotFoundException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    }  

    protected void writeWord(FileOutputStream stream, int value) throws IOException {  
        byte[] b = new byte[2];  
        b[0] = (byte) (value & 0xff);  
        b[1] = (byte) (value >> 8 & 0xff);  
        stream.write(b);  
    }  

    protected void writeDword(FileOutputStream stream, long value) throws IOException {  
        byte[] b = new byte[4];  
        b[0] = (byte) (value & 0xff);  
        b[1] = (byte) (value >> 8 & 0xff);  
        b[2] = (byte) (value >> 16 & 0xff);  
        b[3] = (byte) (value >> 24 & 0xff);  
        stream.write(b);  
    }  

    protected void writeLong(FileOutputStream stream, long value) throws IOException {  
        byte[] b = new byte[4];  
        b[0] = (byte) (value & 0xff);  
        b[1] = (byte) (value >> 8 & 0xff);  
        b[2] = (byte) (value >> 16 & 0xff);  
        b[3] = (byte) (value >> 24 & 0xff);  
        stream.write(b);  
    }  

這是一個工具方法,可直接把圖片的bitmap放入裡面,方法裡面的第二個引數name是儲存bmp格式圖片的名字,savePicturePath(”photoName”)為bmp格式圖片儲存路徑。

public String savePicturePath(String photoName) {
    String mFilePath = Environment.getExternalStorageDirectory().getPath();// 獲取SD卡路徑
    String fpath = "";
    File file = new File(mFilePath + File.separatorChar + photoName);
    if (!file.exists()) {
        file.mkdirs();
    }
    fpath = mFilePath + File.separatorChar + File.separatorChar + photoName;
    file = new File(fpath);
    if (!file.exists()) {
        file.mkdirs();
    }
    long picName = System.currentTimeMillis();
    String PicturePath = fpath + File.separatorChar + picName + ".png";
    file = null;
    return PicturePath;
}