Create QR Barcode with ColdFusion and Zxing

Too often I write “It was too easy with ColdFusion”, and it’s becoming a cliché now. Making a QR barcode with ColdFusion was extremely easy too, especially when someone else had already blogged about it. I had nothing to dig out, nothing to investigate and hadn’t had the need for countless cups of coffee. I wish everything worked like that.

This uses Google zxing java library. I put the necessary classes into a single jar file, and you can download the example below. It uses JavaLoader to load to the Java classes. But if you are happily floating on Railo, you know that you don’t have to use JavaLoader. Railo is Cool.

Railo Code (Without JavaLoader)

   1: <cfset corefile     = ExpandPath('core.jar')>
   2: <cfset BarcodeFormat    = createObject('java','com.google.zxing.BarcodeFormat',corefile)>
   3: <cfset QRCodeWriter    = createObject('java','com.google.zxing.qrcode.QRCodeWriter',corefile).init() />
   4: <cfset MatrixToImageWriter    = createObject('java','com.google.zxing.client.j2se.MatrixToImageWriter',corefile)>
   5: <cfset QRcode     = QRCodeWriter.encode( 'http://cflove.org', BarcodeFormat.QR_CODE, 100, 100 )>
   6: <cfset image     = ImageNew( MatrixToImageWriter.toBufferedImage( QRcode ) )/>
   7: <cfimage action="writeToBrowser" source="#image#" format="png">
Show/Hide Line Numbers . Full Screen . Plain

With JavaLoader

   1: <cfset paths    = arrayNew(1)>
   2: <cfset paths[1]    = expandPath("core.jar")>
   3: <cfset loader    = createObject("component", "javaloader.JavaLoader").init(paths)>
   4: <cfset BarcodeFormat    = loader.create('com.google.zxing.BarcodeFormat')>
   5: <cfset QRCodeWriter    = loader.create('com.google.zxing.qrcode.QRCodeWriter').init()>
   6: <cfset MatrixToImageWriter    = loader.create('com.google.zxing.client.j2se.MatrixToImageWriter')>
   7: <cfset QRcode     = QRCodeWriter.encode( 'http://cflove.org', BarcodeFormat.QR_CODE, 100, 100 )>
   8: <cfset image     = ImageNew( MatrixToImageWriter.toBufferedImage( QRcode ) )>
   9: <cfimage action="writeToBrowser" source="#image#" format="png">
Show/Hide Line Numbers . Full Screen . Plain

Download everything below.

Download (qr.zip)
10 Comments :
Tinu
Sunday 12 April 2015 11:14 AM
sorry for the doulbe ... but your page crashed with an error NO SMTP server defined
Tinu
Sunday 12 April 2015 11:13 AM
Hi

Thanks for the short and concise info. However, here (CF10 on Win2008 R2), the kast line, the image conversion, throws an error:

---
coldfusion.image.Image is not a supported variable type. The variable is expected to contain binary data. (Application)
---

My cfscript code there (all objects and vars are in var l = {} )

---
l.image = ImageNew (l.MatrixToImageWriter.toBufferedImage (l.QRcode));
---

Any clue to fix that?

Thanks

Tinu
Tinu
Sunday 12 April 2015 11:12 AM
Hi

Thanks for the short and concise info. However, here (CF10 on Win2008 R2), the kast line, the image conversion, throws an error:

---
coldfusion.image.Image is not a supported variable type. The variable is expected to contain binary data. (Application)
---

My cfscript code there (all objects and vars are in var l = {} )

---
l.image = ImageNew (l.MatrixToImageWriter.toBufferedImage (l.QRcode));
---

Any clue to fix that?

Thanks

Tinu
Mike
Wednesday 16 January 2013 02:49 PM
more specifically the javaloader folder? I am getting this:

Could not find the ColdFusion component or interface javaloader.JavaLoader.
Ensure that the name is correct and that the component or interface exists.
Wednesday 16 January 2013 11:53 PM
Also you can avoid using JavaLoader, if you can copy the code.jar file into your coldfusion installation folder and restart the server. Then you can replace line 1 to 6 with
<cfset BarcodeFormat    = createObject('java','com.google.zxing.BarcodeFormat')>
<cfset QRCodeWriter    = createObject('java','com.google.zxing.qrcode.QRCodeWriter').init() />
<cfset MatrixToImageWriter    = createObject('java','com.google.zxing.client.j2se.MatrixToImageWriter')>
Wednesday 16 January 2013 11:48 PM
I guess you are trying this in a sub folder. If that is the case, provide the full path to javaloader. Let's say If your path is mysite.com/abc/test/ then path should be abc.test.javaloader.JavaLoader and the code should looks something like
<cfset loader = createObject("component", "abc.test.javaloader.JavaLoader").init(paths)>
Mike
Wednesday 16 January 2013 02:44 PM
I have having trouble. Where do I put the files from this download?
Vishal Patel
Monday 12 November 2012 08:35 AM
I'm new to managing ColdFusion Server. What do I need to do in order to be able to use this? Thanks for any help you can provide.
Monday 12 November 2012 10:25 AM
download demo and run it. it should works.
pim_man
Friday 06 January 2012 04:14 PM
Thank you :P