How to calculate the width (in dots) of this zpl barcode

barcodezebra-printerszpl

^XA
^FO0,90^AD^BY3
^BCN,100,Y,N,Y,A
^MD10
^FD1458622235534^FS
^XZ

Using Label Viewer I was able to play around with ZPL and after reading some documentation figured out that ^FO0,90 is what positions the barcode. 0 is for the number of dots from the left and 90 is for the number of dots from the top.

My question is, how can the width of the above bar code be calcuated in dots? If I know the width of the barcode, I can then center it programmatically.

The printer's resolution is 203dpi and given a 3' (width) x 2' (length) tag, that is 610 dots across.

What is the formula to calculate the width of a ZPL Barcode?

Best Answer

The Code 128 barcode includes three modes. The "A" in your ^BC means that the barcode algorithm will automatically select the best mode based on the placement of where the letters are mixed in with the numbers, and/or how many numbers are in a row. This results in barcodes that vary in length depending on their content.

You can change that A to an N. This will make the width easy to estimate, even if it's not the most space efficient.

For the N option: count 106 dots, total, for the bookends, and 33 dots for each digit.

For the N + Subset C option: If you can say for sure that you will always have an even number of digits (no letters, spaces, or other symbols). You can force the barcode into subset C mode. Count 104 dots for the bookends, and 33 dots for every two digits. But you must have an even number of digits.

^BCN,100,Y,N,Y,N
^FD>;1422335544^FS

The >; tells ZPL to render this as subset C.

If you know that you will need to support alphanumeric barcodes, then stick with the A. This is where things get harder.

You will need to model the barcode algorithm in your programming language of choice. Perhaps you could find an existing library, as Code 128 is a very popular barcode. Use the library to render the barcode in memory. You may have to play with the parameters to get it to produce a barcode equal to your ZPL output. Then have your program use the output of the library to determine the width of the barcode.

If your barcode is always going to be 13 digits, then automatic centering is a moot point. The barcode will always be the same width, even in automatic mode. Find the ^FO that looks like it's centered on the label, and stick with that. This barcode is 368 dots wide. Try ^FO121,90.

I didn't calculate the width of the barcode, I measured it by rendering the label to a Gif and then examining the label in a paint program that would let me count the dots.

Related Topic