How to Scan a Barcode Image in Java
Use an API to scan or look up a barcode. Supported barcode types include AZTEC, CODABAR, CODE_39, CODE_93, CODE_128, DATA_MATRIX, EAN_8, EAN_13, ITF, MAXICODE, PDF_417, QR_CODE, RSS_14, RSS_EXPANDED, UPC_A, UPC_E, All_1D, UPC_EAN_EXTENSION, MSI, PLESSEY, and IMB.
Join the DZone community and get the full member experience.
Join For FreeFrom manufacturing, all the way to point of sale (POS), barcode scanning allows for the accurate tracking and delivering of inventory and products according their order specifications. In a warehouse setting, this tool may allow workers to ensure they are selecting and adding the correct parts to a product or product order according to purchaser requests, and in shipping, it provides for the accurate maintenance of inventory manifests. For organizations outside of commerce such as hospitals, barcode scanning technology can be used to confirm that the appropriate medications are distributed efficiently to the correct patients, mitigating the risk of user error.
The following Barcode APIs will allow you to use Barcode data to retrieve information on a product automatically in two ways: image scanning and text string input. The goal for this to tutorial is to increase your organizational efficiency and improve your product output by automating inventory and purchase processes through an in-house application or POS.
The first API provides a function for barcode scanning, in which an input image can scanned for barcode information. It supports many barcode types including AZTEC, CODABAR, CODE_39, CODE_93, CODE_128, DATA_MATRIX, EAN_8, EAN_13, ITF, MAXICODE, PDF_417, QR_CODE, RSS_14, RSS_EXPANDED, UPC_A, UPC_E, All_1D, UPC_EAN_EXTENSION, MSI, PLESSEY, and IMB. Common image file formats such as PNG and JPG are supported as input.
To start this function, we first need to install our library with Maven or Gradle. Start by adding a Jitpack reference to the repository in pom.xml:
xxxxxxxxxx
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Then, add a reference to the dependency:
xxxxxxxxxx
<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v3.54</version>
</dependency>
</dependencies>
To install with Gradle, add this in your root build.gradle at the end of repositories:
xxxxxxxxxx
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
Then, add the dependency in build.gradle:
xxxxxxxxxx
dependencies {
implementation 'com.github.Cloudmersive:Cloudmersive.APIClient.Java:v3.54'
}
After install, we can add our imports to the file and call our function:
xxxxxxxxxx
// 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.BarcodeScanApi;
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");
BarcodeScanApi apiInstance = new BarcodeScanApi();
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 {
BarcodeScanResult result = apiInstance.barcodeScanImage(imageFile);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling BarcodeScanApi#barcodeScanImage");
e.printStackTrace();
}
Your output for this function should return values for whether it was employed successfully, the type of barcode scanned, and the raw text description of the represented item. To ensure that the API functions properly, you will need to verify that:
- Your input file is valid, and that the file type is supported by the function.
- Your API Key has been properly inserted into the code block. This can be retrieved at no cost on the Cloudmersive website and will provide access to 800 monthly calls across our library of APIs.
If you need to input barcode information for a product but are unable to scan an image due to smudged or absent characters, the second API we are going to show you will help keep your operations running smoothly. With this API, you can quickly and easily look up an EAN barcode value to return product data.
As shown above, we first need to install our library through Maven or Gradle, and then we can add our imports to our file. After this, we can call the function:
// 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.BarcodeLookupApi;
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");
BarcodeLookupApi apiInstance = new BarcodeLookupApi();
String value = "value_example"; // String | Barcode value
try {
BarcodeLookupResponse result = apiInstance.barcodeLookupEanLookup(value);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling BarcodeLookupApi#barcodeLookupEanLookup");
e.printStackTrace();
}
Upon input, this function will inform you whether it was successfully activated, if there are any matches to the input EAN value, and the title or description of the product.
With these APIs, you can ensure that your operations run smoothly from start to finish and that your clients always receive exactly what they order.
Opinions expressed by DZone contributors are their own.
Comments