Convert an Image to JPG, JPEG Format with Java

2 min readApr 28, 2022


There are a lot of popular Image file formats out there, and JPEG probably tops them all. With our JPEG File Conversion API, you can easily convert images from a wide variety of common image file formats into JPEG format. Below, Java code snippets & installation instructions for Maven are provided to facilitate a quick connection.

Begin by adding a reference to the repository found in pom.xml:


Then add a reference to the dependency:


Now you’re almost done — add in the import classes, include your API key where indicated, 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)
ConvertApi apiInstance = new ConvertApi();
Integer quality = 56; // Integer | Set the JPEG quality level; lowest quality is 1 (highest compression), highest quality (lowest compression) is 100; recommended value is 75
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.convertToJpg(quality, imageFile);
} catch (ApiException e) {
System.err.println("Exception when calling ConvertApi#convertToJpg");




There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.