How to Convert an Image to PNG Format with Java
2 min readApr 28, 2022
If you’re looking for a whole bunch of Image file format conversion APIs, you’ve come to the right place. Try starting out with our PNG File Conversion API and implement easily using ready-to-run code snippets provided below in Java (installation with Maven).
To begin Java SDK installation with Maven, first add a reference to the repository in pom.xml:
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
After that, add one to the dependency:
<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v4.25</version>
</dependency>
</dependencies>
Now you can add the import classes, include your Cloudmersive API key (obtainable by creating a free account on our website Home — Cloudmersive APIs), and call the function:
// Import classes:
//import com.cloudmersive.client.invoker.ApiClient;
//import com.cloudmersive.client.invoker.ApiException;
//import com.cloudmersive.client.invoker.Configuration;
//import com.cloudmersive.client.invoker.auth.*;
//import com.cloudmersive.client.ConvertApi;ApiClient defaultClient = Configuration.getDefaultApiClient();// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
Apikey.setApiKey("YOUR API KEY");
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
//Apikey.setApiKeyPrefix("Token");ConvertApi apiInstance = new ConvertApi();
File imageFile = new File("/path/to/inputfile"); // File | Image file to perform the operation on. Common file formats such as PNG, JPEG are supported.
try {
byte[] result = apiInstance.convertToPng(imageFile);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling ConvertApi#convertToPng");
e.printStackTrace();
}