How to Reduce Image Size in Java

How to Reduce Image Size in Java

This image size reducer library offers enterprise-level compression techniques to compress images. You can customize your API calls per your business requirements using various compression types and levels provided by this rich-featured library. Therefore, let’s set up this library in our Java project and build an online image compressor. This tutorial will teach us how to reduce image size in Java programmatically.

The following points will be covered:

Image Size Reducer - Java Library Installation

The installation of any library matters when it comes to rapid application development. Anyway, you can download the JAR file or install this library using the following Maven configurations.

<repository>
    <id>AsposeJavaAPI</id>
    <name>Aspose Java API</name>
    <url>https://repository.aspose.com/repo/</url>
</repository>
<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-imaging</artifactId>
    <version>20.12</version>
    <classifier>jdk16</classifier>
</dependency>

Reduce PNG Image Size Programmatically in Java

There is a huge stack of methods offered by this library that you may find here. In addition, you may set the color, compression type and many other attributes of images programmatically.

Please follow the following steps and the code snippet to compress PNG images:

  1. Load the source PNG image file by calling the Image.load method.
  2. Loop over possible CompressionLevel range.
  3. Create an instance of PngOptions class for each resultant PNG to access the PNG options.
  4. Set CompressionLevel. Whereas, The png image compression level in the 0-9 range, where 9 is maximum compression and 0 is store mode.
  5. Save results on disk by calling the save method.

Copy & paste the following code into your main file to reduce PNG image in Java:

Compress TIFF Images in Java

Likewise, this library lets you build an online image compressor where you can reduce size of TIFF image files using a few lines of Java code.

You may follow the steps mentioned below:

  1. Invoke the image.load method to load an image through file path location or stream.
  2. Create an instance of TiffOptions for the resultant image.
  3. Set BitsPerSample, Photometric mode & Compression mode by calling the setBitsPerSample method.
  4. Call the setCompression method to set the compression.
  5. Invoke the setPhotometric method to set the photometric.
  6. Set the grayscale palette by calling the setPalette method.
  7. Save the output.

The code snippet below shows how to compress TIFF images in Java:

You can see the output in the image below:

Compress TIFF images

Get a Free License

You can get a free temporary license to try the API without evaluation limitations.

Summing up

This brings us to the end of this tutorial. You have learned how to reduce image size in Java along with some other important topics. Moreover, you have gone through how to compress TIFF images and reduce PNG image size programmatically. This blog post will help you build an online image compressor using a few lines of source code. Finally, do not skip the documentation in case you are to build an image size reducer for your business application.

Moreover, we suggest you follow our Getting Started guide.

Finally, conholdate.com is writing new blog posts. So, please stay in touch for regular updates.

Ask a question

You can let us know about your questions or queries on our forum.

FAQs

How do I resize an image in coding?

Install this Java image processing library to resize images programmatically. It offers methods to manipulate images using a few lines of source code in Java.

See Also