参考资料: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. mybatisplus的坑 insert标签insert into select无参数问题的解决
  2. python起点网月票榜字体反爬案例
  3. NPM 和webpack 的基础使用
  4. 【阿里云镜像】使用阿里巴巴DNS镜像源——DNS配置教程
  5. 【阿里云镜像】切换阿里巴巴开源镜像站镜像——Debian镜像
  6. 关于Chromium的Android(安卓)Studio使用
  7. 解决Android模拟器打不开的问题!...
  8. Android(安卓)读取资源文件实例详解
  9. android启动后根文件系统分析

随机推荐

  1. Android(安卓)startActivityForResult()
  2. Android(安卓)Animation之TranslateAnima
  3. android 图片加载和缓存开源项目 Picasso
  4. 通过PC鼠标键盘操控Android手机:androidsc
  5. 这款开源 Android(安卓)实时投屏软件是 Q
  6. android: 使用 AsyncTask
  7. Android官方开发文档Training系列课程中
  8. [Android(安卓)Training视频系列] 8.2 Ma
  9. Message Android的享元模式
  10. Android判断字符串中是否含字母、中文或