protected preresult(string fn) throws ioexception {
fileinputstream fin = new fileinputstream(fn);
//read bmpdata to byte array bmp
int bmplen = fin.available();
bmp = new byte[bmplen];
fin.read(bmp);
w=bmp[18];
h=bmp[22];
fin.close();
}
//check if it is 1 bit/pixel
public void isvalid() throws exception {
if(bmp[28]==1) return;
else throw new exception("pixel is not 1");
}
//check if the bmp length is valid
public boolean checkdatalen() throws exception {
int len=(bmp[2]-bmp[10])>=0?(bmp[2]-bmp[10]):(bmp[2]-bmp[10]+256);
if (len==(w*h/8)){
return true;
}
else if(len*3/4==w*h/8){
system.out.println("this is variable bmp!");
return true;
}
else throw new exception("length is invalid");
}
public void gethead() throws exception {
if(w==16&&h==16){
result[0]=35;
result[1]=17;
result[2]=33;
result[3]=0;
pos=4;
rlen=36;
return;
}
else if(w==32&&h==32){
result[0]=-125;
result[1]=16;
result[2]=-127;
result[3]=0;
pos=4;
rlen=132;
return;
}
else if(((w%8)==0)&&((w*h)<=1024)){
int p=w*h/8+5;
result[0]=(byte)((p<=127)?p:(p-256));
result[1]=18;
result[2]=(byte)(result[0]-2);
result[3]=0;
result[4]=(byte)(w/8);
result[5]=h;
pos=6;
rlen=p+1;
return;
}
throw new exception("header error");
}
public void getbody(){
//according to the ems specification,w is w/8
int wid=w/8;
if(pos==4){
//lh is loop height,lw is loop width
for(int lh=0;lh<h;lh++){
for(int i=bmp.length-(lh+1)*wid;i<bmp.length-lh*wid;i++){
result[pos] = bmp[i];
pos++;
}
}
}
else if(pos==6){
wid = wid+3;
for(int lh=0;lh<h;lh++){
for(int i=bmp.length-(lh+1)*wid;i<bmp.length-lh*wid;i++){
if(i<bmp.length-lh*wid-3){
result[pos] = bmp[i];
pos++;
}
}
}
}
//result has been build,you can check the byte[] here
}
public void putresultintofile() throws ioexception{
fileoutputstream fout = new fileoutputstream("test.tmp");
fout.write(result);
fout.close();
}
public static void main(string[] args) throws ioexception,exception {
preresult pr = new preresult("d:\\tom.bmp");
pr.isvalid();
try{
pr.checkdatalen();
}
catch(exception e){e.printstacktrace();}
pr.gethead();
pr.getbody();
pr.putresultintofile();
}
}
这段程序参考了斑竹:bmp结构! 的帖子,不正确的地方,请指教!
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器