Barcode generation

This will become a "real" page - for now it's just a part of chat transcript (2003-07-09), so the info doesn't get lost... RS

 suchenwi    But one basic question: in barcodes like Code 128 (EAN?), each digit is a black and a white bar, the added thickness of which two is constant, right?
 kroc        Richard: each digit (or letter for some barcodes) is a set of bars and spaces...
 suchenwi    How many?
 kroc        it depends of barcode type.
 kroc        Six per char for Code 128 for a total of 11 units.
 kroc        Here is a sample :
             B == bar and S == space
             digit represents width.
             char == BSBSBS
             a = 121124
             ^ =  431111
             etc..
 kroc        So, graphic representation will be:
             a = "X  X XX    "
             ^ = "XXXX   X X "
 kroc        To build the barcode, you join all char bars and spaces. It also need start and end symbol, sometimes an ending control char, and must respect minimum width requirements to be read.
 suchenwi    Ah - and for a quick shot one could draw canvas lines for the bars...
 suchenwi    Check digit too, if I remember right.
 kroc        It's more accurate to draw on a photo image like that :
             foreach {bar space} $code {
                set larg expr ($bar * $unit)+$debut
                img-$nbimg put #000000 -to $debut 0 $larg $fin
                set debut expr ($space*$unit) + $larg
             }
 suchenwi    Do you have a link to specifications of the bar-space width patterns?
 kroc        mb: to answer your question, ean8 and Code 128 aren't the same thing at all.
     * kroc checking... 
 kroc        http://www.ean-int.org 
 kroc        http://www.barcodeisland.com/symbolgy.phtml 

