Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically Reducing JPEG file size

Apologies for any ignorance, but I have never worked with jpeg images (let alone any types of images) in Java before.

Supposing I want to send a jpeg image from a web service to a client. Is there any way that I can reduce the jpeg file size by manipulating the colour profile of the image in some way?

I have already been able to reduce the image size by scaling it using a neat tool for BufferedImages called imgscalr. See here.

I would also like a jpeg that has less colours than a high quality jpeg image. For example, I would like to be able to use 8bit colour in my jpeg instead of say 16bit colour.

What exactly would I need to change if I have a BufferedImage from Java's 2D package?

like image 490
Joeblackdev Avatar asked Jul 26 '11 13:07

Joeblackdev


People also ask

How do I reduce the size of a JPEG on Android?

Compress Image Size Step 1: Download and install 'Compress image size' from the Play Store. Step 2: Open the app and choose a photo. Step 3: You can check the original image size. Enable Quality toggle and use the slider to reduce image size.


1 Answers

Another way to reduce image size is to change compression level. You can do that using ImageWriter.

    ImageWriter writer = null;
    Iterator<ImageWriter> iwi = ImageIO.getImageWritersByFormatName("jpg");
    if (!iwi.hasNext())
        return;
    writer = (ImageWriter) iwi.next();
    ImageWriteParam iwp = writer.getDefaultWriteParam();
    iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT) ;
    iwp.setCompressionQuality(compressionQuality);
    writer.setOutput(...);
    writer.write(null, image, iwp);
like image 59
Lev Khomich Avatar answered Sep 29 '22 13:09

Lev Khomich