Android储存BMP格式图片
16lz
2021-12-04
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文件头 14buffer[0] = 0x42; //bfType 2bytesbuffer[1] = 0x4D;buffer[2] = (byte) ((size >> 0) & 0xFF); //bfSize 4bytesbuffer[3] = (byte) ((size >> 8) & 0xFF);buffer[4] = (byte) ((size >> 16) & 0xFF);buffer[5] = (byte) ((size >> 24) & 0xFF);buffer[6] = 0x00; //bfReserved1 2bytesbuffer[7] = 0x00;buffer[8] = 0x00; //bfReserved2 2bytesbuffer[9] = 0x00;buffer[10] = 0x36; //bfOffBits 14+40 4bytesbuffer[11] = 0x00;buffer[12] = 0x00;buffer[13] = 0x00;// 2.BMP信息头 40buffer[14] = 0x28; //biSize 40 4bytesbuffer[15] = 0x00;buffer[16] = 0x00;buffer[17] = 0x00;buffer[18] = (byte) ((w >> 0) & 0xFF); //biWidth 4bytesbuffer[19] = (byte) ((w >> 8) & 0xFF);buffer[20] = (byte) ((w >> 16) & 0xFF);buffer[21] = (byte) ((w >> 24) & 0xFF);buffer[22] = (byte) ((h >> 0) & 0xFF); //biHeight 4bytesbuffer[23] = (byte) ((h >> 8) & 0xFF);buffer[24] = (byte) ((h >> 16) & 0xFF);buffer[25] = (byte) ((h >> 24) & 0xFF);buffer[26] = 0x01; //biPlanes 2bytesbuffer[27] = 0x00;buffer[28] = 0x18; //biBitCount 24位位图 2bytes buffer[29] = 0x00;buffer[30] = 0x00; //biCompression 4bytesbuffer[31] = 0x00;buffer[32] = 0x00;buffer[33] = 0x00;buffer[34] = 0x00; //biSizeImage 4bytesbuffer[35] = 0x00;buffer[36] = 0x00;buffer[37] = 0x00;buffer[38] = 0x00; //biXpelsPerMeter 4bytesbuffer[39] = 0x00;buffer[40] = 0x00;buffer[41] = 0x00;buffer[42] = 0x00; //biYPelsPerMeter 4bytesbuffer[43] = 0x00;buffer[44] = 0x00;buffer[45] = 0x00;buffer[46] = 0x00; //biClrUsed 4bytesbuffer[47] = 0x00;buffer[48] = 0x00;buffer[49] = 0x00;buffer[50] = 0x00; //biClrImportant 4bytesbuffer[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;}
更多相关文章
- Android(安卓)BitmapUtils工具类
- Android中LOG机制详解(上)
- 关于android分辨率兼容问题(一)
- [Android1.5]DigitalClock自定义日期输出格式
- TecentUtil 腾讯微博 android
- android 图片的自由缩放和旋转
- android线程池的优化
- Android(安卓)图片处理工具类汇总
- android图片压缩并转为base64字符串