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

该博客展示了一段Java代码,用于处理BMP文件。代码中定义了preresult类,包含读取BMP文件、检查像素和长度有效性、获取文件头和主体等方法,最后将处理结果写入文件。整体围绕Java对BMP文件的处理展开。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本人刚接触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/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/10294527/viewspace-125492/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值