This image size reducer library offers enterprise-level compression techniques to compress images. In fact, 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 library installation
- Reduce PNG image size programmatically
- Compress TIFF images in Java
Image size reducer library installation
The installation of any library matters when it comes to the 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
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:
- Load the source PNG image file by calling the Image.load method.
- Loop over possible CompressionLevel range.
- Create an instance of PngOptions class for each resultant PNG to access the PNG options.
- Set CompressionLevel. Whereas, The png image compression level in the 0-9 range, where 9 is maximum compression and 0 is store mode.
- Save results on disk by calling the save method.
Copy & paste the following code into your main file:
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:
- Invoke the image.load method to load an image through file path location or stream.
- Create an instance of TiffOptions for the resultant image.
- Set BitsPerSample, Photometric mode & Compression mode by calling the setBitsPerSample method.
- Call the setCompression method to set the compression.
- Invoke the setPhotometric method to set the photometric.
- Set grayscale palette by calling the setPalette method.
- Save the output.
You can see the output in the image below:
Get a Free License
You can avail a free temporary license to try the API without evaluation limitations.
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 really 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.
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.