由于在eclipse中,无法对BMP图片文件进行读取,因而我们要将BMP文件拆分读取
首先,关联一个BMP文件,建立个输入流来准备存储数据,.用一个一维字节数组去接收BMP文件头部信息
然后读取BMP图片的宽高
File file = new File("G:\\CP\\新文件夹\\160117\\test.bmp");
FileInputStream fis = new FileInputStream(file);
byte[] bytes = new byte[54];
fis.read(bytes);
int width = byteChangeToInt(bytes[21], bytes[20], bytes[19], bytes[18]);
int height = byteChangeToInt(bytes[25], bytes[24], bytes[23], bytes[22]);
System.out.println(width+" "+height);
this.setSize(width,height);
然后我们需要写一个方法将BMP文件中的byte数据转换为int类型
由于一个int类型是有四个byte组成的,所以要建立4个字节
private int byteChangeToInt(byte byte1,byte byte2,byte byte3,byte byte4) {
int value1 = ((int)byte1&0xff)<<24;
int value2 = ((int)byte2&0xff)<<16;
int value3 = ((int)byte3&0xff)<<8;
int value4 = (int)byte4&0xff;
return value1|value2|value