1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 8位bmp 原始裸格式数据 需要加上文件头+位图信息头+调色板+位图数据 才是bmp图像

8位bmp 原始裸格式数据 需要加上文件头+位图信息头+调色板+位图数据 才是bmp图像

时间:2021-05-26 04:39:24

相关推荐

8位bmp 原始裸格式数据 需要加上文件头+位图信息头+调色板+位图数据 才是bmp图像

byte[ ]进行处理,添加bmp的文件头+位图信息头+调色板+位图数据。保存8位bmp,如果是调色板有问题,位图

会是纯白,纯黑等,无法显示正常图像。好了,代码奉上。

packagecom.idfounder.leakcanarydemo;importandroid.graphics.Bitmap;importandroid.graphics.Matrix;importandroid.util.Log;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io.IOException;/*** Created by hongzhen on /7/13.*/public classBmpUtils {private byte[]bytes;private intindex= 54;publicBmpUtils() {newThread(newRunnable() {@Overridepublic voidrun() {try{File file =newFile("/sdcard/Temp/1.raw");FileInputStream fileInputStream =newFileInputStream(file);bytes=new byte[(int)file.length()];while((fileInputStream.read(bytes)) != -1) {}fileInputStream.close();getBmpWith8(bytes,"/sdcard/Temp/bmp.bmp"); }catch(FileNotFoundException e) {e.printStackTrace();}catch(IOException e) {e.printStackTrace();}}}).start();}/** * 保存8位位图,需要添加颜色调色板,文件信息头+位图信息头+调色板+位图数据 *@parambufferSrc*@paramimgPath*/public voidgetBmpWith8(byte[] bufferSrc, String imgPath) {intw = 800;inth = 750;byte[] color_table = addBMP8ImageInfosHeaderTable(w, h); //颜色表,8位图必须有byte[] infos = addBMP8ImageInfosHeader(w, h);//文件信息头byte[] header = addBMP8ImageHeader(bufferSrc.length, infos.length+color_table.length); //文件头byte[] buffer =new byte[header.length+ infos.length+ color_table.length+ bufferSrc.length]; //申请用来组合上面四个部分的空间,这个空间直接保存就是bmp图了System.arraycopy(header, 0, buffer, 0, header.length);//复制文件头System.arraycopy(infos, 0, buffer, header.length, infos.length);//复制文件信息头System.arraycopy(color_table, 0, buffer, header.length+ infos.length,color_table.length); //复制颜色表System.arraycopy(bufferSrc, 0, buffer, header.length+ infos.length+color_table.length, bufferSrc.length);FileOutputStream fos =null;try{fos =newFileOutputStream(imgPath);fos.write(buffer);}catch(Exception e) {e.printStackTrace();}finally{try{fos.close();}catch(IOException e) {e.printStackTrace();}}}//采集原数据byte数组800*750-->640*640private byte[] changeByte(byte[] byteSrc) {intw = 640;inth = 640;byte[] bytes =new byte[w * h];//灰度图像8位深度intnewbiBitCount = 8;//8位图像数据每行字节数为4的倍数intlineByte = (w * newbiBitCount / 8 + 3) / 4 * 4;// int offset = 0;// for (int i = 55; i < h - 55; i++) {// for (int j = 80; j < w - 80; j++) {//bytes[i * 640 + j] = (byte) byteSrc[i * w + j];//offset++;// }// }for(inti = 0; i < h; i++) {for(intj = 0; j < w; j++){bytes[i * w + j] = (byte) byteSrc[i * 800 + 80 + j];// bytes[i * w + j] = (byte) byteSrc[(i) * (800) + (j)];//offset++;}}returnbytes;}/** * 裁剪并旋转180度原数据 *@parambyteSrc*@return*/private byte[] changeRotateByte(byte[] byteSrc){intdesW=(800-640)/2;//80intdesH=(750-640)/2;//55intw=640;inth=640;byte[] bytes =new byte[640 * 640];//灰度图像8位深度intnewbiBitCount = 8;//8位图像数据每行字节数为4的倍数intlineByte = (w * newbiBitCount / 8 + 3) / 4 * 4;intoffset=0;for(inti = 0; i < h; i++) {for(intj = 0; j < w; j++) {//bytes[i*lineByte + j] = (byte) byteSrc[i*w+j]; bytes[i * w + j] = (byte) byteSrc[(750-desH-i) * 800 + (800-desW - j)];}}returnbytes;}/** * Bitmap图像旋转 *@parambitMap*@paramnewWidth*@paramnewHeight*@return*/privateBitmap changeBitmapScale(Bitmap bitMap,intnewWidth,intnewHeight) {intwidth = bitMap.getWidth();intheight = bitMap.getHeight();// 计算缩放比例floatscaleWidth = ((float) newWidth) / width;floatscaleHeight = ((float) newHeight) / height;// 取得想要缩放的matrix参数Matrix matrix =newMatrix();matrix.postScale(scaleWidth, scaleHeight);// 得到新的图片bitMap = Bitmap.createBitmap(bitMap, 0, 0, width, height, matrix,true);returnbitMap;}/** * 8位位图的文件头 *@paramsize*@paramlenH*@return*/private byte[] addBMP8ImageHeader(intsize,intlenH) {byte[] buffer =new byte[14];intm_lenH = lenH + buffer.length;//lenH:文件信息头 //和颜色表长度之和size += m_lenH; //size:颜色数据的长度+两个文件头长度+颜色表长度buffer[0] = 0x42; //WORD 固定为0x4D42;buffer[1] = 0x4D;buffer[2] = (byte) (size >> 0); //DWORD 文件大小buffer[3] = (byte) (size >> 8);buffer[4] = (byte) (size >> 16);buffer[5] = (byte) (size >> 24);buffer[6] = 0x00; //WORD 保留字,不考虑buffer[7] = 0x00;buffer[8] = 0x00; //WORD 保留字,不考虑buffer[9] = 0x00;buffer[10] = (byte) (m_lenH >> 0);//DWORD 实际位图数据的偏移字buffer[11] = (byte) (m_lenH >> 8);//节数,即所有三个头(文件头、buffer[12] = (byte) (m_lenH >> 16); //文件信息头、颜色表)之和buffer[13] = (byte) (m_lenH >> 24); //14 + 40 + 1024 = 1078 //0x0436 0x0036=40+14=54returnbuffer;}private byte[] addBMP_8(byte[] b,intw,inth) {intlen = b.length;System.out.println(b.length);byte[] buffer =new byte[w * h];intoffset = 0;for(inti = len - 1; i >= (w - 1); i -= w) {// 对于bmp图,DIB文件格式最后一行为第一行,每行按从左到右顺序intend = i, start = i - w + 1;for(intj = start; j <= end; j++) {buffer[offset] = b[j];offset++;}}returnbuffer;}/** * 8位位图的位图信息头 *@paramw*@paramh*@return*/private byte[] addBMP8ImageInfosHeader(intw,inth) {byte[] buffer =new byte[40];intll = buffer.length;buffer[0] = (byte) (ll >> 0); //DWORD:本段头长度:40 0x0028buffer[1] = (byte) (ll >> 8);buffer[2] = (byte) (ll >> 16);buffer[3] = (byte) (ll >> 24);buffer[4] = (byte) (w >> 0); //long:图片宽度buffer[5] = (byte) (w >> 8);buffer[6] = (byte) (w >> 16);buffer[7] = (byte) (w >> 24);buffer[8] = (byte) (h >> 0); //long:图片高度buffer[9] = (byte) (h >> 8);buffer[10] = (byte) (h >> 16);buffer[11] = (byte) (h >> 24);buffer[12] = 0x01;//WORD:平面数:1buffer[13] = 0x00;buffer[14] = 0x08;//WORD:图像位数:8位buffer[15] = 0x00;buffer[16] = 0x00;//DWORD:压缩方式,可以是0,1,2,buffer[17] = 0x00;//其中0表示不压缩buffer[18] = 0x00;buffer[19] = 0x00;buffer[20] = 0x00;//DWORD;实际位图数据占用的字节数,当上一个数值buffer[21] = 0x00;//biCompression等于0时,这里的值可以省略不填buffer[22] = 0x00;buffer[23] = 0x00;buffer[24] = (byte) 0x20; //LONG:X方向分辨率buffer[25] = 0x4E;//20000(0x4E20) dpm 1 in = 0.0254 mbuffer[26] = 0x00;buffer[27] = 0x00;buffer[28] = (byte) 0x20; //LONG:Y方向分辨率buffer[29] = 0x4E;//20000(0x4E20) dpm 1 in = 0.0254 mbuffer[30] = 0x00;buffer[31] = 0x00;buffer[32] = 0x00;//DWORD:使用的颜色数,如果为0,buffer[33] = 0x00;//则表示默认值(2^颜色位数)buffer[34] = 0x00;buffer[35] = 0x00;buffer[36] = 0x00;//DWORD:重要颜色数,如果为0,buffer[37] = 0x00;//则表示所有颜色都是重要的buffer[38] = 0x00;buffer[39] = 0x00;returnbuffer;}/** * 8位位图的颜色调板 *@paramw*@paramh*@return*/private byte[] addBMP8ImageInfosHeaderTable(intw,inth) {byte[] buffer =new byte[256 * 4];//生成颜色表for(inti = 0; i < 256; i++) {buffer[0 + 4 * i] = (byte) i; //Bluebuffer[1 + 4 * i] = (byte) i; //Greenbuffer[2 + 4 * i] = (byte) i; //Redbuffer[3 + 4 * i] = (byte) 0x00; //保留值}returnbuffer;}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。