How to change the DPI of a photo in Java

Adjusting the dots per inch of a photo can have huge value when it comes time to print or web display an image. Being able to adjust this to a common value en mass is the focus of today’s tutorial. We will be using a Cloudmersive API to painlessly complete this chore, and we will do it in under 5 minutes. Let’s get started.

First we need to edit our Maven POM file and add two references.

Repository reference:


Dependency reference:


All that’s left is to call convertImageImageSetDPI and provide it with a file and desired DPI value:

// 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.ConvertImageApi;ApiClient defaultClient = Configuration.getDefaultApiClient();// Configure API key authorization: ApikeyApiKeyAuth 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");ConvertImageApi apiInstance = new ConvertImageApi();Integer dpi = 56; // Integer | New DPI in pixels-per-inch, for example 300 DPI or 600 DPIFile inputFile = new File("/path/to/file"); // File | Input file to perform the operation on.try {byte[] result = apiInstance.convertImageImageSetDPI(dpi, inputFile);System.out.println(result);} catch (ApiException e) {System.err.println("Exception when calling ConvertImageApi#convertImageImageSetDPI");e.printStackTrace();}

That’s it! Our image will now have the desired DPI. Our APIs provide a plethora of other image-related functionality, including converting between image formats, resizing, and facial recognition, so be sure to check out our documentation section.

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

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store