How to Resize an Image using Java

Cloudmersive
2 min readSep 16, 2022

When the size of an image isn’t quite adequate for the location we want to display it online, we can take steps to change the size of the image to accommodate our needs. An easy way to accomplish this change at scale is by utilizing our Image Resizing API, which will give you the option to specify the new width & height of the output image based on your needs. It’s important to note, however, that creating much larger versions of your image will result in steady decreases in quality.

This API is free to use when you register a free account on our website (free accounts supply a limit of 800 API calls per month with zero additional commitments). With your API key in hand, you can then quickly & easily structure your API call using the ready-to-run Java code examples provided below in this article.

Our first step is to install the API client. Let’s add a reference to the repository in pom.xml:

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

And then let’s add one to the dependency in pom.xml:

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

With installation complete, we can shift focus over to our controller & add the imports to the top of our file:

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

Last but not least, we can call the image processing function. The Width and Height parameters require an integer value, and the imageFile parameter simply calls for your file path:

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");
ResizeApi apiInstance = new ResizeApi();
Integer width = 56; // Integer | Width of the output image - final image will be exactly this width
Integer height = 56; // Integer | Height of the output image - final image will be exactly this height
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.resizeResizeSimple(width, height, imageFile);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling ResizeApi#resizeResizeSimple");
e.printStackTrace();
}

With that, you’re all finished — no more code required.

--

--

Cloudmersive

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