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 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