I have tiff files and I want to get the number of pages. I have been followed this question, however I am unable to get the number of pages. There are no errors and I have tried to debug the problem but cannot find the cause.
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import javax.swing.JOptionPane;
import java.io.File;
import java.io.IOException;
import java.awt.Frame;
import java.awt.image.RenderedImage;
import javax.media.jai.widget.ScrollingImagePanel;
import javax.media.jai.NullOpImage;
import javax.media.jai.OpImage;
import com.sun.media.jai.codec.SeekableStream;
import com.sun.media.jai.codec.FileSeekableStream;
import com.sun.media.jai.codec.TIFFDecodeParam;
import com.sun.media.jai.codec.ImageDecoder;
import com.sun.media.jai.codec.ImageCodec;
public class FileSearch {
int sizeOfFile = Integer.parseInt((JOptionPane.showInputDialog(null,"Enter Size: ")));
public void findFiles(File root) throws IOException {
File[] listOfFiles = root.listFiles();
for (int i = 0; i < listOfFiles.length; i++) {
String iName = listOfFiles[i].getName();
if (listOfFiles[i].isFile() && iName.endsWith(".tif")) {
long fileSize = listOfFiles[i].length();
if (fileSize <= (sizeOfFile*1000)) {
File file = new File("\\\\server\\folder1\\TestFiles\\" + listOfFiles[i].getName());
if (file.exists()) {
System.out.println(file.getName() + " already exists");
} else {
File folder = new File("\\\\server\\folder1\\TestFiles\\\\Files by Size\\Files of size " + ((sizeOfFile*1000) / 1000) + " KB");
if (!folder.exists()) {
if (folder.mkdir()) {
System.out.println("Folder : " + folder.getAbsolutePath());
}
}
/* Start here */
SeekableStream s = new FileSeekableStream(new File("C:\\Users\\username\\Desktop\\file.tif")); //using this file as test
TIFFDecodeParam param = null;
ImageDecoder dec = ImageCodec.createImageDecoder(".tif", s, param);
System.out.println("Number of images in this TIFF: " +
dec.getNumPages());
JOptionPane.showMessageDialog(null, dec.getNumPages());
/* END HERE */
try{
System.out.println(listOfFiles[i].getName() + " | " + FileUtils.byteCountToDisplaySize(fileSize));
FileUtils.copyFileToDirectory(listOfFiles[i], folder);
}
catch(IOException e){
System.out.println("Same files");
return;
}
}
}
}
else if (listOfFiles[i].isDirectory()) {
findFiles(listOfFiles[i]);
}
}
}
}
I changed ImageDecoder dec = ImageCodec.createImageDecoder("tiff", s, param);
to ImageDecoder dec = ImageCodec.createImageDecoder(".tif", s, param);
to see for any different results.
Answer to my question :
Change the below between START TO END section