Volume 9, Issue 2
Tricks of the Trade
In and Out
Download This Issue
Tricks of the Trade
We encounter a wide range of barcodes nearly every day. At www.barcodeman.com/info/c39_1.php one can find a number of barcode specifications. In one popular code--Code 39--each character is made up of 9 bars, alternating black and white, 3 of which are wider than the others. A single character therefore consists of 5 black bars and 4 white bars. The ratio of the bar widths, , can range from 2.2:1 to 3:1 and the total width of a character is . The intercharacter gap (the space between each barcode character) is undefined, but is usually equivalent to a narrow white bar.
The Code 39 barcode symbology supports 43 characters plus *, which is used as a delimiter or start/stop character. Note that the alphabetic characters are all upper case. Here is the bar width configuration table. A zero corresponds to a narrow bar and a one to a wide bar.
Corresponding to a single character, the following code generates a sequence of rectangles of alternating black and white color (via MapIndexed and Mod) according to the bar width configuration table.
The position of each successive rectangle is computed using FoldList (computing a cumulative sum starting from pos). Since the alphabetic characters are all upper case, lowercase input is converted using ToUpperCase. The replacement rule is used to map from zeros and ones to narrow bars (of unit width) and wide bars (of width ), respectively.
Adding 1 for the intercharacter gap, the total width of a character is . Generating the barcode for a string of characters is straightforward, again using MapIndexed. StringJoin is used to prepend and append the start/stop character *. The default ratio of widths is 3 (via ).
Here is the barcode of Mathematica in Code 39.
By adding catch all code for missing characters, we can display such characters as a "graphical error."
For example, # and ^ are not in Code 39 and are shown in red.
About Mathematica | Download Mathematica Player
Copyright © Wolfram Media, Inc. All rights reserved.