Code 128 Barcode string encode with ColdFusion

There are multiple ways to generate Code 128 barcodes. Barcode4j and Barbecue are two wonderful java projects that we can use with ColdFusion; both of them generate a barcode that can be saved as image files. Code 128 barcode also can be draw using a Code 128 barcode font. They are not that difficult to find, and there are even free Code 128 barcode fonts.

I guess different situations favor font over images or vice-versa. But as I’ve been working on an InDesign server project, I chose to go with the Code 128 font. With this font I can pass my barcode string and the font will draw it – if designers wish to change the size or other properties, they’ll still have the freedom to do so. But with images, I have to generate image files in the exact size, so all of them will link inside the document.

Unlike Code 39, Code 128 is high-density barcode and it stores an “encoded” string with start character, check character and a stopper. If you are using Barcode4j or Barbecue, you can pass the natural string and it will encode the string and generate the barcode image for you. But if you are using a font, you must encode the string into Code 128 format first. That’s the job of the following function. Pass a string, get it encoded, and pass that to your Code 128 font.

   1: <cffunction name="code128Encode" returntype="string" output="No">
   2:  <cfargument name="string" required="no" type="string" default="">
   3: <cfif REFind('^[0-9]+$',arguments.string)>
   4:  <!--- *********************************************************** --->
   5:  <!--- code128c : numbers only --->
   6:  <!--- *********************************************************** --->
   7:  <cfset local.startChr = 205>
   8:  <cfset local.endChr = 206>
   9:  <cfif len(arguments.string) mod 2>
  10:  <cfset arguments.string = "0#arguments.string#">
  11:  </cfif>
  12:  <cfset local.stringTotal = local.startChr - 100>
  13:  <cfset local.count = 1>
  14:  <cfset local.encoded = "">
  15:  <cfloop from="2" to="#len(arguments.string)#" step="2" index="i">
  16:  <cfset local.letter = (( asc(mid(arguments.string,i-1,1)) -48)*10) + ( asc(mid(arguments.string,i,1)) -48)>
  17:  <cfif val(local.letter)>
  18:  <cfif local.letter lt 95>
  19:  <cfset local.encoded = "#local.encoded##chr(local.letter + 32)#">
  20:  <cfelse>
  21:  <cfset local.encoded = "#local.encoded##chr(local.letter + 100)#">
  22:  </cfif>
  23:  <cfelse>
  24:  <cfset local.encoded = "#local.encoded##chr(194)#">
  25:  </cfif>
  26:  <cfset local.stringTotal = local.stringTotal + (local.letter * local.count)>
  27:  <cfset local.count = local.count + 1>
  28:  </cfloop>
  29:  <cfset local.chkChrVal = int(local.stringTotal mod 103)>
  30:  <cfif val(local.chkChrVal)>
  31:  <cfif local.chkChrVal lt 95>
  32:  <cfset local.chkChr = local.chkChrVal + 32>
  33:  <cfelse>
  34:  <cfset local.chkChr = local.chkChrVal + 100>
  35:  </cfif>
  36:  <cfelse>
  37:  <cfset local.chkChr = 194>
  38:  </cfif>
  39:  <cfset arguments.string = "#chr(local.startChr)##local.encoded##chr(local.chkChr)##chr(local.endChr)# ">
  40:  <cfreturn arguments.string>
  41:  <cfelse>
  42:  <!--- *********************************************************** --->
  43:  <!--- code128a and code128b --->
  44:  <!--- *********************************************************** --->
  45:  <cfif REFind('^[\x00-\x1F]',arguments.string)>
  46:  <!--- code128a : numbers, upper-case letters, and controls --->
  47:  <cfset local.startChr = 203>
  48:  <cfset local.endChr = 206>
  49:  <cfelse>
  50:  <!--- code128b : numbers, upper- and lower-case letters --->
  51:  <cfset local.startChr = 204>
  52:  <cfset local.endChr = 206>
  53:  </cfif>
  54:  <cfset local.string = "">
  55:  <cfloop from="1" to="#len(arguments.string)#" index="i">
  56:  <cfset local.letter = mid(arguments.string,i,1)>
  57:  <cfset local.thisasc = asc(local.letter)>
  58:  <cfif local.thisasc LT 32>
  59:  <!--- handle special characters --->
  60:  <cfif not StructKeyExists(local,'setA') and local.startChr gt 203>
  61:  <cfset local.string = "#local.string##Chr(201)##Chr( local.thisasc + 96)#">
  62:  <cfset local.setA = "">
  63:  <cfelse>
  64:  <cfset local.string = "#local.string##Chr( local.thisasc + 96)#">
  65:  </cfif>
  66:  <cfelse>
  67:  <cfset local.string = "#local.string##local.letter #">
  68:  </cfif>
  69:  </cfloop>
  70:  <cfset arguments.string = local.string>
  71: 
  72:  <cfset local.stringTotal = local.startChr - 100>
  73: <cfloop from="1" to="#len(arguments.string)#" index="i">
  74:  <cfset local.letter = asc(mid(arguments.string,i,1))>
  75:  <cfif local.letter lt 135 >
  76:  <cfset local.letter = local.letter - 32>
  77:  <cfelseif local.letter gt 134 >
  78:  <cfset local.letter = local.letter - 100>
  79:  </cfif>
  80:  <cfset local.letter = local.letter * i>
  81:  <cfset local.stringTotal = local.stringTotal + local.letter>
  82:  </cfloop>
  83:  <cfset local.chkChrVal = int(local.stringTotal mod 103)>
  84:  <cfif val(local.chkChrVal)>
  85:  <cfif local.chkChrVal lt 95>
  86:  <cfset local.chkChr = local.chkChrVal + 32>
  87:  <cfelse>
  88:  <cfset local.chkChr = local.chkChrVal + 100>
  89:  </cfif>
  90:  <cfelse>
  91:  <cfset local.chkChr = 194>
  92:  </cfif>
  93:  <cfset arguments.string = replace(arguments.string,' ',chr(194),'all')>
  94: <cfset arguments.string = "#chr(local.startChr)##arguments.string##chr(local.chkChr)##chr(local.endChr)# ">
  95:  <cfreturn arguments.string>
  96:  </cfif>
  97: </cffunction>
