Android如何把图片转为深度为32位格式为.bmp的图片
2017-10-28 20:47
441 查看
前几天公司的产品要求做个手机拍的照片转深度为32位,格式为.bmp格式的图片,仔细研究了下,通过调系统相机根据照片存的路径以及FileInputStream获得照片的bitmap,拿到这个bitmap后把数据放到下面的方法里,可获取bmp格式的图片,深度为32位的。
这是一个工具方法,可直接把图片的bitmap放入里面,方法里面的第二个参数name是保存bmp格式图片的名字,savePicturePath(”photoName”)为bmp格式图片保存路径。
/** * 将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; }
相关文章推荐
- 在android中如何通过像素填充格式画bmp图片
- 如何让你的 Android 显示gif格式的图片
- php 将bmp图片转为jpg等其他任意格式的图片
- Android 获取图片资源格式(png、jpeg、gif、bmp、...)
- Android Bitmap保存为.bmp格式,图像转化为黑白图片
- C# 如何将PDF转为多种图像文件格式(Png/Bmp/Emf/Tiff)
- Android系统移植与调试之------->如何使用PhotoShop转换24位的bmp图片为16位bmp图片
- UYVY视频格式转为BMP图片
- php 将bmp图片转为jpg等其他任意格式的图片
- Android Bitmap保存为.bmp格式,图像转化为黑白图片
- php 将bmp图片转为jpg等其他任意格式的图片
- C# 如何将PDF转为多种图像文件格式(Png/Bmp/Emf/Tiff)
- 如何将位图格式图片文件(.bmp)生成geotiff格式图片?
- 深度学习caffe应用笔记--如何将图片转换为lmdb格式
- 如何让你的Android显示gif格式的图片
- C++ C语言 读取32位BMP图片转为8位灰度图
- Android Bitmap保存为.bmp格式,图像转化为黑白图片
- BMP格式知识之二:16位,24位,32位的BMP图片算法是如何运算的
- [Image_Codec]常见图片格式的封装及编解码-Android平台(一)BMP
- 如何将位图格式图片文件(.bmp)生成geotiff格式图片?