DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Using Barcodes in iText 7
  • Getting Started With Agentic Workflows in Java and Quarkus
  • Building AI-Powered Java Applications With Jakarta EE and LangChain4j
  • Alternative Structured Concurrency

Trending

  • Introduction to Tactical DDD With Java: Steps to Build Semantic Code
  • Ingesting Fixed-Width Mainframe Files Into Delta Lake: The Details Nobody Writes Down
  • Lambda-Driven API Design: Building Composable Node.js Endpoints With Functional Primitives
  • From AI Chaos to Control: Building Enterprise-Grade LLM Gateways With MuleSoft Anypoint
  1. DZone
  2. Coding
  3. Java
  4. How to Scan a Barcode Image in Java

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.

By 
Brian O'Neill user avatar
Brian O'Neill
DZone Core CORE ·
Oct. 20, 20 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
12.1K Views

Join the DZone community and get the full member experience.

Join For Free

From 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:

XML
xxxxxxxxxx
1
 
1
<repositories>
2
    <repository>
3
        <id>jitpack.io</id>
4
        <url>https://jitpack.io</url>
5
    </repository>
6
</repositories>


Then, add a reference to the dependency:

XML
xxxxxxxxxx
1
 
1
<dependencies>
2
<dependency>
3
    <groupId>com.github.Cloudmersive</groupId>
4
    <artifactId>Cloudmersive.APIClient.Java</artifactId>
5
    <version>v3.54</version>
6
</dependency>
7
</dependencies>


To install with Gradle, add this in your root build.gradle at the end of repositories:

Java
xxxxxxxxxx
1
 
1
allprojects {
2
    repositories {
3
        ...
4
        maven { url 'https://jitpack.io' }
5
    }
6
}


Then, add the dependency in build.gradle:

Java
xxxxxxxxxx
1
 
1
dependencies {
2
        implementation 'com.github.Cloudmersive:Cloudmersive.APIClient.Java:v3.54'
3
}


After install, we can add our imports to the file and call our function:

Java
xxxxxxxxxx
1
24
 
1
// Import classes:
2
//import com.cloudmersive.client.invoker.ApiClient;
3
//import com.cloudmersive.client.invoker.ApiException;
4
//import com.cloudmersive.client.invoker.Configuration;
5
//import com.cloudmersive.client.invoker.auth.*;
6
//import com.cloudmersive.client.BarcodeScanApi;
7

          
8
ApiClient defaultClient = Configuration.getDefaultApiClient();
9

          
10
// Configure API key authorization: Apikey
11
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
12
Apikey.setApiKey("YOUR API KEY");
13
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
14
//Apikey.setApiKeyPrefix("Token");
15

          
16
BarcodeScanApi apiInstance = new BarcodeScanApi();
17
File imageFile = new File("/path/to/inputfile"); // File | Image file to perform the operation on.  Common file formats such as PNG, JPEG are supported.
18
try {
19
    BarcodeScanResult result = apiInstance.barcodeScanImage(imageFile);
20
    System.out.println(result);
21
} catch (ApiException e) {
22
    System.err.println("Exception when calling BarcodeScanApi#barcodeScanImage");
23
    e.printStackTrace();
24
}


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:

Java
x
24
 
1
// Import classes:
2
//import com.cloudmersive.client.invoker.ApiClient;
3
//import com.cloudmersive.client.invoker.ApiException;
4
//import com.cloudmersive.client.invoker.Configuration;
5
//import com.cloudmersive.client.invoker.auth.*;
6
//import com.cloudmersive.client.BarcodeLookupApi;
7

          
8
ApiClient defaultClient = Configuration.getDefaultApiClient();
9

          
10
// Configure API key authorization: Apikey
11
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
12
Apikey.setApiKey("YOUR API KEY");
13
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
14
//Apikey.setApiKeyPrefix("Token");
15

          
16
BarcodeLookupApi apiInstance = new BarcodeLookupApi();
17
String value = "value_example"; // String | Barcode value
18
try {
19
    BarcodeLookupResponse result = apiInstance.barcodeLookupEanLookup(value);
20
    System.out.println(result);
21
} catch (ApiException e) {
22
    System.err.println("Exception when calling BarcodeLookupApi#barcodeLookupEanLookup");
23
    e.printStackTrace();
24
}


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. 

Barcode Java (programming language)

Opinions expressed by DZone contributors are their own.

Related

  • Using Barcodes in iText 7
  • Getting Started With Agentic Workflows in Java and Quarkus
  • Building AI-Powered Java Applications With Jakarta EE and LangChain4j
  • Alternative Structured Concurrency

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook