How to convert TIFF to JPEG/PNG in java

  1. Add dependency

     <dependency>
     <groupId>com.github.jai-imageio</groupId>
     <artifactId>jai-imageio-core</artifactId>
     <version>1.3.1</version> </dependency>
    

https://mvnrepository.com/artifact/com.github.jai-imageio/jai-imageio-core

https://mvnrepository.com/artifact/com.github.jai-imageio/jai-imageio-core/1.3.1

  1. Coding

    final BufferedImage tif = ImageIO.read(new File("test.tif"));
    ImageIO.write(tif, "png", new File("test.png"));
    

Had gone through some study and testing, found a method to convert TIFF to JPEG and sorry for pending so long only uploaded this answer.

SeekableStream s = new FileSeekableStream(inFile);
TIFFDecodeParam param = null;
ImageDecoder dec = ImageCodec.createImageDecoder("tiff", s, param);
RenderedImage op = dec.decodeAsRenderedImage(0);

FileOutputStream fos = new FileOutputStream(otPath);
JPEGEncodeParam jpgparam = new JPEGEncodeParam();
jpgparam.setQuality(67);
ImageEncoder en = ImageCodec.createImageEncoder("jpeg", fos, jpgparam);
en.encode(op);
fos.flush();
fos.close();

otPath is the path that you would like to store your JPEG image. For example: "C:/image/abc.JPG";
inFile is the input file which is the TIFF file

At least this method is workable to me. If there is any other better method, kindly share along with us.

EDIT: Just want to point out an editing in order to handle multipage tiff. Obviously you also have to handle the different names for the resulting images:

for (int page = 0; page < dec.getNumPages(); page++) { 
    RenderedImage op = dec.decodeAsRenderedImage(page );
    ...
}

In case of many pages, working following:

  1. add dependency:

    <dependency>
        <groupId>com.github.jai-imageio</groupId>
        <artifactId>jai-imageio-core</artifactId>
        <version>1.4.0</version>
    </dependency>
    
  2. use following Java8 code

    public void convertTiffToPng(File file) {
    try {
        try (InputStream is = new FileInputStream(file)) {
            try (ImageInputStream imageInputStream = ImageIO.createImageInputStream(is)) {
                Iterator<ImageReader> iterator = ImageIO.getImageReaders(imageInputStream);
                if (iterator == null || !iterator.hasNext()) {
                    throw new RuntimeException("Image file format not supported by ImageIO: " + file.getAbsolutePath());
                }
    
    
                // We are just looking for the first reader compatible:
                ImageReader reader = iterator.next();
                reader.setInput(imageInputStream);
    
                int numPage = reader.getNumImages(true);
    
                // it uses to put new png files, close to original example n0_.tiff will be in /png/n0_0.png
                String name = FilenameUtils.getBaseName(file.getAbsolutePath()); 
                String parentFolder = file.getParentFile().getAbsolutePath();
    
                IntStream.range(0, numPage).forEach(v -> {
                    try {
                        final BufferedImage tiff = reader.read(v);
                        ImageIO.write(tiff, "png", new File(parentFolder + "/png/" + name + v + ".png"));
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                });
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    }
    

Tags:

Java

Image

Tiff

Jai