2

My goal is to modify a TIFF file. I try to read the file using ImageIO. The attempt to get an ImageReader for TIFF file leads to NoSuchElementException. Here is my code:

Iterator iterator = ImageIO.getImageReadersByFormatName("tiff");
// Next code string throws java.util.NoSuchElementException
ImageReader reader = (ImageReader) iterator.next();
ImageInputStream is = null;

try {
    is = new FileImageInputStream(new File(sourceFilePath));
    reader.setInput(is, false, true);
    int pageNum = reader.getNumImages(true);
    System.out.println(pageNum);
} catch (Exception e) {
    e.printStackTrace();
}

I would appreciate any help on this problem.

Harald K
  • 26,314
  • 7
  • 65
  • 111
Victor
  • 474
  • 5
  • 18
  • I found a previous post that could help you: http://stackoverflow.com/questions/4828757/cannot-run-or-jai-imageio-or-imageio-getimagereadersbyformatname-cannot-get-obje – Gavi Aug 19 '14 at 14:32
  • What version of JAI are you using? Verify your JAI installation. Make sure `jai-imageio.jar` (or similar) is installed and accessible at runtime (on class path). The exception indicates that you have no reader for TIFF, which again indicates you don't have JAI available. There's nothing wrong with your code. – Harald K Aug 19 '14 at 14:33
  • Hi @haraldK! I'm using JAI 1.1.3. I've downloaded jai_core.jar, jai_codec.jar and set them in my eclipse project buildpath. No other instalations should be made, because this code should be deployed on client's server and I don't have access and rghts to make installations on it. – Victor Aug 20 '14 at 07:20
  • 1
    @Victor AFAIK, unless you also have `jai_imageio.jar` installed, the JAI ImageIO plugins (including the `TIFFImageReader` and `TIFFImageWriter`) won't be available. – Harald K Aug 20 '14 at 08:07
  • 1
    @haraldK Thanks! I have added `jai_imageio.jar` to project build path and the problem was solved. It took me a lot of time to find a link to download the JAR file without actually installing JAI. The links are mentioned in the answer for this question. – Victor Aug 20 '14 at 08:35

1 Answers1

4

To make this code work without JAI installation the JAR files jai_core.jar jai_codec.jar jai_imageio.jar should be added to class path. Links for downloading:

Victor
  • 474
  • 5
  • 18