How to compress images using java?

From pagepeed I only get the link to the image and possible optimizations in bytes and percentages like Compressing and resizing https://example.com/...ts/xyz.jpg?036861 can save 212KiB (51% reduction ). Compressing https://example.com/...xyz.png?303584508 could save 4.4KiB (21% reduction).

For example, I have a 300kb image and for this image the page speed displays 100kb and 30% reduction.

This is only for one image, but I'm sure I will have a lot of images to compress. so how can i compress the image by passing bytes or percentages as a parameter or using any other calculation in java (using API or image processing tool), so i can get a compressed version of the image as suggested by google.

Thanks in advance.

+3


source to share


2 answers


You can use Java ImageIO

to do compression for many image formats, here is an example

import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Iterator;
import javax.imageio.*;
import javax.imageio.stream.*;

public class Compresssion {

  public static void main(String[] args) throws IOException {

    File input = new File("original_image.jpg");
    BufferedImage image = ImageIO.read(input);

    File compressedImageFile = new File("compressed_image.jpg");
    OutputStream os = new FileOutputStream(compressedImageFile);

    Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("jpg");
    ImageWriter writer = (ImageWriter) writers.next();

    ImageOutputStream ios = ImageIO.createImageOutputStream(os);
    writer.setOutput(ios);

    ImageWriteParam param = writer.getDefaultWriteParam();

    param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
    param.setCompressionQuality(0.05f);  // Change the quality value you prefer
    writer.write(null, new IIOImage(image, null, null), param);

    os.close();
    ios.close();
    writer.dispose();
  }
}

      

You can learn more about this

There are also some third party tools like

EDIT: If you want to use Google PageSpeed

in your application, it is available as a web server for Apache or Nginx, you can find how to configure it for your site here



https://developers.google.com/speed/pagespeed/module/

But if you want to integrate a PageSpeed

C ++ library into your application, you can find the building instructions for that here.

https://developers.google.com/speed/pagespeed/psol

There is also a Java client here

https://developers.google.com/api-client-library/java/apis/pagespeedonline/v1

+6


source


There is color compression ("compression quality"), and there is resolution compression ("resizing"). Fujy's answer is about compression quality, but that's not where the main savings come from: the main savings come from resizing to smaller. For example. I got a 4MB photo up to 207K using maximum compression quality using fujy's answer and it looked awful, but I got it up to 12KB using a reasonable quality but smaller size.

So the above code should be used for "compression quality", but this is my recommendation for resizing:



https://github.com/rkalla/imgscalr/blob/master/src/main/java/org/imgscalr/Scalr.java

I want resizing to be part of the Java standard libraries, but it doesn't seem to be the case (or are there image quality issues with standard methods?). But Riyadh's library is very small - it's just one class. I just copied this class to my project because I never really learned how to use Maven and it works great.

+1


source







All Articles