您的位置:首页 > 移动开发 > Android开发

Android如何把图片转为深度为32位格式为.bmp的图片

2017-10-28 20:47 441 查看
前几天公司的产品要求做个手机拍的照片转深度为32位,格式为.bmp格式的图片,仔细研究了下,通过调系统相机根据照片存的路径以及FileInputStream获得照片的bitmap,拿到这个bitmap后把数据放到下面的方法里,可获取bmp格式的图片,深度为32位的。

/**
* 将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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: