1

I need to display the 3rd page of scanned tiff files. i used the code

TIFFReader reader = new TIFFReader(new File(pathOfFile));    
RenderedImage image = reader.getPage(2);    

its sometimes work. and show error : Decoding of old style JPEG-in-TIFF data is not supported. I used aspriseTIFF.jar

then how i solve this problem. please reply. thanks in advance

sree
  • 11
  • 2
  • 7
  • 1
    'new File(f1.getAbsolutePath())' is just a convoluted way of writing 'f1'. You don't need pointless complications like this. – user207421 May 14 '14 at 12:00
  • 2
    What `TIFFReader` class are you using? You could try using my `TIFFImageReader` plug-in for ImageIO, available here: https://github.com/haraldk/TwelveMonkeys It does support (some) versions of "old-style" JPEG in TIFF. If you attach some of the images that isn't supported, I could test them for you. – Harald K May 14 '14 at 12:02
  • 1
    @EJP Unless `f1` is a file-like object with a `getAbsolutePath()` method but is not an instance of `File` or a `File` subclass. – JAB May 14 '14 at 12:03
  • 1
    @EJP, JAB, I edited the question so that you can focus on the problem... – Harald K May 14 '14 at 12:05
  • 2
    @user3437103 you should not rollback edits when they are justified. – Apolo May 14 '14 at 13:18

1 Answers1

4

The problem you have run into is that "old style" JPEG compression in the TIFF format (compression == 6), is not supported in the library you use.

This is quite common I guess, as "old-style" JPEG compression is deprecated in TIFF, because it was never fully specified. And because of this under-specification, various vendors implemented it in different, incompatible ways. Support was dropped in favor for TIFF compression 7, JPEG.

Unfortunately, old TIFF files using this compression still exists, so you need to find another library. The good news is that you can use ImageIO and a proper plug-in.

Using a TIFF ImageReader plug-in, like the one from my TwelveMonkeys ImageIO open source project, you should be able to do this:

// Create input stream
try (ImageInputStream input = ImageIO.createImageInputStream(file)) {
    // Get the reader
    ImageReader reader = ImageIO.getImageReaders(input).next();

    try {
        reader.setInput(input);

        // Read page 2 of the TIFF file
        BufferedImage image = reader.read(2, null);
    }
    finally {
        reader.dispose();
    }
}

(sorry about the try/finally boiler-plate, but it is important to avoid resource/memory leaks).

Harald K
  • 26,314
  • 7
  • 65
  • 111
  • how do you know how many pages has the TIFF file? I couldn't find any example of TwelveMonkeys library to extract single pages to a JPEG or PNG format from a multi-page tif file. – delkant Jan 11 '17 at 12:44
  • 2
    Please don't use comments for questions. But... This is standard ImageIO functionaliy, and works for any `ImageReader`, simply use [`reader.getNumImages(true)`](https://docs.oracle.com/javase/7/docs/api/javax/imageio/ImageReader.html#getNumImages(boolean)). – Harald K Jan 11 '17 at 12:51
  • try-with-resource for Java7+ `try (ImageInputStream input = ImageIO.createImageInputStream(file)) { ... }` will save one `try { } finally { }` (but not the `reader.dispose()` though) – Matthieu Apr 03 '18 at 13:28