When writing a Tiff file using the standard ImageIO API in Java, you must initialize a TIFFImageWriter
object.
ImageWriter writer = ImageIO.getImageWritersByFormatName("TIFF").next();
The compression type is set at the TIFFImageWriteParam
object, returned from TIFFImageWriter
.
ImageWriteParam param = writer.getDefaultWriteParam();
You can list the available compression types as an array of Strings with the following method. 1
param.getCompressionTypes();
The available compression types for Tiff are:
- CCITT RLE
- CCITT T.4
- CCITT T.6
- LZW
- JPEG
- ZLib
- PackBits
- Deflate
- Exif JPEG
If you need CCITT Group 3, you can choose CCITT T.4. For CCITT Group 4, use CCITT T.6. Please pay attention to the other names a compression type is also known.
To set CCITT Group 3 compression type:
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionType("CCITT T.4");
Now, pass the TIFFImageWriter
created as the third parameter when using javax.imageio.ImageWriter.write()
for single page Tiff, or as the second parameter when using javax.imageio.ImageWriter.writeToSequence()
while creating multi-pages Tiff.
TIFF uses lossless compression 2, so ImageWriteParam.getCompressionQuality()
will always return the highest quality value.