⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bmpfileheader.java

📁 用java实现的对位图文件读取的源程序代码。
💻 JAVA
字号:

import java.util.HashMap;

public class BMPFileHeader {
	
	//图象文件头
	
	public final static int FILE_TYPE 		= 0X0;
	public final static int FILE_SIZE 		= 0X2;
	public final static int RESERVED 		= 0X6;
	public final static int DATA_OFFSET 	= 0XA;
	public final static int HEAD_SIZE 		= 0XE;
	public final static int WIDTH 			= 0X12;
	public final static int HEIGHT 			= 0X16;
	public final static int PLANES 			= 0X1A;
	
	public final static HashMap<Integer,Integer> lengthMap = new HashMap<Integer,Integer>(8);
	//set length  map 
	{
		lengthMap.put(FILE_TYPE,2);
		lengthMap.put(FILE_SIZE,4);
		lengthMap.put(RESERVED,4);
		lengthMap.put(DATA_OFFSET,4);
		lengthMap.put(HEAD_SIZE,4);
		lengthMap.put(WIDTH,4);
		lengthMap.put(HEIGHT,4);
		lengthMap.put(PLANES,2);
	}
	
	private int fileType;
	private int fileSize;
	private int reserved;
	private int dataOffset;
	private int headSize;
	private int width;
	private int height;
	private int planes;	


	public BMPFileHeader() {
		// TODO: Add your code here
	}	
	
	public void fill(BMPReader reader) throws Exception {
		fileType 	= reader.readData(FILE_TYPE,	lengthMap.get(FILE_TYPE));
		
		if (!isBMPFile()) {
			throw new Exception("不是正确的BMP文件格式(可能是由于直接将其它格式的文件的扩展名改为BMP(bmp)造成");
		}
		
		fileSize 	= reader.readData(FILE_SIZE,	lengthMap.get(FILE_SIZE));
		reserved 	= reader.readData(RESERVED,		lengthMap.get(RESERVED));
		dataOffset 	= reader.readData(DATA_OFFSET,	lengthMap.get(DATA_OFFSET));
		headSize 	= reader.readData(HEAD_SIZE,	lengthMap.get(HEAD_SIZE));
		width 		= reader.readData(WIDTH	,		lengthMap.get(WIDTH));
		height 		= reader.readData(HEIGHT,		lengthMap.get(HEIGHT));
		planes 		= reader.readData(PLANES,		lengthMap.get(PLANES));
	}
	
	public boolean isBMPFile() {
		return fileType == 0X4D42;
	}
	
	public String toString() {
		return "(fileType=" 	+ Integer.toHexString(fileType) 	+ "," + 
				"fileSize=" 	+ Integer.toHexString(fileSize)		+ "," + 
				"reserved=" 	+ Integer.toHexString(reserved)		+ "," + 
				"dataOffset=" 	+ Integer.toHexString(dataOffset) 	+ "," + 
				"headSize=" 	+ Integer.toHexString(headSize)		+ "," + 
				"width=" 		+ Integer.toHexString(width) 		+ "," + 
				"height=" 		+ Integer.toHexString(height) 		+ "," + 
				"planes=" 		+ Integer.toHexString(planes)		+ ")" ;
	}
	
	public int getFileType() {
		return fileType;
	}

	public void setFileType(int aFileType) {
		fileType = aFileType;
	}

	public int getFileSize() {
		return fileSize;
	}

	public void setFileSize(int aFileSize) {
		fileSize = aFileSize;
	}

	public int getReserved() {
		return reserved;
	}

	public void setReserved(int aReserved) {
		reserved = aReserved;
	}

	public int getDataOffset() {
		return dataOffset;
	}

	public void setDataOffset(int aDataOffset) {
		dataOffset = aDataOffset;
	}

	public int getHeadSize() {
		return headSize;
	}

	public void setHeadSize(int aHeadSize) {
		headSize = aHeadSize;
	}

	public int getWidth() {
		return width;
	}

	public void setWidth(int aWidth) {
		width = aWidth;
	}

	public int getHeight() {
		return height;
	}

	public void setHeight(int aHeight) {
		height = aHeight;
	}

	public int getPlanes() {
		return planes;
	}

	public void setPlanes(int aPlanes) {
		planes = aPlanes;
	}
	
	public static void main(String[] args) throws Exception {
		String fileName = "girl.bmp";
		String mode = "rw";
		BMPReader reader = new BMPReader(fileName,mode);
		BMPFileHeader aBMPFileHeader = new BMPFileHeader();
		
		aBMPFileHeader.fill(reader);	
		System.out.println(aBMPFileHeader);
		
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -