How to Precisely Composite Two Images Together using Java

If creating composite photos is your thing, you might’ve already benefitted from reading about our basic Image Composite API, which provides a simple image-layering solution with basic parameters to play around with. In this article, I’ll be detailing another iteration of our Image Composite API which provides the opportunity for a much greater degree of precision when combining your images together. While the basic API iteration asks for a general composite location (such as “top-left” or “bottom-center”), the precise iteration allows you to specify the exact coordinates (in pixels) for your composite location, not to mention pad the borders of your layered images with transparent pixels as needed. Below, I’ll demonstrate how to install the API client once again — and this time create a more precise image-composite with your subsequent API call.

Let’s begin API client installation by adding a reference to the repository in pom.xml:

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

And then 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>

Next, 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.EditApi;

Finally, let’s include the final code block below to structure our precise image compositing API call. Each of the precision-focused parameters are accompanied with comments to help you properly configure your arguments. Beyond configuring these options, all you’ll need to do is pass through your source & destination image file paths & authenticate with your API key. With that, you’re all finished!

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");
EditApi apiInstance = new EditApi();
File baseImage = new File("/path/to/inputfile"); // File | Image file to perform the operation on. Common file formats such as PNG, JPEG are supported.
File layeredImage = new File("/path/to/inputfile"); // File | Image to layer on top of the base image.
Integer top = 56; // Integer | Optional; Desired distance in pixels from the top of the base image to the top of the layered image.
Integer bottom = 56; // Integer | Optional; Desired distance in pixels from the bottom of the base image to the bottom of the layered image.
Integer left = 56; // Integer | Optional; Desired distance in pixels from the left side of the base image to the left side of the layered image.
Integer right = 56; // Integer | Optional; Desired distance in pixels from the right side of the base image to the right side of the layered image.
Integer width = 56; // Integer | Optional; Desired width of the layered image in pixels. Leave height empty or 0 to automatically scale the image proportionally.
Integer height = 56; // Integer | Optional; Desired height of the layered image in pixels. Leave width empty or 0 to automatically scale the image proportionally.
try {
byte[] result = apiInstance.editCompositePrecise(baseImage, layeredImage, top, bottom, left, right, width, height);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling EditApi#editCompositePrecise");
e.printStackTrace();
}

--

--

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
Cloudmersive

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