How to Perform a Gaussian Blur on an Image using Java
When you want to add that perfect blur to your images, you should go with a Gaussian. This method uses a kernel filter matrix to change pixel values and results in a high quality “fuzzy” output — perfect for all kinds of creative situations (branding, marketing, personal projects — you name it).
With our Gaussian Blur API, you can easily filter your images via the aforementioned kernel filter matrix, and you can take control over the radius of pixels (larger radius = greater blur effect) as well as the sigma (variance) of the operation through easy-to-configure parameters. The best part is it’s free to use this API — you just need to register a free account on our website to get your free-tier API key (which is valid for up to 800 API calls per month: perfect for small-scale projects & startups). With your API key in hand, you can use the below Java code examples to structure your API call & call the image filtering function.
We can begin by installing the API client. Let’s first add a reference by adding a reference to the pom.xml repository:
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Next let’s add a reference to the pom.xml dependency:
<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v4.25</version>
</dependency>
</dependencies>
Now we can include the 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;
And finally we can call the function, first authenticating our API key (where indicated by the code comments) and then specifying the desired radius & sigma for our Gaussian blur:
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 gaussian blur operation
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.filterGaussianBlur(radius, sigma, imageFile);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling FilterApi#filterGaussianBlur");
e.printStackTrace();
}
Don’t forget to include your image file path where shown in the code comments, and you’re good to go. Simple as that.