CFC for generating and reading 2D barcodes in ColdFusion
One of the projects I’ve been working on recently requires the ability to receive faxes and place them into a customer’s account. At first we looked at using optical character recognition to scan out the customer’s ID from the cover sheet, but this route was looking expensive and time consuming, and we weren’t sure we would be able to reliably read the customer IDs, since faxes are sometimes hard to read even with the human eye. We are providing a pre-filled out cover sheet, so the customer ID would be printed, not hand written, but I think it would still be difficult to read.
Just around this time I saw Jim Rising’s post on cf-talk saying that if you need to do this kind of thing, you really should be looking at using a 2D barcode instead of trying to OCR a number. So I did. He mentioned Java4Less, a company that sells reasonably priced (~$100) java libraries for reading and writing barcodes, including data matrix barcodes. A datamatrix barcode is a 2D barcode that can encode more data in less space than a traditional barcode. Its also much more durable, and can be read even when faxed several times. They look like this:

I’ve finally got this all working, and have packaged it up as a CFC. Here’s how its used:
<cfset BarCoder = CreateObject("component","2DBarCode_j4l").init()>
Reading Barcodes
<cfset results = BarCoder.readFromImage(myImageObject)>
or
<cfset results = BarCoder.readFromFile("myFile.png")>
These methods return an array of barcodes that were found in the image. Each element in the array contains the following structure:
| Key | Description |
| x | The x coordinate of where the barcode was found on the image. |
| y | The y coordinate of where the barcode was found on the image. |
| value | The value that was read from the barcode. |
If no barcodes were found, an empty array is returned.
Creating Barcodes
Basic example:
<cfset BCImage = BarCoder.createBarCode(text='my text to place into barcode')>
BCImage now contains a ColdFusion image object that you can write to file or send it to the browser or whatever you need to do. There are several other options you can pass to createBarCode(), they described in the readme file included in the download.
To make this all work you’ll need to get two jar files from Java4Less and place them into your {coldfusion-install-dir}/lib directory, or place them somewhere else and add the path to them in your java.class.path setting in jvm.config.
Java4Less.com does have evaluation versions of these libraries available for download, so you can play with this before spending any money. My cfc only deals with 2D/Data Matrix barcodes. Java4Less offers libraries for working with regular barcodes, but I have not used them.
I have to say that being on ColdFusion 8 made this project much easier. I think it may have still been possible to do in CF7, but it would have required a lot more psudo-java code to be written in CF.
You can download the cfc from RiaForge:
December 15th, 2007 at 2:56 pm
Ryan,
Awesome stuff…. nice job on this.
-mark
December 15th, 2007 at 3:18 pm
Very cool! I was working with some basic barcode stuff this spring so I can see how this could really come in handy.
Jimmy
December 17th, 2007 at 8:49 am
Thanks for publishing and sharing this! I can see some interesting possibilities here. Please let us know if anyone extends this and uses it with CF 7!
December 17th, 2007 at 12:31 pm
Great job…Thanks for SHARING it with the world!
Regards,
Troy
December 30th, 2007 at 11:02 am
Would it be easy to make the cfc to work with CF 7 or BlueDragon?