JAVA读取BMP图片文件

由于在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
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值