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);
}
}