分享

JAVA实现对BMP图片的读取

 nacy2012 2015-07-21

BMP图片格式,是windows自带的一个图片格式,(*bmp),在windows的系统下都支持这种格式,bmp格式与设备无关的位图(DIB)格式,BMP简称位图,BMP的原始数据是没有经过压缩处理的  占用的空间比其它格式的图片要大

 

   BMP由四部分组成 ,位图文件头 , 位图信息头 ,  颜色 , 图像数据区

 

BMP图片是三个字节为一个颜色保存,将字节拼接为int需要使用位移来做

 

位图文件头 (12个字节):

 0000-0001:文件标识,为字母ASCII码“BM”,42 4D。

0002-0005:整个文件大小,单位字节。 
0006-0009:这个位置的数字是被微软保留的 
000A-000D:记录图像数据区的起始位置。从文件开始到位图数据(bitmap data)之间的偏移量。 
位图信息头()
000E-0011:图像描述信息块的大小 
0012-0015:图像宽度。以像素为单位。 
0016-0019:图像高度。以像素为单位。 
001A-001B:图像的plane总数(恒为1)。 
001C-001D:记录像素,也就是颜色。1 - Monochrome bitmap,4 - 16 color bitmap,8 - 256 color bitmap,F - 16位位图,18 - 24bit (true color) bitmap,20 - 32位位图。

001E-0021:数据压缩方式(数值位0:不压缩;1:8位压缩;2:4位压缩;3:Bitfields压缩)。 
0022-0025:图像区数据的大小。单位字节,该数必须是4的倍数。 
0026-0029:水平每米有多少像素,在设备无关位图(.DIB)中,每字节以00H填写。 
002A-002D:垂直每米有多少像素,在设备无关位图(.DIB)中,每字节以00H填写。 
002E-0031:此图像所用的颜色数。

0032-0035:指定重要的颜色数。当该域的值等于颜色数时(或者等于0时),表示所有颜色都一样重要。



 

颜色
每一个字节表示一个颜色 r 0~255 g  0~255  b 0~255  保留一位  
 
图像数据区
从下到上 从左往右的顺序扫描
 
 
JAVA写一个读取BMP图片
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;

import java.awt.Graphics;
import java.io.BufferedInputStream;
import java.io.FileInputStream;

import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 * 图片查看器的窗口
 * 
 * @author Administrator
 * 
 */
public class priUI extends JFrame {
	public static void main(String[] args) {
		priUI ui = new priUI();
		ui.initUI();
	}

	public void initUI() {

		this.setSize(600, 500);
		this.setTitle("图片查看器");

		// 设置布局
		FlowLayout layout = new FlowLayout();
		this.setLayout(layout);

		JPanel center = new myPanel();
		center.setPreferredSize(new Dimension(400, 300));
		center.setBackground(Color.WHITE);
		this.add(center);

		this.setDefaultCloseOperation(3);
		this.setVisible(true);
	}

	/**
	 * 读取BMP文件的方法(BMP24位)
	 */

	public int[][] readFile(String path) {

		try {
			// 创建读取文件的字节流
			FileInputStream fis = new FileInputStream(path);
			BufferedInputStream bis = new BufferedInputStream(fis);
			// 读取时丢掉前面的18位,
			// 读取图片的18~21的宽度
			bis.skip(18);
			byte[] b = new byte[4];
			bis.read(b);
			// 读取图片的高度22~25
			byte[] b2 = new byte[4];
			bis.read(b2);

			// 得到图片的高度和宽度
			int width = byte2Int(b);
			int heigth = byte2Int(b2);
			// 使用数组保存得图片的高度和宽度
			int[][] date = new int[heigth][width];

			int skipnum = 0;
			if (width * 3 / 4 != 0) {
				skipnum = 4 - width * 3 % 4;
			}
			// 读取位图中的数据,位图中数据时从54位开始的,在读取数据前要丢掉前面的数据
			bis.skip(28);
			for (int i = 0; i < date.length; i++) {
				for (int j = 0; j < date[i].length; j++) {
					// bmp的图片在window里面世3个byte为一个像素
					int blue = bis.read();
					int green = bis.read();
					int red = bis.read();
					// 创建一个Color对象,将rgb作为参数传入其中
					Color c = new Color(red, green, blue);
					// Color c = new Color(blue,green,red);
					// 将得到的像素保存到date数组中
					date[i][j] = c.getRGB();
				}
				// 如果补0的个数不为0,则需要跳过这些补上的0
				if (skipnum != 0) {
					bis.skip(skipnum);
				}
			}
			return date;
		} catch (Exception e) {
			e.printStackTrace();

		}
		return null;

	}

	// 将四个byte拼接成一个int
	public int byte2Int(byte[] by) {
		int t1 = by[3] & 0xff;
		int t2 = by[2] & 0xff;
		int t3 = by[1] & 0xff;
		int t4 = by[0] & 0xff;
		int num = t1 << 24 | t2 << 16 | t3 << 8 | t4;
		return num;

	}

	class myPanel extends JPanel {
		public void paint(Graphics g) {
			super.paint(g);
			// 读取数据
			int[][] date = readFile("C:\\Users\\Administrator\\Desktop\\meinv.bmp");
			// 判断是否存在
			if (date != null) {
				// this.setPreferredSize(new
				// Dimension(date[0].length,date.length));
				this.setPreferredSize(new Dimension(date[0].length, date.length));
				// 遍历
				for (int i = 0; i < date.length; i++) {
					for (int j = 0; j < date[i].length; j++) {
						Color c = new Color(date[i][j]);
						g.setColor(c);
						g.drawLine(j, date.length - i, j, date.length - i);
					}
				}
			}
		}
	}
}
  

 
  

 


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多