Android保存32位BMP格式图片
16lz
2021-12-04
参考资料: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);}
更多相关文章
- mybatisplus的坑 insert标签insert into select无参数问题的解决
- python起点网月票榜字体反爬案例
- NPM 和webpack 的基础使用
- 【阿里云镜像】使用阿里巴巴DNS镜像源——DNS配置教程
- 【阿里云镜像】切换阿里巴巴开源镜像站镜像——Debian镜像
- 关于Chromium的Android(安卓)Studio使用
- 解决Android模拟器打不开的问题!...
- Android(安卓)读取资源文件实例详解
- android启动后根文件系统分析