Sep 30, 2014

Create QR Code with ZXing library and ColdFusion

Using a combination of ColdFusion, ZXing, and JavaLoader you can create QR codes very easily.
Jermaine Gonzales
Simple QR creation with error correction using the ZXING library and javaloader.
ColdFusion 11
ZXING jar files:
The ZXING library used will be influenced by the ColdFusion library CF8 = ZXING core 2.3.0.
This was put together by following a few samples currently floating around, I was unable to find one that used the error correction however.
<h1>Create QR Code</h1>
<form action="#cgi.script_name#" method="post">
<label for="bcData">Data String:</label>
<textarea id="bcData" name="bcData" rows="3" cols="80"><cfif isDefined("form.bcData")>#trim(form.bcData)#</cfif></textarea>
<br />
<label for="size">Size:</label>
<input type="text" id="size" name="size" <cfif isDefined("form.size")> value="#trim(form.size)#"</cfif> />
<br />
<label for="errorC">Error Correction:</label>
<select id="errorC" name="ErrorC" >
<option value="L" <cfif isDefined("form.errorC") AND form.errorC IS "L">selected="selected"</cfif>>L</option>
<option value="M" <cfif isDefined("form.errorC") AND form.errorC IS "M">selected="selected"</cfif>>M</option>
<option value="Q" <cfif isDefined("form.errorC") AND form.errorC IS "Q">selected="selected"</cfif>>Q</option>
<option value="H" <cfif isDefined("form.errorC") AND form.errorC IS "H">selected="selected"</cfif>>H</option>
<br />
<input type="submit" name="generateCode" value="Create QR Code" />

if ( isDefined("form.bcData") AND len( trim(form.bcData) ) ) {
variables.paths = arrayNew(1);
variables.paths[1] = expandPath("\wwwroot\zxing\core-3.1.0.jar");
variables.paths[2] = expandPath("\wwwroot\zxing\javase-3.1.0.jar");
variables.loader = createObject("component", "wwwroot.javaloader.JavaLoader").init( paths );
variables.barCodeFormat = loader.create('');
variables.MatrixToImageWriter = loader.create('');
variables.EncodeHintType = loader.create('');

//setup the error correction level for the QR code
variables.errorCorrectionLevel = loader.create('');
//use a java hashtable for hints information
variables.javaHash = loader.create("java.util.Hashtable").init();

//determine error correction level and set

if ( isDefined("form.errorC") ) {
switch(form.errorC) {
case "L":
variables.javaHash.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
//writeOutput("Choose L");
case "M":
variables.javaHash.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
//writeOutput("Choose M");
case "Q":
variables.javaHash.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.Q);
//writeOutput("Choose Q");
case "H":
variables.javaHash.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
//writeOutput("Choose H");
variables.javaHash.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.Q);
//writeOutput("Choose Default");

// set the generated code size
if ( isDefined("form.size") AND len(trim(form.size)) AND isNumeric(trim(form.size)) ){
variables.useSize = trim(form.size);
else {
variables.useSize = 200;

//load the QR Writer and create the QR Code
try {
variables.QRCodeWriter = loader.create('').init();
variables.QRcode = variables.QRCodeWriter.encode( trim(form.bcData), variables.barCodeFormat.QR_CODE, variables.useSize, variables.useSize, variables.javaHash );
variables.imageQR = ImageNew( variables.MatrixToImageWriter.toBufferedImage( QRcode ) );

//create image and display the QR image
imageWrite(variables.imageQR, "images/genQR.jpg", 1);
writeOutput('<img src="images/genQR.jpg" />');
catch ( any e ) {
//if something happens an exception is thrown at the java level so ColdFusion will time out if it tries to use the cfcatch.message and cfcatch.detail. This probably could be handled better but for now this will work. writeOutput("There was an error creating the QR Code");

//examples to create a different barcodes
//load the PDF417 Writer and create the PDF417 Code
try {
variables.PDF417CodeWriter = loader.create('').init();
variables.PDF417code = PDF417CodeWriter.encode( form.bcData, BarcodeFormat.PDF_417, variables.useSize, variables.useSize );
variables.imagePDF417 = ImageNew( variables.MatrixToImageWriter.toBufferedImage( PDF417code ) );

//create image and display the QR image
imageWrite(variables.imagePDF417, "images/genPDF417.jpg", 1);
writeOutput('<img src="images/genPDF417.jpg" />');
catch ( any e ) {
writeOutput("There was an error creating the PDF417 Code");
//Load the AZTEC write and create the AZTEC code
try {
variables.AZTECCodeWriter = loader.create('').init();
variables.AZTECcode = AZTECCodeWriter.encode( form.bcData, BarcodeFormat.AZTEC, variables.useSize, variables.useSize );
variables.imageAZTEC = ImageNew( variables.MatrixToImageWriter.toBufferedImage( AZTECcode ) );
//create image and display the AZTEC image
imageWrite(variables.imageAZTEC, "images/genAZTEC.jpg", 1);
writeOutput('<img src="images/genAZTEC.jpg" />');
catch ( any e ) {
writeOutput("There was an error creating the AZTEC Code");



1 comment:

Anonymous said...

Perfect! Cheers!