How to Add a Customizable Drop Shadow to an Image using Java

Cloudmersive
2 min readSep 14, 2022

--

Adding a shadow behind an image helps create the impression that the image is three-dimensional, giving it a little more “pop” off the page. Even though drop shadows are a common marketing/content aesthetic, however, it’s still difficult to create them without working within a document or photo editing application in the process. Thankfully, you can avoid that inconvenience & use our Drop Shadow API to transform images at scale instead. This API allows you to determine the opacity & blur distance (sigma) of the shadow, as well as specify the exact vertical/horizontal offset from the original image.

Below, I’ll demonstrate how you can easily incorporate this API into your app/website using complementary, ready-to-run Java code examples. To call the API, you’ll also need a valid & secure API key, which you can obtain for free by registering a free account on our website (this account comes with a limit of 800 API calls per month; perfect for small-scale projects & start ups).

Our first step is to install the API client. We can do this 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 adding one to the dependency:

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

Now we can turn our attention to the controller. Include the imports at the top of your 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;

And finally, we can call the API. Each parameter is labeled & described by its adjacent comment for your convenience:

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();
Integer X = 56; // Integer | Horizontal (X) offset of the drop shadow
Integer Y = 56; // Integer | Vertical (Y) offset of the drop shadow
Integer sigma = 56; // Integer | Sigma (blur distance) of the drop shadow
Integer opacity = 56; // Integer | Opacity of the drop shadow; 0 is 0% and 100 is 100%
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.editDropShadow(X, Y, sigma, opacity, imageFile);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling EditApi#editDropShadow");
e.printStackTrace();
}

That’s all the code you’ll need!

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet