JPG is a well-known and one of the most commonly used image formats. However, JPG images lose quality each time they are saved. Therefore, you may need to convert them into a lossless format such as PNG. To perform this conversion programmatically, this article shows how to convert a JPG image to PNG in Java.
Java API for JPG to PNG Conversion
Aspose.Imaging for Java is a feature-rich image processing API that supports the manipulation of popular image formats. Moreover, it lets you convert images from one format to another seamlessly. In this article, we will use this API to convert our JPG images to PNG format. You can either download the API’s JAR or install it using the following Maven configuration in pom.xml.
<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java API</name>
<url>http://repository.aspose.com/repo/</url>
</repository>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-imaging-java</artifactId>
<version>21.12</version>
<classifier>jdk16</classifier>
</dependency>
Convert a JPG Image to PNG in Java
It is quite straightforward to convert a JPG image to PNG format using Aspose.Imaging. You only need to load the JPG file and save it as a PNG image. The following are the steps to convert a JPG image to PNG in Java.
- Load the JPG file using Image class.
- Create an instance of PngOptions class.
- Set color type using PngOptions.setColorType() method.
- Convert JPG image to PNG using Image.Save(string, PngOptions) method.
The following code sample shows how to convert a JPG image to PNG format.
Get a Free API License
You can get a free temporary license to use Aspose.Imaging for Java without evaluation limitations.
Conclusion
JPG to PNG conversion is often performed to convert images to a lossless format. In this article, you have learned how to convert JPG images to PNG format programmatically in Java. Thus, you can easily embed JPG to PNG conversion into your Java applications. In addition, you can explore Java image processing API using its documentation. Also, you can share your queries on our forum.