📄 bmp_information.java
字号:
package bmpinfo;
import java.io.*;
public class Bmp_Information implements Cloneable
{
String type;
double totalsize;
double offbits;
double inf_size;
double width;
double Length;
int bitcounts;
double datasize;
int[] redvalue=new int[256];
int[] greenvalue=new int[256];
int[] bluevalue=new int[256];
int[] redlevel=new int[256];
int[] greenlevel=new int[256];
int[] bluelevel=new int[256];
int[] graylevel=new int[256];
int[] greyvalue=new int[256];
public Bmp_Information(String file)
{
try{
String type="";
File bmpfile=new File(file);
DataInputStream file_in=new DataInputStream(new FileInputStream(bmpfile));
//读取文件格式
for (int i=1;i<=2 ;i++ )
{
byte ch=file_in.readByte();
type+=(char)ch;
}
this.type=type;
//读取整个文件大小
int totalsize=file_in.readInt();
String size=Integer.toHexString(totalsize);
this.totalsize=DWORD(size)/1024;
//保留2个字
int reserved=file_in.readInt();
//读取文件头到实际的位图数据的偏移字节数
int offbits=file_in.readInt();
String bits=Integer.toHexString(offbits);
this.offbits=DWORD(bits);
//读取信息头
//读取信息头长度
int inf_size=file_in.readInt();
String infsize=Integer.toHexString(inf_size);
this.inf_size=DWORD(infsize);
//读取图片大小
int bmp_width=file_in.readInt();
String width=Integer.toHexString(bmp_width);
this.width=DWORD(width);
int bmp_length=file_in.readInt();
String Length=Integer.toHexString(bmp_length);
this.Length=DWORD(Length);
//忽略biPlanes 为1
byte biPlanes=file_in.readByte();
biPlanes=file_in.readByte();
//读取图像位数(用两个字节表示,只读出一个既可)
byte bitcount=file_in.readByte();
this.bitcounts=bitcount;
byte bitcount1=file_in.readByte();
int biCompression=file_in.readInt();//跳过
//读取图像数据大小
int datasize=file_in.readInt();
String dataSize=Integer.toHexString(datasize);
this.datasize=DWORD(dataSize)/1024;
//继续读取4个双字节
for(int i=1;i<=4;i++)
{int filter=file_in.readInt();}
//如果bitvount=8,则开始读取调色板数据
if(bitcounts==8)
{
int m=0,n=0,k=0;
for(int i=1;i<=256;i++)
{
bluevalue[m]=file_in.readUnsignedByte();m++;
greenvalue[n]=file_in.readUnsignedByte();n++;
redvalue[k]=file_in.readUnsignedByte();k++;
int a=file_in.readUnsignedByte();
}
for(int w=1;w<=((int)datasize*1024);w++)
{
int index=file_in.readUnsignedByte();
bluelevel[bluevalue[index]]+=1;
greenlevel[greenvalue[index]]+=1;
redlevel[redvalue[index]]+=1;
}
int skip=(int)((4-(this.width%4))*this.Length);
bluelevel[bluevalue[0]]-=skip;
greenlevel[greenvalue[0]]-=skip;
redlevel[redvalue[0]]-=skip;
}else if(bitcounts==24)
{
for(int q=1;q<=((int)datasize*1024/3);q++)
{
int bluevalue=file_in.readUnsignedByte();
bluelevel[bluevalue]++;
int greenvalue=file_in.readUnsignedByte();
greenlevel[greenvalue]++;
int redvalue=file_in.readUnsignedByte();
redlevel[redvalue]++;
}
int zero=(int)((4-(this.width%4))*this.Length);
bluelevel[0]-=zero;
greenlevel[0]-=zero;
redlevel[0]-=zero;
}
}catch(Exception e)
{
System.out.println("Error: "+e);
}
}
public Object clone()
{
Bmp_Information bmp=null;
try{
bmp=(Bmp_Information)super.clone();
}catch(CloneNotSupportedException e){
e.printStackTrace();
}
return bmp;
}
public String getType()
{ String str="Type: "+this.type;
return str;
}
public String getTotalsize()
{
String str="Totalsize: "+this.totalsize+"KB";
return str;
}
public String getOffbits()
{
String str="Offbits: "+this.offbits+"B";
return str;
}
public String getInfsize()
{
String str="Inf_Size: "+this.inf_size+"B";
return str;
}
public String getWidth()
{
String str="Width: "+this.width+"Pels";
return str;
}
public String getLength()
{
String str="Length: "+this.Length+"Pels";
return str;
}
public String getBitcounts()
{
String str="BitCounts: "+this.bitcounts+"bits";
return str;
}
public String getDatasize()
{
String str="DataSize: "+this.datasize+"KB";
return str;
}
public int[] getBluevalue()
{
return bluevalue;
}
public int[] getGreenvalue()
{
return greenvalue;
}
public int[] getRedvalue()
{
return redvalue;
}
public int[] getBluelevel()
{
return bluelevel;
}
public int[] getGreenlevel()
{
return greenlevel;
}
public int[] getRedlevel()
{
return redlevel;
}
public int[] getGraylevel()
{
for(int i=0;i<256;i++)
{
if(bitcounts==8)
graylevel[i]=(redlevel[i]+greenlevel[i]+bluelevel[i])/3;
/*else if(bitcounts==24)
graylevel[i]=(int)(0.4*redlevel[i]+0.2*greenlevel[i]+0.3*bluelevel[i]);*/
}
return graylevel;
}
public static double DWORD(String str)
{
double size=0;
byte[] ch=new byte[8];
byte[] ch1=str.getBytes();
int i=0;
if(ch1.length<8)
{
for(int m=ch1.length,l=7;m>=1;m--,l--)
{
ch[l]=ch1[m-1];
i=l;
}
for(int k=0;k<i;k++)
{
ch[k]=48;
}
}else
{
ch=str.getBytes();
}
for (int n=0;n<8 ;n++ )
{
if(ch[n]=='a'||ch[n]=='A')
ch[n]=10;
else if(ch[n]=='b'||ch[n]=='B')
ch[n]=11;
else if(ch[n]=='c'||ch[n]=='C')
ch[n]=12;
else if(ch[n]=='d'||ch[n]=='D')
ch[n]=13;
else if(ch[n]=='e'||ch[n]=='E')
ch[n]=14;
else if(ch[n]=='f'||ch[n]=='F')
ch[n]=15;
else
ch[n]-=48;
}
size=(ch[6]*(Math.pow(16,7)))+(ch[7]*(Math.pow(16,6)))
+(ch[4]*(Math.pow(16,5)))+(ch[5]*(Math.pow(16,4)))
+(ch[2]*(Math.pow(16,3)))+(ch[3]*(Math.pow(16,2)))
+(ch[0]*16)+ch[1];
return size;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -