Programmatically Reduce JPEG File Size

Apologies for any ignorance, but I've never worked with jpeg images (not to mention any types of images) in Java before.

Suppose I want to send a jpeg image from a web service to a client. Is there a way to reduce the size of the jpeg file by somehow manipulating the color profile of the image?

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

I will also like jpeg, which has fewer colors than high quality jpeg image. For example, I would like to be able to use 8-bit color in jpeg instead of 16-bit color.

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

+8
java graphics jpeg 2d
source share
4 answers

Another way to reduce image size is to change the compression level. You can do this with 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); 
+4
source share

The easiest way to do this is to unzip the byte stream into a Java image, resize it at will (making it smaller), and then restore the JPEG image with the desired quality setting.

This new image is what is sent to the client.

+1
source share

Take a look at the ImageIO class. As for reducing file size: since the image will already be JPEG, the only thing you can do is reduce the quality or size of the image.

Another thing to keep in mind: if the image is a CMYK jpeg, it may be larger. Unfortunately, ImageIO cannot handle them, but you can try JAI ImageIO convert from CMYK to RGB (which should be much less).

+1
source share

Two possible solutions are zooming out, here's how you do it:

 BufferedImage original = //your image here scaled = original.getScaledInstance(finalWidth, finalHeight, Image.SCALE_SMOOTH); // scale the image to a smaller one BufferedImage result = new BufferedImage(finalWidth, finalHeight, original.getType()); Graphics2D g = result.createGraphics(); g.drawImage(scaled, 0, 0, null); //draw the smaller image g.dispose(); 

Obviously, you need to calculate the scaled width and height so that the image remains at the same aspect ratio.

Once you draw it less, now you can turn this image into a JPEG file:

 BufferedImage image = // this is the final scaled down image JPEGImageEncoder jpegEncoder = JPEGCodec.createJPEGEncoder(output); JPEGEncodeParam jpegEncodeParam = jpegEncoder.getDefaultJPEGEncodeParam(image); jpegEncodeParam.setDensityUnit(JPEGEncodeParam.DENSITY_UNIT_DOTS_INCH); jpegEncodeParam.setXDensity(92); jpegEncodeParam.setYDensity(92); jpegEncodeParam.setQuality( 0.8F , false); jpegEncoder.encode(image, jpegEncodeParam); 

These classes belong to the JAI package (more precisely, com.sun.image.codec.jpeg ), and the JVM may complain that they should not be used directly, but you can ignore it.

You can download JAI from here if it doesn’t work, I have github mirrors setup for two libraries, JAI core and JAI ImageIO .

+1
source share

All Articles