Here is a proc that returns Code 128 bars and spaces for a given string :

 proc String2C128 { signes } {
    set code ""
    foreach part [split $signes {}] {
        switch -- $part {
            \x20 {append code "212222" ; # 0 }
            \x21 {append code "222122"}
            \x22 {append code "222221"}
            \x23 {append code "121223"}
            \x24 {append code "121322"}
            \x25 {append code "131222"}
            \x26 {append code "122213"}
            \x27 {append code "122312"}
            \x28 {append code "132212"}
            \x29 {append code "221213"}
            \x2A {append code "221312" ; # 10 }
            \x2B {append code "231212"}
            \x2C {append code "112232"}
            \x2D {append code "122132"}
            \x2E {append code "122231"}
            \x2F {append code "113222"}
            \x30 {append code "123122"}
            \x31 {append code "123221"}
            \x32 {append code "223211"}
            \x33 {append code "221132"}
            \x34 {append code "221231" ; # 20 }
            \x35 {append code "213212"}
            \x36 {append code "223112"}
            \x37 {append code "312131"}
            \x38 {append code "311222"}
            \x39 {append code "321122"}
            \x3A {append code "321221"}
            \x3B {append code "312212"}
            \x3C {append code "322112"}
            \x3D {append code "322211"}
            \x3E {append code "212123" ; # 30 }
            \x3F {append code "212321"}
            \x40 {append code "232121"}
            \x41 {append code "111323"}
            \x42 {append code "131123"}
            \x43 {append code "131321"}
            \x44 {append code "112313"}
            \x45 {append code "132113"}
            \x46 {append code "132311"}
            \x47 {append code "211313"}
            \x48 {append code "231113" ; # 40 }
            \x49 {append code "231311"}
            \x4A {append code "112133"}
            \x4B {append code "112331"}
            \x4C {append code "132131"}
            \x4D {append code "113123"}
            \x4E {append code "113321"}
            \x4F {append code "133121"}
            \x50 {append code "313121"}
            \x51 {append code "211331"}
            \x52 {append code "231131" ; # 50 }
            \x53 {append code "213113"}
            \x54 {append code "213311"}
            \x55 {append code "213131"}
            \x56 {append code "311123"}
            \x57 {append code "311321"}
            \x58 {append code "331121"}
            \x59 {append code "312113"}
            \x5A {append code "312311"}
            \x5B {append code "332111"}
            \x5C {append code "314111" ; # 60 }
            \x5D {append code "221411"}
            \x5E {append code "431111"}
            \x5F {append code "111224"}
            \x60 {append code "111422"}
            \x61 {append code "121124"}
            \x62 {append code "121421"}
            \x63 {append code "141122"}
            \x64 {append code "141221"}
            \x65 {append code "112214"}
            \x66 {append code "112412" ; # 70 }
            \x67 {append code "122114"}
            \x68 {append code "122411"}
            \x69 {append code "142112"}
            \x6A {append code "142211"}
            \x6B {append code "241211"}
            \x6C {append code "221114"}
            \x6D {append code "413111"}
            \x6E {append code "241112"}
            \x6F {append code "134111"}
            \x70 {append code "111242" ; # 80 }
            \x71 {append code "121142"}
            \x72 {append code "121241"}
            \x73 {append code "114212"}
            \x74 {append code "124112"}
            \x75 {append code "124211"}
            \x76 {append code "411212"}
            \x77 {append code "421112"}
            \x78 {append code "421211"}
            \x79 {append code "212141"}
            \x7A {append code "214121" ; # 90 }
            \x7B {append code "412121"}
            \x7C {append code "111143"}
            \x7D {append code "111341"}
            \x7E {append code "131141"}
            \x7F {append code "114113"}
            \x80 {append code "114311"}
            \x81 {append code "411113"}
            \x82 {append code "411311"}
            \x83 {append code "113141"}
            \x84 {append code "114131" ; # 100 }
            \x85 {append code "311141"}
            \x86 {append code "411131" ; # FNC1 }
            \x87 {append code "211412" ; # Start A }
            \x88 {append code "211214" ; # Start B }
            \x89 {append code "211232" ; # Start C }
            \x8A {append code "23311121" ; # End }
        }
    }
    return [split $code {}]
 }

Spun off from here: Code 39 generation

Jan 19,2008 SRIV Heres the remainder of the code needed to generate Code128 B.

http://server.linuxsys.net/images/code128B.png

 #One sees 6 parts in each symbol (3 bars, 3 gaps),  This part 
 #converts an input string into a bar-gap sequence, with added start, 
 #checksum and stop characters, followed by the termination bar
 #This renders a bar-gap sequence into a photo image

 proc c128img {c128} {
        set offset 20 ; #add some whitespace to the left & right of the barcode as required
        set scale 1 ;# change this to 2 for a 2X larger barcode
        set width [expr {round(([string length $c128] * $scale) + $offset)}]
        set height 60
        set im [image create photo -width $width -height $height]
        $im put white -to 0 0 $width $height

        foreach {bar space} $c128 {
          set larg [expr ($bar * $scale)+$offset]
          $im put #000000 -to $offset 0 $larg $height
          set offset [expr ($space*$scale) + $larg]
        }
        set im
 }

 proc code128checksum {data} {
        set pos 1
        #change next line to 103 for code128A or 105 for code 128C
        set sum 104
        foreach char [split $data ""] {
                scan $char %c decimal
                incr decimal -32
                set mult [expr $decimal * $pos]
                incr sum $mult
                incr pos
        }
        set sum [expr $sum % 103]
        incr sum 32
        set sum [binary format c $sum]
        #change the \x88 on next line to \x87 for Code128A or \x89 for code128C
        return "\x88${data}${sum}\x8a"
 }
        
        

 # Demo:
 package require Tk
 set txt "ABCDE-12345"
 set bc [String2C128 [code128checksum $txt]]
 set im [c128img $bc]
 label .l1 -anchor c -text  $txt
 label .l2 -anchor c -image $im
 pack  .l1 .l2


ALX 2014-09-11 21:59:00:

tzint - tcl package for libzint barcode encoding library (no Tk needed)