In various scenarios, the images are compressed to reduce their file size. For this, different online or offline applications allow you to apply image compression. However, in certain cases, you may need to implement the image compression feature from within your web or desktop applications. For such cases, this article covers how to compress images including PNG, JPEG, and TIFF programmatically using Java.
- Java Image Compression API
- Compress PNG Images in Java
- Compress JPEG Images in Java
- Apply Compression to TIFF Images in Java
- Get a Free API License
Info: Aspose recently developed a new online web app that allows you to transform a set of images into a video. You may also be interested in another service that can be used to compress videos by extracting the sounds from videos.
Java Image Compression API
Aspose.Imaging for Java is a powerful API that allows you to perform various image processing operations from within the Java applications. Furthermore, it lets you compress PNG, JPEG, and TIFF images seamlessly. In order to use the API, you can either download its JAR or install it within your Maven-based applications.
<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>
Compress PNG Images in Java
PNG format supports a lossless compression that means compressing a PNG image will reduce its file size but not the quality. For PNG images, Aspose.Imaging for Java lets you set compression levels from 0 to 9 where 0 and 9 are minimum and maximum compression levels respectively. The following are the steps to compress a PNG image using Java.
- First, load the PNG image using the Image class.
- Create an instance of PngOptions class.
- Set compression level using PngOptions.setCompressionLevel(int) method.
- Finally, save the compressed PNG image using Image.save(String, PngOptions) method.
The following code sample shows how to compress a PNG image using Java.
Compress JPEG Images using Java
For JPEG image compression, Aspose.Imaging for Java provides the following compression types.
- Baseline
- Progressive
- Lossless
- JpegLs
The following are the steps to apply one of the above-mentioned compression types to JPEG images using Java.
- First, load the JPEG image using the Image class.
- Create an instance of JpegOptions class.
- Set compression level using JpegOptions.setCompressionType(JpegCompressionMode) method.
- Finally, save the compressed JPEG image using Image.save(String, JpegOptions) method.
The following code sample shows how to compress JPEG images using Java.
Compress TIFF Images using Java
Aspose.Imaging for Java provides a variety of TIFF compression types that include LZW, Packbits, CCIT Fax 3 & 4 and etc. Therefore, you may select the compression type that best suits your requirement. The following are the steps to perform TIFF compression using Java.
- First, load the TIFF image using the Image class.
- Create an instance of TiffOptions class.
- Set compression level using TiffOptions.setCompression(TiffCompressions) method.
- Set other options such as bits per sample, photometric, palette, etc.
- Finally, save the compressed TIFF image using Image.save(String, TiffOptions) method.
The following code sample shows how to compress TIFF images using Java.
Get a Free API License
In case you want to try the API without evaluation limitations, you can get a free temporary license.
Conclusion
Image compression is often required in order to reduce the size of the images. Therefore, this article covered how to compress PNG, JPEG, and TIFF images programmatically using Java. Furthermore, you can explore more about the Java image processing API using documentation.