How to Change Image DPI with Java

Cloudmersive
2 min readJun 10, 2022

Dots per inch (DPI) expresses the number of printed dots present within a single inch of an image. Higher DPI Images print more sharply, so if you’re looking for crisp, top-quality prints, it’s great to have a way of manipulating that number at will. You can easily include that operation into your website or application using our Image DPI API. Below, we’ve included ready-to-run code examples to help you structure an API call in Java. If you’re looking for to make the call in a different programming language, check out our API console page and find additional code examples in 12+ common programming languages.

Let’s begin by installing Maven. Include a reference in the pom.xml repository:

<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>

Next, add in the Maven package:

<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v4.25</version>
</dependency>
</dependencies>

After that, include the import classes:

// 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;

Lastly, include the remaining code block below. You’ll need include the following parameters:

  1. The new DPI, expressed in pixels-per-inch (e.g., 300 DPI or 600 DPI).
  2. The input file to perform the operation on.
  3. A Cloudmersive API key (attainable by registering a free account on our website).
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");
ConvertImageApi apiInstance = new ConvertImageApi();
Integer dpi = 56; // Integer | New DPI in pixels-per-inch, for example 300 DPI or 600 DPI
File inputFile = new File("/path/to/inputfile"); // 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();
}

--

--

Cloudmersive

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