Today's article deals with bar codes. Specifically, it deals with generating and decoding UPC-A and Code 3 of 9 bar codes in Java. A Java library and sample code are provided to demonstrate the functionality.
This bar code engine is implemented as a Java library, along with a few demo applications which show the library in action.
The bar code generation functionality is very simple, just drawing the bar codes according to the calling program's specifications.
The decoding process is a bit more involved. The image is first sharpened using an edge detection algorithm, then contrast-normalized and converted to two-level (either 100% black or 100% white) monochrome representation. Then the image is scanned in several directions at regularly spaced intervals and the resulting pixel streams are passed into an algorithm which examines the bars and spaces, and decodes them back into the original numeric or text data which is represented by the bar code.
The decoder is able to scan an image of arbitrary size, and to detect, decode and return the bar code(s) found in the image with a reasonable degree of reliability.
The most obvious application is for decoding bar codes which appear on scanned documents, so that the scanned documents can be classified according to an identifier found in the bar code.
Other applications could involve using a digital camera or web cam as a bar code scanner, or possibly even some form of online coupon system using bar codes.
The following instructions assume you're running on a Linux box, with a reasonably recent version of Sun's JDK installed. You can get the JDK at java.sun.com. Be sure to read my instructions on how to get rid of the counterfeit (and incomplete/incompatible) GNU Java implementation. You can find that here.
Now that you have a working Java implementation, let's proceed. When you download and unpack the tarball, you'll have a "barcode" directory. Under it you'll find these scripts:
The source code *should* already be compiled, and there should be a barcode.jar file in the top-level "barcode" directory. If so, you can proceed. If not, or if you need to rebuild after making a change to the source code, just do the following:
./compile && ./createJars
Assuming there are no errors, you'll get freshly compiled classes and a new barcode.jar with your changes.
To see the bar code renderer in action, run either or both of the following commands:
./code39barcoderenderer
./upcabarcoderenderer
Now let's try out the bar code decoder. If you look under the sampleBarCodeImages/code39 and sampleBarCodeImages/upc directories, there are several image files. Each of these contains at least one bar code, and can be used to demonstrate the functionality of the bar code decoder engine. To test the bar code decoder engine on an image, do something like this:
./imagebarcodescannerdemo sampleBarCodeImages/upc/barcodeCollage.jpg
To use the code in your own program, put barcode.jar into your classpath and follow one of the usage patterns found in the main() function of one of the source files. Have a look at the content of each of the demo scripts you ran, to find out which classes were actually executed.
Feel free to look at the other source files, if you're interested in the inner workings of the bar code engine. The concepts are fairly simple, yet reasonably effective. The most complex parts are the edge enhancement algorithm, which is an adaptation of the Sobel Edge Detection algorithm, and the actual bar and space decoding algorithms which automatically adapt to bar codes of various sizes.
I'm releasing this engine under the GPL license, version 2. This means that you can use it in GPL-compatible projects, and release modified versions, as long as you abide by the terms of the GPL version 2. This means, among other things, that your project (and any modified versions of this library) must also be released under the GPL or a compatible license. You can find licenses which are compatible with the GPL at http://www.opensource.org/. If you need to use this library for a proprietary product which you will be selling or otherwise distributing, contact me using the Contact Us form for a commercial-use license.
You can download the latest version of the OCR engine, including source code, compiled object code, scripts and sample training and test images, using this link:
As always, I'm interested in your feedback, suggestions for improvement, use cases, success stories, or whatever.
Enjoy!