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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Using Barcodes in iText 7
  • How to Scan a Barcode Image in Java
  • Designing a Java Connector for Software Integrations
  • How to Convert XLS to XLSX in Java

Trending

  • Scalable, Resilient Data Orchestration: The Power of Intelligent Systems
  • Medallion Architecture: Efficient Batch and Stream Processing Data Pipelines With Azure Databricks and Delta Lake
  • How AI Agents Are Transforming Enterprise Automation Architecture
  • Understanding IEEE 802.11(Wi-Fi) Encryption and Authentication: Write Your Own Custom Packet Sniffer
  1. DZone
  2. Coding
  3. Java
  4. How to Create a Barcode Image in Java

How to Create a Barcode Image in Java

The following five APIs will allow you to create barcode images in the three previously mentioned formats.

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

Join the DZone community and get the full member experience.

Join For Free

Barcodes are used universally in commerce and retail to aid in tracking, purchasing, and inventory. This allows organizations to keep accurate records of their supplies, products, and other items that are pivotal to their operations. For international and national use, there are different barcodes that are utilized for specific contexts. For example, UPC and EAN barcodes look similar in their formatting and can be used in similar ways, but UPC is a largely North American coding system; however, both UPC and EAN are used globally. QR Codes are also used globally but are usually employed for sharing complex sets of information like item details or website links.  

The following five APIs will allow you to create barcode images in the three previously mentioned formats. This will allow you to print or create packaging with your personal barcodes attached, without needing to retrieve it from an outside source. This can be especially useful for small businesses as it provides increased independence as a company and improved visual, professional appeal for your products. 

To use any of the following APIs, you will first need to install the SDK reference with Maven or Gradle. To install with Maven, add a Jitpack reference to the repository in pom.xml: 

XML
 




x


 
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 the reference to 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
}



The first set of functions are centered on creating UPC barcodes in two formats: UPC-A and UPC-E. UPC-A is the standard UPC barcode type, containing 12 digits that encode the product’s information. The UPC-E acts as a compressed form of this code, as it can take the original 12-digit value and suppressing it to 6 digits with a seventh checksum digit. If your UPC value does not start with zero, however, you cannot compress it into a UPC-E code. These should be mainly used in retail and simple product encoding as they are not equipped to convey complex information.  

To create a UPC-A barcode, install the SDK as shown above, and then call the 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.GenerateBarcodeApi;
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
GenerateBarcodeApi apiInstance = new GenerateBarcodeApi();
17
String value = "value_example"; // String | UPC-A barcode value to generate from
18
try {
19
    byte[] result = apiInstance.generateBarcodeUPCA(value);
20
    System.out.println(result);
21
} catch (ApiException e) {
22
    System.err.println("Exception when calling GenerateBarcodeApi#generateBarcodeUPCA");
23
    e.printStackTrace();
24
}



The only input necessary for this function is the UPC barcode value from which the image will be generated. This will return an output file containing the barcode image as a PNG. To ensure that this API works properly, you need to ensure certain requirements are met:   

  • The UPC barcode value is valid and inputs correctly. 

  • You have input your API Key. This can be retrieved at no cost on the Cloudmersive website, providing 800 monthly calls across our API library. 

To create a UPC-E code, follow the same steps to install and call the following 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.GenerateBarcodeApi;
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
GenerateBarcodeApi apiInstance = new GenerateBarcodeApi();
17
String value = "value_example"; // String | UPC-E barcode value to generate from
18
try {
19
    byte[] result = apiInstance.generateBarcodeUPCE(value);
20
    System.out.println(result);
21
} catch (ApiException e) {
22
    System.err.println("Exception when calling GenerateBarcodeApi#generateBarcodeUPCE");
23
    e.printStackTrace();
24
}



EAN barcodes were created for use in Europe but are recognized internationally. The two types of EAN code are EAN-13 and EAN-8, with EAN-13 being the most widely used. Like the UPC format EAN codes can only encode numeric data, which allows it to be scanned quickly and easily. EAN-13 contains 13 digits that represent the country code, product and manufacturer information, and the checksum digits. EAN-8 contains 8 digits and is intended for use on products with packaging that is too small to feature an EAN-13 code.  

To generate an EAN-13 code, install the SDK as shown above and then call the 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.GenerateBarcodeApi;
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
GenerateBarcodeApi apiInstance = new GenerateBarcodeApi();
17
String value = "value_example"; // String | Barcode value to generate from
18
try {
19
    byte[] result = apiInstance.generateBarcodeEAN13(value);
20
    System.out.println(result);
21
} catch (ApiException e) {
22
    System.err.println("Exception when calling GenerateBarcodeApi#generateBarcodeEAN13");
23
    e.printStackTrace();
24
}



For an EAN-8 code, perform the same initial steps and call the function below:

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.GenerateBarcodeApi;
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
GenerateBarcodeApi apiInstance = new GenerateBarcodeApi();
17
String value = "value_example"; // String | Barcode value to generate from
18
try {
19
    byte[] result = apiInstance.generateBarcodeEAN8(value);
20
    System.out.println(result);
21
} catch (ApiException e) {
22
    System.err.println("Exception when calling GenerateBarcodeApi#generateBarcodeEAN8");
23
    e.printStackTrace();
24
}



All these previous barcode formats are considered 1D types, as they can only encode simple, numerical data like product codes. However, if you wish to convey more complex information, a QR Code, which is a 2D format, is ideal. Short for Quick Return code, a QR code can be used to convey a wide variety of information by scanning with a QR code enabled device such as a smartphone. These can be used for product details, marketing purposes, and sharing service information such as menus.  

To generate a QR Code, call the following 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.GenerateBarcodeApi;
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
GenerateBarcodeApi apiInstance = new GenerateBarcodeApi();
17
String value = "value_example"; // String | QR code text to convert into the QR code barcode
18
try {
19
    byte[] result = apiInstance.generateBarcodeQRCode(value);
20
    System.out.println(result);
21
} catch (ApiException e) {
22
    System.err.println("Exception when calling GenerateBarcodeApi#generateBarcodeQRCode");
23
    e.printStackTrace();
24
}



Input for this function can include any freeform text, and thus is flexible according to your requirements. 

All these functions will return a PNG file containing your barcode image for use on any product or item. If you have any questions about using these APIs or inquiries concerning other API solutions, you can visit the Cloudmersive website, where our team is happy to help with anything you might need. 

Barcode Java (programming language)

Opinions expressed by DZone contributors are their own.

Related

  • Using Barcodes in iText 7
  • How to Scan a Barcode Image in Java
  • Designing a Java Connector for Software Integrations
  • How to Convert XLS to XLSX in Java

Partner Resources

×

Comments
Oops! Something Went Wrong

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!