/***
*@paramimageFileList 图片路径列表 (图片为E:/xx/xx.jpg格式)
*@paramtoPath tif文件所放路径
*@paramdistFileName tif文件名
*@paramconvertPath 转换图片存放路径
*@paramisCompress 是否压缩(压缩的时候是先将不是jpg的格式转为jpg,因为JAI可以设置JPEG的压缩模式。
* 但是png转jpg会有点失真,所以建议用非压缩的方式!!!)*/
public static String manyImgToTif(List imageFileList,String toPath, String distFileName, String convertPath, booleanisCompress) {
String tifFile= null;if(imageFileList != null && imageFileList.size()>0){
List fileArr = new ArrayList();
String fileName;
String imageFile;
String fileType= "";
File tmpFile ;
FileInputStream tmpIns= null;boolean isNeedTransfer = false;for (int i = 0; i < imageFileList.size(); i++){
imageFile=imageFileList.get(i);
String[] tempFile= imageFile.split("/");
fileName= tempFile[tempFile.length-1];
tmpFile= newFile(imageFile);//处理文件
try{//以防是png格式的图片直接改后缀的情况
tmpIns = newFileInputStream(tmpFile);
fileType=getPicType(tmpIns);if(isCompress){
File convertFile= newFile(convertPath);if (!convertFile.exists()) {
convertFile.mkdirs();
}if ("bmp".equals(fileType)|| "jpg".equals(fileType)){//有时图片有损坏,但是能展示,就是用这个架包的时候会报错,//比较严格,所以需要捕获异常做一下转换
isNeedTransfer = false;try{//需要重新获取流,因为上面判断格式已经读过流了.
tmpIns = newFileInputStream(tmpFile);
JPEGImageDecoder decoder=JPEGCodec.createJPEGDecoder(tmpIns);
decoder.decodeAsBufferedImage();
}catch(TruncatedFileException e){
e.printStackTrace();
logger.info("图片有损坏,需要做转换,image:"+imageFile);
isNeedTransfer= true;
}if(isNeedTransfer){
fileArr.add(pngtoJpg(imageFile,convertPath,fileName));
}else{
fileArr.add(newFile(imageFile));
}
}else if("gif".equals(fileType)){
fileArr.add(giftoJpg(imageFile,convertPath,fileName));
}else if("png".equals(fileType)){
fileArr.add(pngtoJpg(imageFile,convertPath,fileName));
}
}else{if ("bmp".equals(fileType)|| "jpg".equals(fileType)|| "gif".equals(fileType)|| "png".equals(fileType)){
fileArr.add(newFile(imageFile));
}
}
}catch(Exception e) {
e.printStackTrace();
}finally{if(tmpIns != null){try{
tmpIns.close();
}catch(IOException e) {
e.printStackTrace();
}
}
}
}if (fileArr.size() > 0) {try{
ArrayList pages= new ArrayList(fileArr.size() - 1);
FileSeekableStream[] stream= newFileSeekableStream[fileArr.size()];for (int i = 0; i < fileArr.size(); i++) {
stream[i]= newFileSeekableStream(fileArr.get(i).getCanonicalPath());
}
ParameterBlock pb= (newParameterBlock());
PlanarImage firstPage= JAI.create("stream", stream[0]);for (int i = 1; i < fileArr.size(); i++) {
PlanarImage page= JAI.create("stream", stream[i]);
pages.add(page);
}
TIFFEncodeParam param= newTIFFEncodeParam();if(isCompress){
param.setCompression(TIFFEncodeParam.COMPRESSION_JPEG_TTN2);
}
TIFFField[] extras= new TIFFField[4];
extras[0] = new TIFFField(262, TIFFField.TIFF_SHORT, 1, (Object) new short[] { 6});
extras[1] = new TIFFField(282, 5, 1, (Object) new long[][]{{(long) 200, 1}, {0, 0}});
extras[2] = new TIFFField(283, 5, 1, (Object) new long[][]{{(long) 200, 1}, {0, 0}});
extras[3] = new TIFFField(258, TIFFField.TIFF_SHORT, 1, (Object) new char[] { 8});
param.setExtraFields(extras);
param.setExtraImages(pages.iterator());
File f= newFile(toPath);if (!f.exists()) {
f.mkdirs();
}
tifFile= toPath + "\\"+ distFileName+".tif";
OutputStream os= newFileOutputStream(tifFile);
ImageEncoder enc= ImageCodec.createImageEncoder("tiff", os, param);
enc.encode(firstPage);//关掉流
os.flush();
os.close();for (int i = 0; i < fileArr.size(); i++) {
stream[i].close();
}
logger.info("====manyImgToTif done====");
}catch(IOException e) {
e.printStackTrace();
}
}
}returntifFile;
}