Home
Java
Apache
Compress
HttpClient
Collections
Date and Time
PDF
Apache PdfBox
iText
Image
JAX-RS (REST)
JAX-WS (SOAP)
JSON
Google GSON
Mail
Servlet
Utilities
XML
JavaScript
Spring Framework
Spring Boot
Spring Core
Spring Cloud
Spring Data
Spring JMS
Spring Kafka
Spring LDAP
Spring Mail
Spring Mobile
Spring MVC
Spring Security
Spring WS
Database
Hibernate
MySQL
Redis
JSF
Logging
Log4j
Logback
Testing
JUnit
Mockito
Selenium
Build Tools
Maven
Photoshop
Diggs Programming - Loves Java
HOME » JAVA » IMAGE » COMPRESS IMAGES JAVA EXAMPLE
Compress Images Java Example
BY MEMORYNOTFOUND · OCTOBER 24, 2017
DISCOVER MORE ARTICLES
Create ZIP File Using try with Resources in Java
JSF Validate Required Field Example
JSF Component listener with @ListenerFor
Caching Netflix Hystrix Request Executions Example
Passing variables in JSF Flash Scope
This tutorial shows how to compress images in Java. You can
reduce an image file size by compressing an image. This file
reduction sometimes leads to increased performance and
decreased network bandwidth. But when you compress an image
you’ll loose some information/quality of the image.
Compress Images Java Example
The following example demonstrates the use of ImageWriteParam
class to compress an image. We obtain the ImageWriteParam by
calling the ImageWriter.getDefaultWriteParam() . Next we need
to check if the image can be compressed by calling the
ImageWriteParam.canWriteCompressed() . Note: this’ll return
false when reading PNG images. We set the compressionMode to
ImageWriteParam.MODE_EXPLICIT and the compressionQuality
to 0.5f .
ImageWriteParam.MODE_DISABLED – when this mode is set the
stream will not be tiled, progressive, or compressed..
ImageWriteParam.MODE_DEFAULT – when this mode is enabled
the stream will be tiled, progressive, or compressed according
to a sensible default chosen internally by the writer in a plug-in
dependent way.
ImageWriteParam.MODE_EXPLICIT – when this mode is set the
stream will be tiled or compressed according to additional
information supplied to the corresponding set methods in this
class. Note that this mode is not supported for progressive
output.
ImageWriteParam.MODE_COPY_FROM_METADATA – when this
mode is enabled the stream will be tiled, progressive, or
compressed based on the contents of stream and/or image
metadata passed into the write operation.
package com.memorynotfound.image;
import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageOutputStream;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class CompressImages {
public static void main(String... args) throws IOEx
File input = new File("/tmp/duke.jpg");
BufferedImage image = ImageIO.read(input);
File output = new File("/tmp/duke-compressed-00
OutputStream out = new FileOutputStream(output)
ImageWriter writer = ImageIO.getImageWritersBy
ImageOutputStream ios = ImageIO.createImageOutp
writer.setOutput(ios);
ImageWriteParam param = writer.getDefaultWriteP
if (param.canWriteCompressed()){
param.setCompressionMode(ImageWriteParam.MO
param.setCompressionQuality(0.05f);
writer.write(null, new IIOImage(image, null, nu
out.close();
ios.close();
writer.dispose();
Converted Images
To demonstrate the result, we added the following table. The
image on the left is the original. The second compressed image has
a quality factor of 0.05f . The last compressed image has a quality
factor of 0.005f . Notice the different file sizes of each image.
Consequently the lower you set the quality factor, the lower the
image quality becomes but also the lower the file size becomes.
duke.jpg (original) duke-compressed- duke-compressed-
05.jpg 005.jpg
Size: 12KB Size: 9KB Size: 4KB
duke.jpg (original) duke-compressed- duke-compressed-
05.jpg 005.jpg
References
ImageWriter JavaDoc
ImageIO JavaDoc
ImageOutputStream JavaDoc
ImageWriteParam JavaDoc
IIOImage JavaDoc
Tags: Compression Image Java JPG
YOU MAY ALSO LIKE...
Netflix Hystrix Reactive Create Public Private Get The First Day Of The
RxJava Execution KeyStore for Client and Month From Date Object
Command Example Server JANUARY 24, 2015
OCTOBER 17, 2017 MARCH 29, 2016
Subscribe
Be the First to Comment!
0 COMMENTS
Subscribe to our mailing list
email address Subscribe
Privacy policy
Cookie Policy
Terms of use
© Copyright Memorynotfound.com 2015-2021