1. 程式人生 > >Android儲存32位BMP格式圖片

Android儲存32位BMP格式圖片

參考資料:java讀取bmp點陣圖
java讀取bmp影象檔案

Android 獲取24位BMP RGB資料


     /**
     * 獲取BMP 檔案的RGB 資料
     * @param srcBitmap 原Bitmap
     * @return bitmap的RGB資料
     */
    public static  byte[] getBmpRGBData24(Bitmap srcBitmap ){
        int bmpWidth =srcBitmap.getWidth();
        int bmpHeight =srcBitmap.getHeight();
        int
bufferSize = bmpHeight*bmpWidth*3; byte[] bmpData = new byte[bufferSize]; int wWidth = (bmpWidth * 3); for (int nCol = 0, nRealCol = bmpHeight - 1; nCol < bmpWidth; ++nCol, --nRealCol){ for (int wRow = 0, wByteIndex = 0; wRow < bmpWidth; wRow++, wByteIndex += 3
) { int clr = srcBitmap.getPixel(wRow, nCol); bmpData[nRealCol * wWidth + wByteIndex] = (byte) Color.blue(clr); bmpData[nRealCol * wWidth + wByteIndex + 1] = (byte) Color.green(clr); bmpData[nRealCol * wWidth + wByteIndex + 2] = (byte) Color.red(clr); } } return
bmpData; }

該方法不獲取到的資料不包含14個位元組的檔案頭和40個位元組的資訊頭。

Android 獲取32位BMP RGB資料

  /**
     * 獲取BMP 檔案的RGB 資料
     * @param srcBitmap 原Bitmap
     * @return bitmap的RGB資料
     */
    public static  byte[] getBmpRGBData(Bitmap srcBitmap ){
        int nBmpWidth =srcBitmap.getWidth();
        int nBmpHeight =srcBitmap.getHeight();
        int bufferSize = nBmpHeight*nBmpWidth*4;
        byte[] bmpData = new byte[bufferSize];
        int wWidth = (nBmpWidth * 4);
        for (int nCol = 0, nRealCol = nBmpHeight - 1; nCol < nBmpHeight; ++nCol, --nRealCol){
            for (int wRow = 0, wByteIdex = 0; wRow < nBmpWidth; wRow++, wByteIdex += 4) {
                int clr = srcBitmap.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);
            }
        }
        return bmpData;
    }

該方法不獲取到的資料不包含14個位元組的檔案頭和40個位元組的資訊頭。

Android 儲存32位BMP 圖片方法

  /**
     * 將Bitmap存為 .bmp格式圖片
     * @param bitmap 原圖片
     */
    public static void saveBmp(Bitmap bitmap, String path) {
        if (bitmap==null){
            return;
        }

        byte bmpData[];
        int nBmpWidth = bitmap.getWidth();
        int nBmpHeight = bitmap.getHeight();
        // 影象資料大小
        int bufferSize = nBmpHeight*nBmpWidth*4;
        try {
            File file = new File(path);
            File fileParent=file.getParentFile();
            if (!fileParent.exists()){
                fileParent.mkdirs();
            }
            if (file.exists()) {
                file.delete();
            }
            FileOutputStream fileos = new FileOutputStream(path);
            // 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;
            int biPlanes = 1;
            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, (long) nBmpWidth);
            writeLong(fileos, (long) nBmpHeight);
            writeWord(fileos, biPlanes);
            writeWord(fileos, biBitCount);
            writeDword(fileos, biCompression);
            writeDword(fileos, biSizeImage);
            writeLong(fileos, biXpelsPerMeter);
            writeLong(fileos, biYPelsPerMeter);
            writeDword(fileos, biClrUsed);
            writeDword(fileos, biClrImportant);
            // 畫素掃描
            bmpData = new byte[bufferSize];
            int wWidth = (nBmpWidth * 4);
            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();
        }
    }
private static 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);
}

private static 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);
}

private static 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);
}