How to Composite Two Images Together using Java

Cloudmersive
2 min readSep 14, 2022

--

We see composite images everywhere in the real world, whether they’re in advertisements, websites or even your friend’s/family’s social media feeds. When we composite two images together, we’re typically using the subject of one image (layered image) on top of the background of another (base image), creating interesting effects (like a Giraffe walking through NYC, a person in beach clothes on the moon, etc.). You can create this effect at scale with our Image Composite API, which allows you to take control over the exact location the layered image should be placed. Using Java code examples provided below, you can easily incorporate this API into your project for free; in addition, you’ll need to get an API key by registering a free account on our website (this account comes with a limit of 800 API calls per month & zero additional commitments — perfect for getting small projects off the ground with low cost).

Without further ado, let’s install the API client. We can do so by first 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 in 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 move on to our controller & add the following imports to the top of the 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;

Last but not least, we can call the function. Pay close attention to the location parameter comments to ensure your format your request properly; otherwise, simply include your API key & file paths where shown:

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();
String location = "location_example"; // String | Location to composite the layered images; possible values are: \"center\", \"top-left\", \"top-center\", \"top-right\", \"center-left\", \"center-right\", \"bottom-left\", \"bottom-center\", \"bottom-right\"
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.
try {
byte[] result = apiInstance.editCompositeBasic(location, baseImage, layeredImage);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling EditApi#editCompositeBasic");
e.printStackTrace();
}

And you’re all set — no more contributions required. Composite away!

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet