How to Generate a Perceptual Image Hash using Java
Sometimes referred to as “digital fingerprinting,” perceptual hashing is a ubiquitous & highly effective way of tracking duplicate versions of original media content across the web. The utility of perceptual hashes is demonstrated most frequently in the field of digital forensics, which notably involves identifying copyright infringement and tracking down other forms of illicit/illegally distributed media.
With our Perceptual Hash API, you can easily generate new hash values for your own images and use those hashes to track down copies or new versions of your content that you didn’t authorize the creation of. You can use this API for free — all you need to do is register a free account on our website (free accounts provide a limit of 800 API calls per month with zero additional commitments; perfect for small-scale projects) and then follow the below instructions to install the API client & structure your API call with Java code examples.
To begin API client installation, let’s first include a reference in the pom.xml repository:
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
And let’s wrap up installation by adding another reference to the pom.xml dependency:
<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v4.25</version>
</dependency>
</dependencies>
After that, we can add the imports to 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.RecognizeApi;
Lastly, we can call the perceptual hash function, passing through our API key & file path arguments. If you’d like to change the API’s default recognition mode (Normal), you can enter ‘Basic’ or ‘Advanced’ as a string value for the optional parameter recognitionMode.
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");RecognizeApi apiInstance = new RecognizeApi();
File imageFile = new File("/path/to/inputfile"); // File | Image file to perform the operation on. Common file formats such as PNG, JPEG are supported.
String recognitionMode = "recognitionMode_example"; // String | Optional, specify the recognition mode; possible values are Normal, Basic and Advanced. Default is Normal.
try {
ImageSimilarityHashResponse result = apiInstance.recognizeSimilarityHash(imageFile, recognitionMode);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling RecognizeApi#recognizeSimilarityHash");
e.printStackTrace();
}