Creating 2D Barcodes with AS3
For a recent project I needed to create 2D barcodes on printouts as a way of encoding a large batch of data. I settled on the PDF417 barcode format since it was well-known and unencumbered by licensing restrictions. This is the same format used on the back of many drivers licenses. There are numerous commercial libraries and barcode fonts on the market, but there was nothing that supported AS3 out of the box. After some research, I decided to port an open-source java library (pdf417lib by Paulo Soares) to AS3. This is the result of those efforts.
Source Code: pdf417lib_as3_src_20080730.zip
Online Demo: PDF417 Barcode Generator
The core library is just one class file (pdf417lib/Pdf417lib.as). I preserved the original Java API as much as possible. Note, there are a number of options in this class, and not all of them have been verified. Specifically, the raw codewords support has not been tested. I believe the error correction settings are working. One of the key things I added was the ability to create a DisplayObject out of the barcode data bits. I encapsulated the most useful settings in a static method that can be used like so:
-
import pdf417lib.Pdf417lib;
-
import flash.display.Shape;
-
-
var barcode:Shape = Pdf417lib.generateBarcode( "Some text to encode" );
-
addChild( barcode );
This library contains no Flex dependencies, and there is a Flash-friendly demo included in the source package. However, there is a Flex component (pdf417lib.flex.Pdf417Barcode) that makes this easy to use from within MXML. See the source code for the barcode generator app in pdf417lib/flex/demo/ for usage information.
Disclaimer: Whether or not a given barcode scanner can decode this output depends on a number of factors, most notably the quality of your printer and the scale at which the barcode is printed. The Flex component automatically scales the Y-dimension of the barcode shape by 3 which seems to work well for most purposes. Your mileage my vary.
