How to convert tiff to jpeg / png in java

I recently encountered a problem while trying to display an image file. Unfortunately, the image format is the TIFF format, which is not supported by the main web browser (since I only know Safari to support this format). Due to a certain limitation, I need to convert this format to another format supported by the main browser. However, this brings a lot of problems for me when I try to convert the format.

I had a search on the Internet, and although a similar problem was posted in this link How to convert TIF to PNG in Java? "but I can’t have the result that he proposed ..

Therefore, I raise this question again in order to wish you to have the best explanation and recommendations from all of you.

There were several problems that I encountered while going through the proposed solution:

1) In accordance with the answer proposed by Jonathan Feinberg , he needs to install JAI and JAI / ImageIO. However, after I installed both of them, I still could not import the file into Netbean 7.2. NetBean 7.2 remains to offer the default imageIO library.

2) when I use the standard method of reading the ImageIO library, it will return NULL and I can’t continue working.

3) I also tried other methods, such as converting a TIFF file to a BIN file using the BufferedOutputStream method, but the result file is larger than 11 MB, which is too large to load and as a result the download failed.

if (this.selectedDO != null) { String tempDO = this.selectedDO.DONo; String inPath = "J:\\" + tempDO + ".TIF"; String otPath = "J:\\" + tempDO + ".bin"; File opFile = new File(otPath); File inFile = new File(inPath); BufferedInputStream input = null; BufferedOutputStream output = null; try { input = new BufferedInputStream(new FileInputStream(inPath), DEFAULT_BUFFER_SIZE); output = new BufferedOutputStream(new FileOutputStream(otPath), DEFAULT_BUFFER_SIZE); byte[] buffer = new byte[DEFAULT_BUFFER_SIZE]; int length; while ((length = input.read(buffer)) > 0) { output.write(buffer, 0, length); } } finally { try { output.flush(); output.close(); input.close(); } catch (IOException e) { e.printStackTrace(); } } 

Therefore, I hope that you can get help and advise all of you so that I can convert the TIFF format to another format, for example JPEG / PNG.

+11
source share
5 answers

I went through some research and testing, found a way to convert TIFF to JPEG, and sorry to have downloaded this answer for so long.

  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(); 

p / s: otPath is for the path where you want to save the image in JPEG format. For example: "C: /image/abc.JPG"; inFile is the input file, which is a TIFF file

At least this method applies to me. If there is another best method, kindly share with us.

Thanks and respect,

+15
source
  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")); 
+7
source

The following works on many pages:

  1. add dependency:

     <dependency> <groupId>com.github.jai-imageio</groupId> <artifactId>jai-imageio-core</artifactId> <version>1.4.0</version> </dependency> 
  2. use the 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(); } } 
+5
source

First, see What is the best java image processing / processing library? . For your code you can use

 javax.imageio.ImageIO.write(im, type, represFile); 

as you can see in write an example image to a file .

0
source

If your goal is Android, you can try this wonderful Java library on Github, which provides many utilities for processing, opening and writing .tiff files.

A simple example from this Git shows how to convert TIFF to JPEG:

 TiffConverter.ConverterOptions options = new TiffConverter.ConverterOptions(); //Set to true if you want use java exception mechanism options.throwExceptions = false; //Available 128Mb for work options.availableMemory = 128 * 1024 * 1024; //Number of tiff directory to convert; options.readTiffDirectory = 1; //Convert to JPEG TiffConverter.convertTiffJpg("in.tif", "out.jpg", options, progressListener); 
-one
source

All Articles