我正在尝试创建具有多个图像文件的.tif文件。 请参考下面的代码,该代码没有任何异常或错误。 tif创建成功。将第一张图像成功写入tif文件,但未成功写入其他文件。
尝试使用writer.write(IIOMetadata streamMetadata,IIOImage图像,ImageWriteParam参数)和writer.writeInsert(int imageIndex,IIOImage图像,ImageWriteParam参数),但是两者都做相同的事情。
import javax.imageio.ImageIO;
import javax.imageio.ImageWriter;
import javax.imageio.IIOImage;
import javax.imageio.ImageWriteParam;
import javax.imageio.stream.ImageOutputStream;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Iterator;
/**
*
*
* This class is used to convert the multiple images in to single multi-pages tiff image file.
*
*/
public class MultiPageTiffGenerator {
private static String compressionType="JPEG";
public static boolean generateMultiPageTiff(String dirName,String outputFileName) throws Exception{
boolean generated=false;
ImageOutputStream ios=null;
ImageWriter writer=null;
try {
// verify the directory for the image files
File dir = new File(dirName);
if(null!=dir && !dir.isDirectory()){
throw new FileNotFoundException("No directory exists with the given name"+dirName);
}
System.out.println("verify the directory for the image files");
// verify the images files exist
File[] files = dir.listFiles();
if(null == files || files.length == 0){
throw new FileNotFoundException("No image files to process");
}else{
// Create the output file on the disk
File file = new File(dirName+outputFileName+".tif");
ios = ImageIO.createImageOutputStream(file);
// Get the appropriate Tiff Image Writer
Iterator <ImageWriter> writers=ImageIO.getImageWritersByFormatName("jpg");
if(null== writers || ! writers.hasnext()){
throw new Exception("Appropriate Tiff writer not found");
}
writer = ImageIO.getImageWritersByFormatName("jpg").next();
System.out.println("Get the writer");
writer.setOutput(ios);
System.out.println("Set the compression parameters for Tiff image");
// Set the compression parameters for Tiff image
ImageWriteParam param = writer.getDefaultWriteParam();
writer.prepareWriteSequence(null);
// Loop through all image files and write them to output tiff image
BufferedImage image ;
for (int i = 0; i < files.length; i++) {
System.out.println(writer.canWriteSequence());
System.out.println("Inside Loop-->" + files[i].getName());
InputStream fis=null;
int dotIndex= files[i].getName().lastIndexOf('.');
dotIndex=dotIndex>0?dotIndex:files[i].getName().length();
String fileName = files[i].getName().substring(0,dotIndex);
if(!fileName.equalsIgnoreCase(outputFileName)){
try{
fis = new BufferedInputStream(new FileInputStream(files[i]));
image = ImageIO.read(fis);
IIOImage img = new IIOImage(image,null,null);
writer.writeToSequence(img,param);/*tried using writer.write(IIOMetadata streamMetadata,IIOImage image,ImageWriteParam param) and writer.writeInsert(int imageIndex,ImageWriteParam param) but both does the same thing*/
image.flush();
}
catch(Exception e){
e.printStackTrace();
}
finally{
if(null!=fis){
fis.close();
}
}
}
}
writer.endWriteSequence();
ios.flush();
generated=true;
}
}catch(FileNotFoundException ex){
generated=false;
ex.printStackTrace();
}catch(IOException ex){
generated=false;
ex.printStackTrace();
}catch(Exception ex){
generated=false;
ex.printStackTrace();
}finally{
if(null!=ios)
ios.close();
if(null!=writer)
writer.dispose();
}
return generated;
}
public static void main(String[] a) {
try {
generateMultiPageTiff("C:\\Users\\user1\\Desktop\\PDFToWord\\Files\\","output");
}
catch(Exception e) {
e.printStackTrace();
}
}
}