Android储存BMP格式图片

BMP格式可参考这片文章:点击跳转

上代码

	public static byte[] convertBitmap2Bmp(Bitmap bitmap) {
		if (bitmap == null) {
			return null;
		}
		
		int w = bitmap.getWidth();
		int h = bitmap.getHeight();
		int wWidth = w * 3 + w % 4;
		int bmpDateSize = h * wWidth;
		int size = 14 + 40 + bmpDateSize;
		byte buffer[] = new byte[size];
		
		// 1.BMP文件头 14
		buffer[0] = 0x42; //bfType 2bytes
		buffer[1] = 0x4D;
		buffer[2] = (byte) ((size >> 0) & 0xFF); //bfSize 4bytes
		buffer[3] = (byte) ((size >> 8) & 0xFF);
		buffer[4] = (byte) ((size >> 16) & 0xFF);
		buffer[5] = (byte) ((size >> 24) & 0xFF);
		buffer[6] = 0x00; //bfReserved1 2bytes
		buffer[7] = 0x00;
		buffer[8] = 0x00; //bfReserved2 2bytes
		buffer[9] = 0x00;
		buffer[10] = 0x36; //bfOffBits 14+40 4bytes
		buffer[11] = 0x00;
		buffer[12] = 0x00;
		buffer[13] = 0x00;
		// 2.BMP信息头 40
		buffer[14] = 0x28; //biSize 40 4bytes
		buffer[15] = 0x00;
		buffer[16] = 0x00;
		buffer[17] = 0x00;
		buffer[18] = (byte) ((w >> 0) & 0xFF); //biWidth 4bytes
		buffer[19] = (byte) ((w >> 8) & 0xFF);
		buffer[20] = (byte) ((w >> 16) & 0xFF);
		buffer[21] = (byte) ((w >> 24) & 0xFF);
		buffer[22] = (byte) ((h >> 0) & 0xFF); //biHeight 4bytes
		buffer[23] = (byte) ((h >> 8) & 0xFF);
		buffer[24] = (byte) ((h >> 16) & 0xFF);
		buffer[25] = (byte) ((h >> 24) & 0xFF);
		buffer[26] = 0x01; //biPlanes 2bytes
		buffer[27] = 0x00;
		buffer[28] = 0x18; //biBitCount 24位位图 2bytes 
		buffer[29] = 0x00;
		buffer[30] = 0x00; //biCompression 4bytes
		buffer[31] = 0x00;
		buffer[32] = 0x00;
		buffer[33] = 0x00;
		buffer[34] = 0x00; //biSizeImage 4bytes
		buffer[35] = 0x00;
		buffer[36] = 0x00;
		buffer[37] = 0x00;
		buffer[38] = 0x00; //biXpelsPerMeter 4bytes
		buffer[39] = 0x00;
		buffer[40] = 0x00;
		buffer[41] = 0x00;
		buffer[42] = 0x00; //biYPelsPerMeter 4bytes
		buffer[43] = 0x00;
		buffer[44] = 0x00;
		buffer[45] = 0x00;
		buffer[46] = 0x00; //biClrUsed 4bytes
		buffer[47] = 0x00;
		buffer[48] = 0x00;
		buffer[49] = 0x00;
		buffer[50] = 0x00; //biClrImportant 4bytes
		buffer[51] = 0x00;
		buffer[52] = 0x00;
		buffer[53] = 0x00;
		
		byte bmpData[] = new byte[bmpDateSize];
		for (int nCol = 0, nRealCol = h - 1; nCol < h; ++nCol, --nRealCol) {
			for (int wRow = 0, wByteIdex = 0; wRow < w; wRow++, wByteIdex += 3) {
				int clr = bitmap.getPixel(wRow, nCol);
				//clr = clr == 0 ? 0xFFFFFF : clr; //黑色背景转为白色
				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);
			}
		}
		
		System.arraycopy(bmpData, 0, buffer, 54, bmpDateSize);
		
		// 输出到sdcard查看
		try {
			FileOutputStream fos = new FileOutputStream(new File("/sdcard/test.bmp"));
			fos.write(buffer);
			fos.close();
		} catch(Exception e) {
			e.printStackTrace();
		}
		return buffer;
	}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值