Over a million developers have joined DZone.

Java Barcode API

DZone's Guide to

Java Barcode API

· Java Zone
Free Resource

Build vs Buy a Data Quality Solution: Which is Best for You? Gain insights on a hybrid approach. Download white paper now!

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

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

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.



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());

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")));

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.


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

Build vs Buy a Data Quality Solution: Which is Best for You? Maintaining high quality data is essential for operational efficiency, meaningful analytics and good long-term customer relationships. But, when dealing with multiple sources of data, data quality becomes complex, so you need to know when you should build a custom data quality tools effort over canned solutions. Download our whitepaper for more insights into a hybrid approach.


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}