How to Perform a Motion Blur on an Image using Java

While simple Gaussian Blurs distort pixels in a radius from the center of a photo, motion blurs go a step further, taking angle into account & thus directing our gaze towards an area of implied movement. With our Motion Blur API, you can easily create this effect for your images by configuring parameters including the radius, sigma (variance) and angle (in degrees) of the motion blur. The best part is you can use this API for free — you just need to register a free account on our website (free account provide an API key with a limit of 800 API calls per month) and follow steps below to structure your API with the ready-to-run Java code examples I’ve provided. If you’re looking for code examples in a different programming language, that’s no problem, either — just head to our API Console page, and you’ll find comparable examples in 12+ additional programming languages for your convenience.

Let’s jump into he demo. Our first step is to install the API client; we can begin that process 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 we can finish installation by adding a reference to the dependency in pom.xml:

<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 following imports at the top of our 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.FilterApi;

Last but not least, we can call the function. It’s time to determine what our blur radius, sigma & angle will be, and we need to include our image file path where shown by the code comments. Don’t forget to add your API key where shown as well:

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

After that you’re all done — no more code required.

--

--

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
Cloudmersive

Cloudmersive

436 Followers

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