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 Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
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
Partner Zones AWS Cloud
by AWS Developer Relations
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
Partner Zones
AWS Cloud
by AWS Developer Relations
11 Monitoring and Observability Tools for 2023
Learn more
  1. DZone
  2. Data Engineering
  3. Databases
  4. Java Barcode API

Java Barcode API

Vineet Manohar user avatar by
Vineet Manohar
·
Sep. 27, 10 · Interview
Like (2)
Save
Tweet
Share
110.26K Views

Join the DZone community and get the full member experience.

Join For Free

Originally Barcodes were 1D representation of data using width and spacing of bars. Common bar code types are UPC barcodes which are seen on product packages. There are 2D barcodes as well (they are still called Barcodes even though they don’t use bars). A common example of 2D bar code is QR code (shown on right) which is commonly used by mobile phone apps. You can read history and more info about Barcodes on Wikipedia.

There is an open source Java library called ‘zxing’ (Zebra Crossing) which can read and write many differently types of bar codes formats. I tested zxing and it was able to read a barcode embedded in the middle of a 100 dpi grayscale busy text document!

This article demonstrates how to use zxing to read and write bar codes from a Java program.

Getting the library

It would be nice if the jars where hosted in a maven repo somewhere, but there is no plan to do that (see Issue 88). Since I could not find the binaries available for download, I decided to download the source code and build the binaries, which was actually quite easy.

The source code of the library is available on Google Code. At the time of writing, 1.6 is the latest version of zxing.

1. Download the release file ZXing-1.6.zip (which contains of mostly source files) from here.
2. Unzip the file in a local directory
3. You will need to build 2 jar files from the downloaded source: core.jar, javase.jar

Building core.jar

 cd zxing-1.6/core 
mvn install
cd zxing-1.6/core
mvn install

This will install the jar in your local maven repo. Though not required, you can also deploy it to your company’s private repo by using mvn:deploy or by manually uploading it to your maven repository.

There is an ant script to build the jar as well.

Building javase.jar

Repeat the same procedure to get javase.jar

   cd zxing-1.6/javase 
   mvn install
cd zxing-1.6/javase
mvn install

Including the libraries in your project

If you are using ant, add the core.jar and javase.jar to your project’s classpath.

If you are using maven, add the following to your pom.xml.

<dependencies>
 <dependency>
  <groupId>com.google.zxing</groupId>
  <artifactId>core</artifactId>
  <version>1.6-SNAPSHOT</version>
 </dependency>

 <dependency>
  <groupId>com.google.zxing</groupId>
  <artifactId>javase</artifactId>
  <version>1.6-SNAPSHOT</version>
 </dependency>
<dependencies> 
<dependencies>
 <dependency>
  <groupId>com.google.zxing</groupId>
  <artifactId>core</artifactId>
  <version>1.6-SNAPSHOT</version>
 </dependency>

 <dependency>
  <groupId>com.google.zxing</groupId>
  <artifactId>javase</artifactId>
  <version>1.6-SNAPSHOT</version>
 </dependency>
<dependencies>

Once you have the jars included in your project’s classpath, you are now ready to read and write barcodes from java!

Reading a Bar Code from Java

You can read the bar code by first loading the image as an input stream and then calling this utility method.

InputStream barCodeInputStream = new FileInputStream("file.jpg");
BufferedImage barCodeBufferedImage = ImageIO.read(barCodeInputStream);

LuminanceSource source = new BufferedImageLuminanceSource(barCodeBufferedImage);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Reader reader = new MultiFormatReader();
Result result = reader.decode(bitmap);

System.out.println("Barcode text is " + result.getText());
InputStream barCodeInputStream = new FileInputStream("file.jpg");
BufferedImage barCodeBufferedImage = ImageIO.read(barCodeInputStream);

LuminanceSource source = new BufferedImageLuminanceSource(barCodeBufferedImage);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Reader reader = new MultiFormatReader();
Result result = reader.decode(bitmap);

System.out.println("Barcode text is " + result.getText());

Writing a Bar Code from Java

You can encode a small text string as follows:

String text = "98376373783"; // this is the text that we want to encode

int width = 400;
int height = 300; // change the height and width as per your requirement

// (ImageIO.getWriterFormatNames() returns a list of supported formats)
String imageFormat = "png"; // could be "gif", "tiff", "jpeg" 

BitMatrix bitMatrix = new QRCodeWriter().encode(text, BarcodeFormat.QR_CODE, width, height);
MatrixToImageWriter.writeToStream(bitMatrix, imageFormat, new FileOutputStream(new File("qrcode_97802017507991.png")));
String text = "98376373783"; // this is the text that we want to encode

int width = 400;
int height = 300; // change the height and width as per your requirement

// (ImageIO.getWriterFormatNames() returns a list of supported formats)
String imageFormat = "png"; // could be "gif", "tiff", "jpeg" 

BitMatrix bitMatrix = new QRCodeWriter().encode(text, BarcodeFormat.QR_CODE, width, height);
MatrixToImageWriter.writeToStream(bitMatrix, imageFormat, new FileOutputStream(new File("qrcode_97802017507991.png")));

In the above example, the bar code for “97802017507991″ is written to the file “qrcode_97802017507991.png” (click to see the output).

JavaDocs and Documentation

The Javadocs are part of the downloaded zip file. You can find a list of supported bar code formats in the Javadocs. Open the following file to see the javadocs.

zxing-1.6/docs/javadoc/index.html

zxing-1.6/docs/javadoc/index.html

From http://www.vineetmanohar.com/2010/09/java-barcode-api/

Barcode Java (programming language) API

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • [DZone Survey] Share Your Expertise and Take our 2023 Web, Mobile, and Low-Code Apps Survey
  • Accelerating Enterprise Software Delivery Through Automated Release Processes in Scaled Agile Framework (SAFe)
  • How to Use Java Event Listeners in Selenium WebDriver?
  • 5 Common Firewall Misconfigurations and How to Address Them

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: