国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

EMS圖片格式轉(zhuǎn)換(Java)程序

2019-11-18 20:09:05
字體:
供稿:網(wǎng)友
本人剛接觸EMS,還有很多地方不明
這個(gè)程序?qū)?6*16,32*32,72*14有效,可直接得到要發(fā)送的byte[140],40*35還沒測(cè)試,本著公開源碼的精神,獻(xiàn)丑了

//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;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結(jié)構(gòu)!  的帖子,不正確的地方,請(qǐng)指教!


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 泌阳县| 杂多县| 乐业县| 石景山区| 大兴区| 宕昌县| 淄博市| 通渭县| 蓬安县| 大荔县| 英山县| 苍山县| 石林| 正宁县| 墨脱县| 恩施市| 家居| 中江县| 阿鲁科尔沁旗| 孟津县| 措勤县| 体育| 怀远县| 潮州市| 高唐县| 三台县| 墨竹工卡县| 古交市| 化隆| 图木舒克市| 萨嘎县| 寿光市| 缙云县| 清水县| 鹿邑县| 临洮县| 贺兰县| 双辽市| 德兴市| 双辽市| 建昌县|