Generating Barcodes with Barbecue and Coldfusion

If you need to generate a barcode within your web app, you have a few options. To keep it completely server-side (i.e. not dependent on the client's fonts etc) one option is the open-source java package Barbecue.

This is a graphics solution, so the server needs to properly support graphics. Windows servers will be fine; *nix servers may be fine and they may not. Some help with graphics on *nix:

(Thanks Massimo for putting the links in one place)

Now that's out of the way, onto Barbecue itself. If you have a servlet container you can use the servlet supplied with Barbecue. CF Enterprise will suffice, as will anything like Tomcat or a J2EE server if you want. However, you may not want to have a servlet that any old muggins can call, generating random barcodes at will. It is for this purpose I wrote a simple java wrapper:

package net.sourceforge.barbecue;

import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import net.sourceforge.barbecue.env.EnvironmentFactory;
import net.sourceforge.barbecue.linear.code39.Code39Barcode;

public class BarcodeCF {

public void main() {};

public ByteArrayOutputStream getCFBarcode (String data) {
Barcode barcode = null;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
EnvironmentFactory.setDefaultMode();
barcode = new Code39Barcode(data, false, true);
barcode.setBarWidth(2);
barcode.setDrawingText(true);
} catch (BarcodeException e) {
// Error handling
throw new RuntimeException(e);
}
try {
// Let the barcode image handler do the hard work
BarcodeImageHandler.outputBarcodeAsJPEGImage(barcode, bos);
} catch (IOException e) {
// Error handling here
throw new RuntimeException(e);
}
return bos;
}

public void writeBarcode(String data, String filePath) {
// TODO Auto-generated method stub
ByteArrayOutputStream whatever = new BarcodeCF().getCFBarcode(data);
try {
FileOutputStream fos = new FileOutputStream(filePath);
whatever.writeTo(fos);
fos.close();
}
catch (IOException e) {
// Error handling here
throw new RuntimeException(e);
}
}
}

You may want to change the kind of barcode you create; this example uses the Code39Barcode class.

Calling it is simple. If we've used createobject or some other method of getting an instance of net.sourceforge.barbecue.BarcodeCF called BarcodeCF:

<cfset BarcodeCF.writeBarcode("Barcode Text Here","/barcode/location/here.jpg")>

The BarcodeCF class will write the image file to the desired location.

Somebody who's paying attention will be thinking, well why not just write the CF equivalent of that Java. Yes, you can if you have access to get the Barbecue jar into the classpath on the machine, but this gives you a class you can use with a dynamic classloader, like Spikes method.

 

 

Comments
Dave Phipps's Gravatar I just came across this blog entry and I am trying to make use of the java code for some barcode experiments I am doing. My java knowledge is a little scant so please bear with me. The java wrapper I copied and pasted and saved as a .java file. Do I need to compile it into a .class file or just save it as a .class file. When I try to compile it throws an error and I can't find the outputBarcodeAsJPEGImage() in the BarcodeImageHandler of barbecue. Any help would be appreciated as I can't seem to find any other examples of using bbq with cf.

Cheers,

Dave
# Posted By Dave Phipps | 1/26/07 5:55 AM
James Holmes's Gravatar You definitely need to compile it. In your IDE (or in your command line if that's what you're using) you need to make sure the source libraries for the barcode servlet are available. This is easiest with Eclipse or another good Java IDE, where you can add the libraries to the compile classpath and you can see if things are going to work. Post back if you keep having issues, or start a discussion here:

http://www.bifrost.com.au/forums/threads.cfm?forum...

or post on on CF-Talk at House of Fusion for more input.
# Posted By James Holmes | 1/26/07 8:59 AM
Giancarlo Gomez's Gravatar I am also trying to make use of your code and no luck. I created the Class file in Eclipse and added my external barbecue JAR file for all the imports and I get errors compiling.

Barcode cannot be resolved to a type   BarcodeCF/src   BarcodeCF.java   line 15
BarcodeException cannot be resolved to a type   BarcodeCF/src   BarcodeCF.java   line 22   
BarcodeImageHandler cannot be resolved   BarcodeCF/src   BarcodeCF.java   line 28   
e cannot be resolved   BarcodeCF/src   BarcodeCF.java   line 24   
The declared package "net.sourceforge.barbecue" does not match the expected package ""   BarcodeCF/src   BarcodeCF.java   line 1

I do have access to my classpath and placed the barbecue jar in it, can you do an example creating the equivalent in a CFC?
# Posted By Giancarlo Gomez | 9/29/07 4:12 PM
James Holmes's Gravatar The compile issues might be due to the way the project is set up. I just included the jar from the barbecue distro, not the source. This is also for 1.0.6d, not the 1.5 beta.

You can't just write a CFC to use Barbecue as you can't instantiate a barcode. You need to use a wrapper class that you can instantiate.
# Posted By James Holmes | 10/1/07 7:45 PM
Giancarlo Gomez's Gravatar Thanks for responding. Yeah we noticed it would not compile using the 1.5 beta. A friend of mine actually wrote a new class using the new 1.5 beta allowing to create the all the code types and allowing to output to all the versions. Haven't gotten to testing yet but when I do and it is all good I will go ahead and post it up on my blog.

http://fusecf.blogspot.com
# Posted By Giancarlo Gomez | 10/1/07 10:59 PM
BlogCFC was created by Raymond Camden. This blog is running version 5.5.1.