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.
How it Works
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 http://www.oracle.com/technetwork/java/index.html. 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:
- compile – compiles the Java files into class files in the classes directory
- createJars – creates barcode.jar from the compiled classes
- upcabarcoderenderer – renders a UPC-A bar code
- code39barcoderenderer – renders a Code 3 of 9 bar code
- imagebarcodescannerdemo – scans an image for UPC-A and/or Code 3 of 9 bar codes, and prints the values of all barcodes which are found in the image
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.
Running the Demos
To see the bar code renderer in action, run either or both of the following commands:
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:
Using the Code in your Program
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 originally released this engine under the GPL license, version 2. However, I felt it would be more commercially friendly if it were re-released under the BSD license. As of may 6, 2010, I’ve created a project page on SourceForge, changed the license to BSD, and uploaded the whole thing to the SourceForge Subversion repository.
SourceForge Project Page
The new JavaBarCode SourceForge project is located here: http://javabar.sourceforge.net
As always, I’m interested in your feedback, suggestions for improvement, use cases, success stories, or whatever.