How to add a Gaussian blur to an image in Java

Cloudmersive
2 min readJan 28, 2020

--

There are many useful applications for a Gaussian blur, but you didn’t come here to read about that. No, today’s post is only concerned with how to implement this effect with a minimum of effort. Five minutes is all we should need.

We shall begin by compiling our library. This can be done dynamically with Jitpack if we simply add the following references to Maven POM.

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>

All that’s left is to call filterGaussianBlur, define sigma and radius for the blur effect, and specify 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.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 gaussian blur operationFile 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.filterGaussianBlur(radius, sigma, imageFile);System.out.println(result);} catch (ApiException e) {System.err.println("Exception when calling FilterApi#filterGaussianBlur");e.printStackTrace();}

Here is an example image we can test the blur on:

We set the radius to 2 and sigma to 5, resulting in this:

Done!

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet