How to add motion blur to an image in Java

Cloudmersive
2 min readJan 28, 2020

A touch of motion blur can add excellent cinematic effect to an image. But how can we apply this in a photo app without writing a ton of code? The answer is easy. There is an API for that.

Firstly, we must compile our library with Jitpack. With that in mind, we will need to have two references in pom.xml.

Repository reference

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

Dependency reference

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

Now let us use the following lines of code to call filterMotionBlur.

// 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.FilterApi;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");FilterApi apiInstance = new FilterApi();Integer radius = 56; // Integer | Radius in pixels of the blur operation; a larger radius will produce a greater blur effectInteger sigma = 56; // Integer | Sigma, or variance, of the motion blur operationInteger angle = 56; // Integer | Angle of the motion blur in degreesFile 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.filterMotionBlur(radius, sigma, angle, imageFile);System.out.println(result);} catch (ApiException e) {System.err.println("Exception when calling FilterApi#filterMotionBlur");e.printStackTrace();}

We can set the motion blur’s angle, radius, and sigma, allowing for a high degree of customization. Let’s try it on the following image.

Here is a sample output created with radius:10, sigma:5, angle:255:

--

--

Cloudmersive

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