When writing a JPEG image, how do I control the compression level/quality?

John Zukowski

You can get the default writing parameters for a specific ImageWriter through its getDefaultWriteParam() method. The method returns an ImageWriteParam object. For JPEGs, this happens to be an instance of the javax.imageio.plugins.jpeg.JPEGImageWriteParam. To change the compression level, you need to tell the ImageWriteParam object that you want to set the compression mode explicitly: iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT). Then you set the compression quality with iwp.setCompressionQuality(floatLevel). You don't have to pick a value at random. Instead, you can ask the writer what compression quality values it supports (or how much it will compress things) float values[] = iwp.getCompressionQualityValues().

About | Sitemap | Contact