Show/Hide Line Numbers . Full Screen . Plain

 

10 Comments :
Saturday 28 June 2014 12:10 AM
Thanks for share this nice information! Your Sound is really good about "Code 128 Barcode string encode with ColdFusion". I am intimidated by the excellence of information. There are a bundle of good funds here. I am sure I will visit this position again soon.
I think, Then, you need to arrange to print that barcode, or incorporate it into your existing label or packaging.
Here is some information about code 128.
Carl
Monday 04 November 2013 05:44 PM
Am i missing something here? The code here works fine (i can display the characters just fine), but how do you get the actual barcode to display? If fonts need to be installed, do you install the font(s) in Windows font folder or do you use CF's Font Management on the admin console? I'm not having any luck with either. Also, how does this code know to even look for a particular font? it doesn't just stand out to me. Please advise. Thanks.
Tuesday 06 May 2014 01:22 PM
It you use that on an (cf)image, ColdFusion need direct access to the font. Yes, you have to register that with CF and then write the string with your font. I use this on PDF files. I have my PDF file from-field, formatted with the font and then fill the field with the newly created string. That gives me a PDF with the barcode.
Carl
Monday 04 November 2013 05:46 PM
also, can you put a demo button on this blog post so we can see the expected result?
Wednesday 12 September 2012 02:55 PM
This doesn't work for me either.
When I pass 8390 into this function, I get back Ísz􀀄Î􀀃
Only the 4th and 6th characters can be rendered in the Code 128 font.
Wednesday 12 September 2012 07:45 PM
8390 gave me Ísz[Î - that is the correct string.
Wednesday 12 September 2012 11:06 PM
Thanks. I wonder what causes the difference?
My results, when parsed by a code 128 font (from fonts4free) into a PDF form field; produces a barcode in which '[' and '' are coded. The rest of the string (ie the 'Ísz' and the 'Î') are just rendered in a plain serif text.
Could it be an incorrect 128 type ttf? I tried two different Code128 fonts with the same result.

Thursday 13 September 2012 12:20 PM
try the font form http://www.netlabels.com/_setup/128fonts.htm

Johnathan Griffiths
Thursday 16 August 2012 10:05 AM
I think your code may be slightly off. some values that I try to encode produce characters that are illegal in Code128 use. For example, encoding "0B09B2" Produces "Ë0B09B2ÆÎ" of which "Æ" is not supported. Likewise for the values "0B09C1" and "0B09C2" which result in "Ë0B09C1ÅÎ" and "Ë0B09C2ÂÎ" respectively ("Å" and "Â" not supported). For the most part, the script works, but it's not 100%.
Wednesday 22 August 2012 03:50 PM
Thanks Johnathan.. I updated the function. Now it should cover everything, unless i missed something again.