Java how to set jpg quality

I came across a similar problem and the answer was not very clear to me, since at the time i did not have a knowledge on ImageIO, so to the people like me that came across this post i made a example.

try {
            
            //  Image to be altered 
            BufferedImage imagem = ImageIO.read(new File("c://nota.jpg"));
            
            //  The output image
            File outPutImage = new File("c://nota2.jpg");
            
            //  Encapsulate the outPut image
            ImageOutputStream  ios =  ImageIO.createImageOutputStream(outPutImage);
            
            //  List of ImageWritre's for jpeg format 
            Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName("jpeg");
            
            //  Capture the first ImageWriter
            ImageWriter writer = iter.next();
            
            //  define the o outPut file to the write
            writer.setOutput(ios);

            //  Here you define the changes you wanna make to the image
            ImageWriteParam iwParam = writer.getDefaultWriteParam();
            iwParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
            iwParam.setCompressionQuality(.90f);

            //  Compression and saving to file the altered image
            writer.write(null, new IIOImage(imagem, null, null), iwParam);
            
            writer.dispose();               
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

If you know of a easier way or you found out something wrong in my comments or code, please let me know in the comments so i can alter.


Finally did it with this code ...

try
{

    ImageOutputStream  ios =  ImageIO.createImageOutputStream(var7);
    Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName("jpeg");
    ImageWriter writer = iter.next();
    ImageWriteParam iwp = writer.getDefaultWriteParam();
    iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
    iwp.setCompressionQuality(0.85f);
    writer.setOutput(ios);
    writer.write(null, new IIOImage(var6,null,null),iwp);
    writer.dispose();

    //ImageIO.write(var6, "jpg", var7);
}

You might want to elaborate on what your actual problem with the code is.

Generally speaking, the second sniplet you were using is (more or less) the correct approach:

1) ImageIO.write(...) uses default values for pretty much everything, it requires no extra configuration.

2) If you want to tweak parameters, e.g. for the compression ratio, you should instead use an ImageWriter. You can obtain a suitable writer for any format (in your case jpg) using ImageWriter writer = ImageIO.getImageWritersByFormatName("jpg");

3) You then set the configuration parameters to be used by the writer on an instance of ImageWriteParam. You could instanciate a JPEGImageWriteParam directly, but if you're just looking to change the compression ratio it is easier to request a default instance using ImageWriteParam param = writer.getDefaultWriteParam();

4) Set the compression quality as shown in the above code snipplet, and set the compression type to explicit accordingly.

5) The call to writer.write(null, new IIOImage(image, null, null), iwparam); basically tells your writer instance to create an image without meta data or embedded thumbnails, containing nothing but your BufferedImage and using the configuration object you created in 3).

Tags:

Java