How to Composite 2 Images Together in Java
Composite a layered image onto a base image by leveraging APIs in Java.
Join the DZone community and get the full member experience.
Join For FreeLooking for a way to jazz up your websites or applications? Creating compelling imagery to accompany your copywriting, service offerings, and projects can be an excellent method for ramping up your content. One type of imagery that has become incredibly popular, especially during the digital age, is the composite image. A composite image is made up of two or more images, which are merged together to create a single composition. The resulting composite image often depicts people or objects in implausible, improbable, or impossible circumstances, such as a 21st-century man in an 18th-century city, or a castle in the clouds. Another slightly more practical application of the technique is to integrate photos of landscapes or backgrounds with lighting or other visual aspects that are generally technically or physically unachievable.
While this technique may sound relatively uncomplicated in our current technology-driven world, it was a different story when the composite image was first created in the mid-1800s. Artists and photographers began toying with the layering effect by literally cutting, gluing, and juxtaposing photos and paintings together to market and memorialize large social events. As society moved into the World War I era, the process utilized the virtual reality effect to create scenes of soldiers with their loved ones, as well as propaganda for the war effort.
Nowadays, constructing a composite image is much less complex and can be achieved via a variety of digital apps and editing tools. In this brief tutorial, we will demonstrate two APIs that can be used in Java to composite two input images together; a layered image onto a base image. These tools support PNG transparency and padding and can be used for various projects or integrated into your applications as needed.
To begin, we will install the API client by adding a repository reference to our Maven POM file:
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Then, we can add a reference to the dependency:
<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v3.90</version>
</dependency>
</dependencies>
Now, we are ready for our first function, which will composite two images together based on a location such as “center”, “bottom-right”, “top-left”, etc. We can add the imports to the top of our controller and call the function with the following example code:
// 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;
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();
}
In order for the operation to run successfully you will need to ensure the following parameters are included:
- Location - 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”.
- Base Image - image file to perform the operation on; common file formats such as PNG, JPEG are supported.
- Layered Image - image to layer on top of the base image.
- API Key – your personal API key. If you need to obtain a personal API key, you can do so by registering for a free account on the Cloudmersive website; this will provide 800 monthly calls across any of our APIs.
For this next function, we will still need our base image, layered image, and API key, but there are additional parameters that we can input to improve the precision of the layering:
- Top – desired distance in pixels from the top of the base image to the top of the layered image.
- Bottom – desired distance in pixels from the bottom of the base image to the bottom of the layered image.
- Left – desired distance in pixels from the left side of the base image to the left side of the layered image.
- Right – desired distance in pixels from the right side of the base image to the right side of the layered image.
- Height – desired width of the layered image in pixels. Leave height empty or 0 to automatically scale the image proportionally.
- Width - desired height of the layered image in pixels. Leave width empty or 0 to automatically scale the image proportionally.
It’s important to note that providing multiple parameters in a single axis (for example top and bottom) is not recommended, since only one of the parameters will be used per axis. Once you have your information in order, you can add it to the following example code:
// 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;
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();
}
And that’s it for this tutorial! As you incorporate composite images into your applications, projects and websites, keep in mind that you should never portray a fabricated image as “real”; they can be exposed without much difficulty, and it’s not a good look for an individual or a corresponding company/brand.
Beyond that, however, let your compositing creativity flow!
Opinions expressed by DZone contributors are their own.
Comments