参考资料: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);}

更多相关文章

  1. 待机后,android中activity的数据丢失问题解决
  2. Android 读取资源文件实例详解
  3. Android通过ContentProvider传输文件
  4. android启动后根文件系统分析
  5. 转:Android下文件操作模式(含SDCard的读写)
  6. Android 文件存储--内部存储的例子
  7. Android之再谈文件操作和SDcard读写
  8. Android 封装json数据
  9. android的文件操作。(未整理完成)

随机推荐

  1. LinearLayout中实现水平方向上的两个text
  2. Android应用程序启动过程源代码分析
  3. Android进程与线程基本知识
  4. 从linux看Android之一--init进程
  5. android中的handler
  6. Android(安卓)如何获取keyboard和TP消息
  7. Android(安卓)Scripting Environment -AS
  8. 在Android程序中使用全局变量
  9. 【算法总结】30道题搞定大厂算法面试-二
  10. Android——修改开机画面