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:
- http://www.macromedia.com/support/flex/ts/documents/java_awt_headless.htm
- http://www.doughughes.net/index.cfm/page-blogLink/entryId-29
(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.

Cheers,
Dave
http://www.bifrost.com.au/forums/threads.cfm?forum...
or post on on CF-Talk at House of Fusion for more input.
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?
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.
http://fusecf.blogspot.com