old style JPEG-in-TIFF  格式转换

import java.awt.Point;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Vector;

public class processJPEG2TIFF {
    private FileInputStream imgTIFF=null;
    private byte[] bytesTIFF=null;
    private int qdeImagens=0;
    private Vector pontosIMG=null;
    private boolean FileExiste=false;
   
    public processJPEG2TIFF() {
     
    }
    
   
    public boolean setArqTIFF(String file){
      try{
     
      imgTIFF = new FileInputStream(file);
    int tam=imgTIFF.available();
    System.out.println(tam);
    bytesTIFF=new byte[tam];
    imgTIFF.read(bytesTIFF,0,tam);
  
      }catch(IOException erro){
      System.out.println(erro.toString());
      }
      pontosIMG=new Vector();
      FileExiste=getPosicaoJPEG();
      return FileExiste;
    }
    
   
  public boolean isExisteJPEG(){
  return FileExiste;
  }    
    
   
    private boolean getPosicaoJPEG(){
   
    int inicio=0;int fim=0;String ant="";
    Vector imgsPontos=new Vector();
   
    for(int i=0;i
     
    String atual=paraStringHexaByte(bytesTIFF[i]); 
    atual=atual.toUpperCase();
     
    if(ant.equals("FF") && atual.equals("D8")){
   
    String pos1=paraStringHexaByte(bytesTIFF[i+1]).toUpperCase();
    String pos2=paraStringHexaByte(bytesTIFF[i+2]).toUpperCase();
   
    if(pos1.equals("FF") && pos2.equals("E0") )
    {   
    System.out.println(pos1+"-"+pos2);
    inicio=i-1;ant=null;
    }
    else ant=null;
    }
    else if(inicio!=0 && ant.equals("FF") && atual.equals("D9")){ 
       
         fim=i-1;ant=null;
       
     }
    //--------------------------------------------------------------------  
     
    ant=atual;
     
    if(inicio!=0 && fim!=0){
    imgsPontos.add(new Point(inicio,fim));
    inicio=0;fim=0;ant="";atual="";
    };
     
    }
   
    if(imgsPontos.size()>0){ 
    pontosIMG=imgsPontos;
    return true;
        } else return false;
   
    }
    
   
     private String paraStringHexaByte(byte bytes) {
        StringBuilder s = new StringBuilder();
        
            int parteAlta = ((bytes >> 4) & 0xf) << 4;
            int parteBaixa = bytes & 0xf;
            if (parteAlta == 0) s.append('0');
            
            String strHex=Integer.toHexString(parteAlta | parteBaixa);
            s.append(strHex);
 
        return s.toString();
    }
    
 
private byte[] getJPEGinTIFF(byte[] imgBytes,int setI,int setF){
 ByteArrayOutputStream bytesExtraidos=null;
      try{  
bytesExtraidos = new ByteArrayOutputStream();
bytesExtraidos.write(imgBytes,setI,setF-setI);
 }catch(Exception e){System.out.println(e.toString());}
 
 return bytesExtraidos.toByteArray();
    }
    
 
   public byte[] getJPEG(int numPagina){
   
     if(numPagina>=0 && pontosIMG.size()>0){
      byte[] temp=getJPEGinTIFF(bytesTIFF,
                    ((Point)pontosIMG.elementAt(numPagina)).x,
                    ((Point)pontosIMG.elementAt(numPagina)).y
                  );
      return temp;             
     }else return null;
     
   }
   
  
   public void salvarJPEG(int numPagina){
    try{
   
     FileOutputStream saida=new FileOutputStream("d:\\JPEG.jpg");
     saida.write(getJPEG(numPagina));
     saida.close();
     
    }catch(IOException e){System.out.println(e.toString());}
   
   }  
   
 
     private int getQdeImagens(){
      return pontosIMG.size();
     }
   
 
 public static void main(String args[]){
 
   processJPEG2TIFF teste =new processJPEG2TIFF();
   //此处可以判断是否为JPEG格式.如果不是.你可以使用JIMI或其它方式来转TIF图片
   boolean result=teste.setArqTIFF("d:\\JPGE.tif");
   
   System.out.println(teste.getQdeImagens());
   if(result==true) teste.salvarJPEG(0);
   
 }
 
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ma_xs

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值