2D datamatrix Barcode with Barcode4j, CFC

Here I created a CFC wrapper from the previous code for creating a datamarix barcode with barcode4j. You can output EPS, PNG and JPG files, but EPS is the best format if you are planning to use it inside PDF, it can reduce printing time if you are using a digital printer. Even though you can create JPG output, it can seriously affect the reading part. In other words, using JPG is not an very good idea.

   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>
Show/Hide Line Numbers . Full Screen . Plain

Download (bc.zip)
11 Comments :
George
Thursday 04 June 2015 02:22 PM
Hi, I tried to use the example but i got this error

Could not find the designated method Could not find the method 'Constructor' in the class org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider
The error occurred on line 351.

Have you seen this before?

Regards
Billy Co
Thursday 03 May 2012 12:12 PM
Thanks a bunch for this example. This has already saved me a bunch of time and I'm tweaking it for a warehouse inventory application.
When I try to encode the following string...
R13072*SOY SAUCE, GRANULATED*134679461*02/28/12*C158*PK-50*RAW*WHEAT SOY*
...I get an illegal character error, apparently on the comma in the string.

I remove the comma, and it generates a 2D barcode. Good...except that when I scan the barcode it returns the following string:
R13072*SOY SAUCE GRANULATED*1MAGVYA2YF3R13QP2WVJ5C2X483HOXX*RAW*WHEAT SOY*

I thought that the slashes in the data and the hyphen in the 'PK-50' string were causing havoc, but the following string alone encodes correctly:
134679461*02/28/12*C158*PK-50*RAW*WHEAT SOY*

Do you have any suggestions?
jacki felice
Thursday 28 April 2011 11:08 AM
THANK you for your POSTING SAM this has saved me a lot of work and with tiny tweeks is working GREAT. I was wondering HOW to make the DIMENSIONS of the Barcode all the same size. They appear in all different sizes. Do I need to "PAD" the string with a symbol like "*" or is there a way to setup a new attribute where I could pass in the Width and Height of the desired 2d Datamatrix barcode. Any Help here would be GREAT.
Saturday 11 December 2010 01:41 PM
Cool. I can't seem to encode much more than 2142-2200 characters (I used a Lorem Ipsum generator) with out getting an exception:
"Can't find a symbol arrangement that matches the message. Data codewords: 1560","RAW_TRACE at cfdatamatrix2ecfc705079265$funcDMBARCODE.runFunction(/home/projects/htdocs/dev.projects.archinetonline.com/ssl/bc/datamatrix.cfc:57"

Not sure why. This is really cool stuff though!
Saturday 11 December 2010 01:48 PM
It seems so. I tested up to 2430 and a bit.. And i got the same error. The problem is, my poor old barcode reader even can’t read 2430 characters.
Saturday 11 December 2010 02:07 PM
Good Point... there is obviously a practical limit to this size of these in relation to the actual barcode reader. I do find it interesting to use this as a means of encoding larger amounts of data for other purposes. Thanks again!

Bryan Rice
Saturday 11 December 2010 01:25 AM
This is really cool. Just wanted to let that this doesn't seem to work with JavaLoader version 1.0 on CF 9.1. I get an error "The following error was encountered: /bc/tags. Ensure that you have specified a valid tag library.The CFML compiler was processing:A cfimport tag beginning on line 20, column 2.
" Just wanted to let you know. Thanks for such a cool example.
Saturday 11 December 2010 09:49 AM
Thanks for letting me know! i just fixed it.
Saturday 11 December 2010 12:52 PM
Cool. One minor issue - in your new zip file you put the JavaLoader files in a folder named "javaloader" (all lowercase) but in the datamatrix.cfc you have the default path for the JavaLoader argument set to "JavaLoader.JavaLoader"... so it tries to look for the JavaLoader.cfc in a folder named "JavaLoader" (uppercase "J" and "L") and so your createObject on loaderpath fails. Easy to fix, but just wanted to let you know.

BTW,What is the data limit that a 2D datamatrix barcode can encode? Thanks again- great work!

Saturday 11 December 2010 01:20 PM
Thanks. Fixed that too!

There is no specific limit on the content. Barcode image size expands as content get bigger and even it can go beyond the reading area of the barcode reader.

Mike Johnson
Monday 08 February 2010 09:18 PM
This works great.
Would there be an easy way to change the cfc to generate 1D codes? I have looking through the javadocs trying to figure it out but i am not getting any where.