How to Resize an Image & Preserve its Aspect Ratio using Java

Aspect ratio refers to the proportion of an image’s frames/dimensions, and it can be difficult to retain an original aspect ratio when resizing an image with insufficient parameters. That’s why our Resize Image/Preserve Aspect Ratio API is a big help — it allows you to specify the maximum width and maximum height of the image you want to resize, thus retaining your image’s relative dimensions and displaying the same proportionate look/feel.

It’s easy to take advantage of this API for free: all you need to do is register a free account on our website and use the Java code examples below to structure your API call. Equipped with 800 free API calls per month, your free account will make it possible to perform this operation hundreds of times without worrying about paying a cent in monthly fees or subscriptions.

We can get API client installation started by first adding a reference to the pom.xml repository:

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

And then we can wrap up installation by adding a reference to the pom.xml dependency:

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

Now let’s 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;

Finally, let’s call the function. First pass through your free-tier API key (where shown in the comments) and file path, and then specify the maxWidth and maxHeight of your image:

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 maxWidth = 56; // Integer | Maximum width of the output image - final image will be as large as possible while less than or equial to this width
Integer maxHeight = 56; // Integer | Maximum height of the output image - final image will be as large as possible while less than or equial to 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.resizePost(maxWidth, maxHeight, imageFile);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling ResizeApi#resizePost");
e.printStackTrace();
}

--

--

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
Cloudmersive

Cloudmersive

436 Followers

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