jar包准备 jai-codec和jai-core
主要过程
private boolean parseTifFile(FileItem item) {
logger.info("-------parseTifFile---start----");
try {
InputStream ins = item.getInputStream();
ImageDecodeParam imageDecodeParam = new TIFFDecodeParam();
JPEGEncodeParam param1 = new JPEGEncodeParam();
ImageDecoder imageDecoder = ImageCodec.createImageDecoder("tiff", ins, imageDecodeParam);
int size = imageDecoder.getNumPages();//tif页数
String ApplicationPath = SysVar.getVar("ApplicationPath");//ui所在目录/data/app/Claim-YS2/ui/
String des = SysVar.getVar("TiffAndJPG");//scanningCopy/folder/
String name = item.getName();//投保意向书-李帅.tif
name = name.substring(0,name.lastIndexOf(".tif"));//投保意向书-李帅
// deleteAllFiles(file);//删除image目录以删除原来可能存在的图片
for(int i = 0; i < size; i++){
RenderedImage renderedImage = imageDecoder.decodeAsRenderedImage(i);//单个图片
ParameterBlock arg = new ParameterBlock();
String despath = ApplicationPath + des + "jpg/" + this.mBatchNo + "/" + i + ".jpg";
File file = new File(despath);
if (!file.exists()) {
file.getParentFile().mkdirs();
file.createNewFile();//如果不存在,新建空图片
}
arg.addSource(renderedImage);
arg.add(file.toString());
arg.add("JPEG");
arg.add(param1);
RenderedOp r = JAI.create("filestore", arg);
r.dispose();
//保存于数据库中,这里属于lis框架的内容
T_SAVE_FILESchema tSaveFile = new T_SAVE_FILESchema();
tSaveFile.setBatchNo(this.mBatchNo);
tSaveFile.setName(name + "_" + i + ".jpg");//xxx_0.jpg
tSaveFile.setUrl(despath);
tSaveFile.setFileType("JPEG");
tSaveFile.setMakeDate(PubFun.getCurrentDate());
tSaveFile.setMakeTime(PubFun.getCurrentTime());
this.tMap.put(tSaveFile, "DELETE&INSERT");
}
} catch (Exception ex) {
ex.printStackTrace();
this.tCErrors.addOneError(ex.getMessage());
return false;
}
logger.info("-------parseTifFile----end----");
return true;
}
循环删除某个文件夹下的所有文件
public static void deleteAllFiles(File file){
if(file == null || !file.exists())
return ;
if(file.isDirectory()){
File[] files = file.listFiles();
if(files != null){
for(File f : files)
deleteAllFiles(f);
}
}
file.delete();
}