Image Processing API: Resize an Image with Java

Cloudmersive
2 min readMay 6, 2022

--

For this simple Image Resizing operation, all you need to do is specify the new width and height of the image file you want to resize. With code samples provided below, you can easily create a custom-code connection in Java and put your Image resizing needs to rest in the long term.

To begin the Java SDK installation with Maven, start by adding a reference to the repository in pom.xml:

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

Then 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>

To finish the process, just copy in the import classes 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.ResizeApi;
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();
}

All done. If it’s your first time using a Cloudmersive API, head to our website Home — Cloudmersive APIs and create a free account (with zero financial obligations) to receive a limit of 800 API calls per month.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

Responses (1)