ITPub博客

首页 > Linux操作系统 > Linux操作系统 > EMS图片格式转换(Java)程序(转)

EMS图片格式转换(Java)程序(转)

原创 Linux操作系统 作者:jcszjswkzhou 时间:2019-05-08 22:42:06 0 删除 编辑
本人刚接触ems,还有很多地方不明
这个程序对16*16,32*32,72*14有效,可直接得到要发送的byte[140],40*35还没测试,本着公开源码的精神,献丑了

//victorachc@163.com
import java.io.*;

public class preresult{
//pos is the pixel data start postion
private int pos;
//bitmap's width and height
private byte w;
private byte h;
//bmp is byte[] of the bmp file
private byte[] bmp;
//result is the byte[] which save the result
private byte[] result = new byte[140];
//result[] length
private int rlen;

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;lhfor(int i=bmp.length-(lh+1)*wid;iresult[pos] = bmp[i];
pos++;
}
}
}
else if(pos==6){
wid = wid+3;
for(int lh=0;lhfor(int i=bmp.length-(lh+1)*wid;iif(iresult[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结构! 的帖子,不正确的地方,请指教!


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10294527/viewspace-125492/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2007-08-29

  • 博文量
    2942
  • 访问量
    2167548