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

📄 bmpreader.java

📁 用java实现的对位图文件读取的源程序代码。
💻 JAVA
字号:
import java.io.IOException;
import java.io.FileNotFoundException;
import java.io.RandomAccessFile;

import java.nio.*;
import java.nio.channels.*;


public class BMPReader extends RandomAccessFile{
	
	public static final int[] BMP_TYPE = new int[]{1,2,4,8,16,24,32};
	//private final static int _1M = 1024 * 1024;
	private byte[] bmpData;
	
	public BMPReader(String fileName,String mode) throws Exception {
		super(fileName,mode);
		long fileLength = length();		
		
		if (fileLength < 0X28) {
			throw new Exception("不是正确的BMP文件格式(没到达基本长度)");
		}
		if (fileLength > Integer.MAX_VALUE) {
			throw new Exception("超过处理能力 > 2G");
		}
		
		
		ByteBuffer byteBuffer 	= ByteBuffer.allocateDirect((int)fileLength);
		bmpData 				= new byte[(int)fileLength];
		
		FileChannel fileChannel = this.getChannel();
		
		fileChannel.read(byteBuffer);
		
		byteBuffer.flip();
		
		byteBuffer.get(bmpData);
		
	}
	
	
	protected boolean isBMPFile(int colorBits) {
		boolean isBMP = false;
		for (int i=0; i<BMP_TYPE.length; i++) {
			if (colorBits == BMP_TYPE[i]) {
				isBMP = true;
				break;
			}
		}
		return isBMP;
	}	
	
	public int[][] readBMP(int offset, int width, int height, int bits) throws IOException, Exception {
		if (!isBMPFile(bits)) {
			throw new Exception("文件类型不匹配(有效类型位:1bit,2bit" + 
										",4bit,8bit,16bit,24bit,32bit)");
		}
		int[][] data = null;
		switch(bits) {
			case 1 	: 	data = read1bitBMP(offset,width,height);	break;
			case 2 	: 	data = read2bitBMP(offset,width,height);	break;
			case 4 	: 	data = read4bitBMP(offset,width,height);	break;
			case 8 	: 	data = read8bitBMP(offset,width,height);	break;
			case 16 : 	data = read16bitBMP(offset,width,height);	break;
			case 24 : 	data = read24bitBMP(offset,width,height);	break;
			case 32 : 	data = read32bitBMP(offset,width,height);	break;
		}
		
		return data;
	}
	
	public int readData(int offset, int byteSize) throws IOException {
		
		int data = 0;
		int shift = 0;
		int end = offset + byteSize;
		for (int i=offset; i<end; i++) {
			
			data += getUnsignByte(bmpData[i])<<8*shift;
			
			shift ++;
		}	
		return data;
	}
	
	
	public static int getUnsignByte(byte aByte) {
		if (aByte >= 0) {
			return aByte;
		}
		
		return (aByte & 0X7F) + 0X80;
	}
	//public int
	
	
	public int[] read4byteArray(int offset, int intSize) throws IOException {
		int[] data = new int[intSize];
		final int BYTES = 4;
		int index = offset;
		
		for (int i=0; i<intSize; i++) {			
			data[i] = readData(index,BYTES);
			index += 4;
		}

		return data;
	}
		
	public int[][] read1bitBMP(int offset, int width, int height) throws IOException {
		return null;
	}
	
	public int[][] read2bitBMP(int offset, int width, int height) throws IOException {
		return null;
	}
	
	public int[][] read4bitBMP(int offset, int width, int height) throws IOException {
		return null;
	}
	
	public int[][] read8bitBMP(int offset, int width, int height) throws IOException {
		width = ((width+3)/4)*4;
		int[][] data = new int[height][width];
		int index = offset;
		for (int i=height-1; i>-1; i--) {
			for (int j=0; j<width; j++) {
				data[i][j] = getUnsignByte(bmpData[index++]);
			}
		}
		return data;
	}
	
	public int[][] read16bitBMP(int offset, int width, int height) throws IOException {
		int[][] data = new int[height][width];
		int oneData = 0;

		
		for (int i=height-1; i>-1; i--) {
			for (int j=0; j<width; j++) {
				
			}
		}
	
		return data;
	}
	
	public int[][] read24bitBMP(int offset, int width, int height) throws IOException {
		width = ((width+3)/4)*4;
		
		int[][] data = new int[height][width];
		
		int index = offset;
		int aInt = 0;
		for (int i=height-1; i>-1; i--) {
			for (int j=0; j<width; j++) {
				aInt = 		getUnsignByte(bmpData[index++]);
				aInt += 	getUnsignByte(bmpData[index++]) << 8;
				aInt += 	getUnsignByte(bmpData[index++]) << 16;
				data[i][j] = aInt;
			}
		}

		return data;
	}	
	
	public int[][] read32bitBMP(int offset, int width, int height) throws IOException {
		int[][] data = new int[height][width];
		
		int index = offset;
		int aInt = 0;
		for (int i=height-1; i>-1; i--) {
			for (int j=0; j<width; j++) {	
				aInt = 		getUnsignByte(bmpData[index++]);
				aInt += 	getUnsignByte(bmpData[index++]) << 8;
				aInt += 	getUnsignByte(bmpData[index++]) << 16;
				index ++;
				data[i][j] = aInt;
			}
		}

		return data;
	}	
}

⌨️ 快捷键说明

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