How to add a drop shadow to an image in Java

Drop shadows are a great way to add visual interest to an image, whether your end result is for website or app. This is easy enough to do manually in Photoshop, but what about doing it automatically on the fly? That requires a bit of time and coding. Luckily, there is an alternative method, which we will be employing today. Using an API, we will have functionality in a matter of minutes.

First, add references to pom.xml, which will allow Jitpack to dynamically compile the library that we need.

Repository

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

Dependency

<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v2.75</version>
</dependency>
</dependencies>

With our library ready to go, we can now call editDropShadow and provide an image.

// 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: ApikeyApiKeyAuth 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 shadowInteger Y = 56; // Integer | Vertical (Y) offset of the drop shadowInteger sigma = 56; // Integer | Sigma (blur distance) of the drop shadowInteger opacity = 56; // Integer | Opacity of the drop shadow; 0 is 0% and 100 is 100%File imageFile = new File("/path/to/file"); // 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();}

Done! Our image will be given a pleasing drop shadow. Note that you can customize this shadow however you like via offset, sigma, and opacity. Here is an example result image:

Written by

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