Create a Datamatrix 2D Barcode with Coldfusion and Barcode4j
Monday 17 September 2012 03:08 PM
During the last few days, I have had quite an enjoyable time trying to create barcodes. This post explains how to create a Datamatrix barcode using java4less, and it helped me to present a project before its deadline. So, “Thank you, Stillnet Studios!”
Here is another way to create Datamatrix 2D barcodes using barcode4j, which is an open source project.
You will need :
1. Barcode4j (download here)
2. Administrative access to Coldfusion MX or JavaLoader CFC (download here)
3. A cup of Ceylon tea (optional)
If you have administrative access, copy the barcode4j.jar file––which might be found under the lib folder––into "C:\ColdFusion8\wwwroot\WEB-INF\lib" or any other folder you choose. Then set the path in ColdFusion Administrator > Server Settings > Java and JVM under "ColdFusion Class Path." Next, restart the ColdFusion server. (If you are new to this, here is a complete hitchhiker's guide to the ColdFusion class path.)
Now, if you don't want to go through the horrible trouble of restarting the server, use the wonderful JavaLoader CFC that I show in the example below.
As you can see, I did not make a UDF out of it. I leave that decision up to you. Instead, I set all the values you will need at the top of the page for your convenience.
"Value" is what you want to display in your barcode. Since this a Datamatrix barcode, you can basically include the complete Gettysburg Address if wanted to. I tried, and it worked.
"SaveAt" is the location where you want the png barcode to be saved.
"JavaLoder" is the path to JavaLoder.cfc. You may have to change it.
"Resolution" is––you guessed it!––the resolution of the barcode image.
1: <cffunction name="DMBarcode" access="public" returntype="void" output="no" hint="Create Datamatrix Barcode"> 2: <cfargument name="value" hint="String encode in barcode." required="true"> 3: <cfargument name="file" hint="File Name to be saved. (xxx.eps, xxx.jpg or xxx.bmp file)" default="dm.png"> 4: <cfargument name="shape" hint="Shape of the Barcode (SQUARE, RECTANGLE, NONE)" default="SQUARE"> 5: <cfargument name="resoluton" hint="Resolution of JPG PNG output" default="50"> 6: <cfargument name="savepath" hint="Path of the image to be saved" default="#ExpandPath('./')#"> 7: <cfargument name="loaderpath" hint="JavaLoader.cfc path" default="JavaLoader"> 8: <cfset in = ArrayNew(1)> 9: <cfset in[1] = "#ExpandPath('./')#barcode4j.jar"> 10: <cfset loader = createObject("component", loaderpath).init(in)> 11: <cfset format = listlast(file,'.')> 12: <cfset Reader = loader.create("org.krysalis.barcode4j.impl.datamatrix.DataMatrixBean")> 13: <cfset sharpner = loader.create("org.krysalis.barcode4j.impl.datamatrix.SymbolShapeHint")> 14: <cfset out = CreateObject("java","java.io.FileOutputStream").Init(CreateObject("java","java.io.File").Init('#savepath##file#')) /> 15: <cfswitch expression="#format#"> 16: <cfcase value="eps"> 17: <!--- EPS file format ---> 18: <cfset canvas = loader.create('org.krysalis.barcode4j.output.eps.EPSCanvasProvider').init(out,0)> 19: <cfset ctype = "application/postscript"> 20: </cfcase> 21: <cfcase value="jpg"> 22: <!--- JPG file format ---> 23: <cfset BufferedImage = CreateObject('java',"java.awt.image.BufferedImage")> 24: <cfset canvas = loader.create('org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider').init(out, "image/jpeg", JavaCast('double',resoluton), BufferedImage.TYPE_BYTE_BINARY, false, 0)> 25: <cfset ctype = "image/jpeg"> 26: </cfcase> 27: <cfdefaultcase> 28: <!--- PNG file format ---> 29: <cfset BufferedImage = CreateObject('java',"java.awt.image.BufferedImage")> 30: <cfset canvas = loader.create('org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider').init(out, "image/x-png", JavaCast('double',resoluton), BufferedImage.TYPE_BYTE_BINARY, false, 0)> 31: <cfset ctype = "image/png"> 32: </cfdefaultcase> 33: </cfswitch> 34: <cfset Reader.setModuleWidth(JavaCast('int',1))> 35: 36: <!--- set the shape of the barcode ---> 37: <cfswitch expression="#shape#"> 38: <cfcase value="SQUARE"> 39: <cfset Reader.setShape(sharpner.FORCE_SQUARE)> 40: </cfcase> 41: <cfcase value="RECTANGLE"> 42: <cfset Reader.setShape(sharpner.FORCE_RECTANGLE)> 43: </cfcase> 44: <cfdefaultcase> 45: <cfset Reader.setShape(sharpner.FORCE_NONE)> 46: </cfdefaultcase> 47: </cfswitch> 48: 49: <cfset Reader.generateBarcode(canvas, JavaCast('string',value))> 50: <cfset canvas.finish()> 51: <cfset out.close()> 52: </cffunction>
Posted by Saman W Jayasekara at Friday 25 December 2009 01:09 AM
.
Barcode
.
ColdFusion
Tuesday 16 August 2011 12:58 PM
This is great, but I'm especially excited to see that more and more companies are personalizing their QR codes. Roughly 25% to 30% of the code can be altered, which leaves you some space for inputting your company logo or even text.
Great to have the instructions for this as well, I'll have to try it out!
Priya Rajarajan
Saturday 16 July 2011 08:29 AM
Hi,
Your code worked fine for me.But I have now encountered with a new problem
1.I created a PDF using Adobe Lifecycle designer ES2.
2.Name of the file is timesheetform.pdf anf it contained 4 values,first,lastname,deptnameand managername.
3.I wanted to create a 2D barcode for all the form valuesthrough cold fusion.
4.Everything is done,infact your code only I used for generating 2D barcode,everything went fine.
4.Now the problem is I want to combine all the form values to create a new 2D barcodefor the form values(i.e,firstname,lastname,deptname,managername in one single 2D barcode).
Kindly help me
Joseph
Tuesday 18 January 2011 11:41 PM
I have been scouring the Barcode4j website for the jar. Can you post instructions on how to compile it?
Joseph
Tuesday 18 January 2011 11:55 PM
I found it, never mind.
Thursday 20 January 2011 10:25 AM
This post got everything you need for a quick start http://cflove.org/2009/11/d-datamatric-barcode-with-barcodej-cfc.cfm