Author Topic: (Challenge) Create a barcode generator  (Read 7981 times)

0 Members and 1 Guest are viewing this topic.

Andrea

  • Water Moccasin
  • Posts: 2372
Re: (Challenge) Create a barcode generator
« Reply #15 on: October 08, 2009, 12:32:10 PM »
oh ..I see..

thanks.

Now,..I'll try to make a barcode reader. (if (not (already done))....

 :|
Keep smile...

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: (Challenge) Create a barcode generator
« Reply #16 on: October 08, 2009, 03:17:07 PM »
well....nice ElpanovEvgeniy ..and Tim..

but we got diffrent result on start code..


It is my error!
I as used barcod-128b, but by mistake, the beginning code has put barcod-128a
The code has already been corrected, but you have written the answer earlier.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: (Challenge) Create a barcode generator
« Reply #17 on: October 08, 2009, 03:39:24 PM »
Here is my 128B with the check character.

Code: [Select]
;|
Specification
A Code 128 barcode will have six sections:
    * Quiet Zone
    * Start Character
    * Encoded Data
    * Check Character
    * Stop Character
    * Quiet Zone
The check character is calculated from a weighted sum (modulo 103) of all the characters.
|;

;;  CAB 10.08.09  Code 128B
(defun c:BarCode (/
                  str
                              ;BarWidthUnits
                              ;BarHeight
                  QuietZoneWidth
                  pt
                 )
  (and
    (setq str (getstring t "\nEnter text for Barcode: "))
    ;;  may need to trim leading & trailing spaces or tabs?
    (or BarWidthUnits
        (setq BarWidthUnits (getint "\nEnter Unit width for each one Bar: "))
    )
    (or BarHeight (setq BarHeight (getdist "\nEnter Height of Barcode: ")))
    (setq QuietZoneWidth 0.25)

    (setq pt (getpoint "\nPick insert point, lower left."))

    (AddBarCode str BarWidthUnits BarHeight QuietZoneWidth pt)
  )
  (princ)
)




(defun AddBarCode (str BarWidthUnits BarHeight QuietZoneWidth pt /
                   code letter codelist CheckDigitW CheckDigit BCdata idx )
  (setq BCdata
        '((" " 212222)("!" 222122)("\"" 222221)("#" 121223)("$" 121322)("%" 131222)("&" 122213)
          ("\'" 122312)("(" 132212)(")" 221213)("*" 221312)("+" 231212)("," 112232)("-" 122132)
          ("\." 122231)("/" 113222)("0" 123122)("1" 123221)("2" 223211)("3" 221132)("4" 221231)
          ("5" 213212)("6" 223112)("7" 312131)("8" 311222)("9" 321122)(":" 321221)(";" 312212)
          ("<" 322112)("=" 322211)(">" 212123)("?" 212321)("@" 232121)("A" 111323)("B" 131123)
          ("C" 131321)("D" 112313)("E" 132113)("F" 132311)("G" 211313)("H" 231113)("I" 231311)
          ("J" 112133)("K" 112331)("L" 132131)("M" 113123)("N" 113321)("O" 133121)("P" 313121)
          ("Q" 211331)("R" 231131)("S" 213113)("T" 213311)("U" 213131)("V" 311123)("W" 311321)
          ("X" 331121)("Y" 312113)("Z" 312311)("[" 332111)("\\" 314111)("]" 221411)("^" 431111)
          ("_" 111224)("`" 111422)("a" 121124)("b" 121421)("c" 141122)("d" 141221)("e" 112214)
          ("f" 112412)("g" 122114)("h" 122411)("i" 142112)("j" 142211)("k" 241211)("l" 221114)
          ("m" 413111)("n" 241112)("o" 134111)("p" 111242)("q" 121142)("r" 121241)("s" 114212)
          ("t" 124112)("u" 124211)("v" 411212)("w" 421112)("x" 421211)("y" 212141)("z" 214121)
          ("{" 412121)("|" 111143)("}" 111341)("~" 131141) (95 114113) (96 114311) (97 411113)
          (98  411311)(99  113141) (100 114131) (101 311141)(102 411131) ))
  
  (setq CodeList (mapcar (function(lambda(x)
                            (setq CheckDigitW (cons (vl-position (assoc (chr x) BCdata) BCData) CheckDigitW))
                            (cadr(assoc (chr x) BCdata)) )) (vl-string->list str)))

  
  
  (defun MakeBar (letter / width code BarFlag)
    (setq BarFlag t)
    (foreach code (vl-string->list (itoa Letter))
      (setq code  (atoi (chr code))
            width (* code BarWidthUnits))
      (if BarFlag
          (entmakex
             (list '(0 . "LWPOLYLINE")
                   '(100 . "AcDbEntity")
                   '(100 . "AcDbPolyline")
                   (cons 43 width)
                   '(90 . 2)
                   (cons 10 (polar pt 0 (/ width 2.)))
                   (cons 10 (polar (polar pt 0 (/ width 2.)) (/ pi 2) BarHeight))
             ))
      )
      (setq pt (polar pt 0 width)
            BarFlag (not BarFlag)
      )
    )
  )

  
  (setq CheckDigitW (reverse CheckDigitW))
  (makebar 211214) ; Start 128B
  (setq CheckDigit 104)  ; Start 128B
  (setq idx 1)
  (foreach letter CodeList
     (makebar letter)
    (setq CheckDigit (+ CheckDigit (* idx (nth (1- idx) CheckDigitW))))
    (setq idx (1+ idx))
   )
  (setq CheckDigit (rem CheckDigit 103))
  (makebar (cadr (nth CheckDigit BCdata)))  ; add the Check Character
  (makebar 2331112) ; Stop
  (princ)
)

<edit: bug in check character fixed, I think>
« Last Edit: October 09, 2009, 05:06:40 PM by CAB »
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: (Challenge) Create a barcode generator
« Reply #18 on: October 09, 2009, 01:57:27 AM »
Gymnastics with a cup of morning coffee...  :-)

Code: [Select]
(defun c:b2 (/ A S X Y)
 ;; By ElpanovEvgeniy
 ;; barcod-128-B
 (setq s '(212222    222122    222221    121223    121322    131222    122213    122312
           132212    221213    221312    231212    112232    122132    122231    113222
           123122    123221    223211    221132    221231    213212    223112    312131
           311222    321122    321221    312212    322112    322211    212123    212321
           232121    111323    131123    131321    112313    132113    132311    211313
           231113    231311    112133    112331    132131    113123    113321    133121
           313121    211331    231131    213113    213311    213131    311123    311321
           331121    312113    312311    332111    314111    221411    431111    111224
           111422    121124    121421    141122    141221    112214    112412    122114
           122411    142112    142211    241211    221114    413111    241112    134111
           111242    121142    121241    114212    124112    124211    411212    421112
           421211    212141    214121    412121    111143    111341    131141
          )
       s (strcat "211214"
                 (apply (function strcat)
                        (mapcar (function (lambda (a) (itoa (nth (- a 32) s))))
                                (vl-string->list (getstring "\n Enter text:  "))
                        ) ;_  mapcar
                 ) ;_  apply
                 "2331112"
         ) ;_  strcat
       y (getpoint "\n Start point:  ")
       x (car y)
       y (cadr y)
 ) ;_  setq
 (repeat (1+ (/ (strlen s) 2))
  (if (setq a (atoi (substr s 1 1)))
   (entmakex (list '(0 . "LWPOLYLINE")
                   '(100 . "AcDbEntity")
                   '(100 . "AcDbPolyline")
                   (cons 43 a)
                   '(90 . 2)
                   (list 10 (+ x (/ a 2.)) y)
                   (list 10 (+ x (/ a 2.)) (+ y 50))
             ) ;_  list
   ) ;_  entmakex
  ) ;_  if
  (setq x (+ a x (atoi (substr s 2 1)))
        s (substr s 3)
  ) ;_  setq
 ) ;_  repeat
)
« Last Edit: October 09, 2009, 02:14:22 AM by ElpanovEvgeniy »

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: (Challenge) Create a barcode generator
« Reply #19 on: October 09, 2009, 03:25:55 AM »
Has added Check Digit Calculation

Code: [Select]
(defun c:b2 (/ A S X Y)
 ;; By ElpanovEvgeniy
 ;; barcod-128-B
 ;; add check character
 (setq s '(212222    222122    222221    121223    121322    131222    122213    122312
           132212    221213    221312    231212    112232    122132    122231    113222
           123122    123221    223211    221132    221231    213212    223112    312131
           311222    321122    321221    312212    322112    322211    212123    212321
           232121    111323    131123    131321    112313    132113    132311    211313
           231113    231311    112133    112331    132131    113123    113321    133121
           313121    211331    231131    213113    213311    213131    311123    311321
           331121    312113    312311    332111    314111    221411    431111    111224
           111422    121124    121421    141122    141221    112214    112412    122114
           122411    142112    142211    241211    221114    413111    241112    134111
           111242    121142    121241    114212    124112    124211    411212    421112
           421211    212141    214121    412121    111143    111341    131141    114113
           114311    411113    411311    113141    114131    311141    411131
          )
       x 104
       y 0
       s (strcat "211214"
                 (apply (function strcat)
                        (mapcar (function (lambda (a / i)
                                           (setq i (if (< a 145)
                                                    32
                                                    145
                                                   ) ;_  if
                                                 y (1+ y)
                                                 x (+ x (* (- a i) y))
                                           ) ;_  setq
                                           (itoa (nth (- a i) s))
                                          ) ;_  lambda
                                ) ;_  function
                                (vl-string->list (getstring "\n Enter text:  "))
                        ) ;_  mapcar
                 ) ;_  apply
                 (itoa (nth (rem x 103) s))
                 "2331112"
         ) ;_  strcat
       y (getpoint "\n Start point:  ")
       x (car y)
       y (cadr y)
 ) ;_  setq
 (repeat (1+ (/ (strlen s) 2))
  (if (setq a (atoi (substr s 1 1)))
   (entmakex (list '(0 . "LWPOLYLINE")
                   '(100 . "AcDbEntity")
                   '(100 . "AcDbPolyline")
                   (cons 43 a)
                   '(90 . 2)
                   (list 10 (+ x (/ a 2.)) y)
                   (list 10 (+ x (/ a 2.)) (+ y 50))
             ) ;_  list
   ) ;_  entmakex
  ) ;_  if
  (setq x (+ a x (atoi (substr s 2 1)))
        s (substr s 3)
  ) ;_  setq
 ) ;_  repeat
)

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: (Challenge) Create a barcode generator
« Reply #20 on: October 09, 2009, 04:02:56 AM »
Here is my 128B with the check character.

In your code a bug.
It in check ' Check Digit Calculation.
The good help

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: (Challenge) Create a barcode generator
« Reply #21 on: October 09, 2009, 09:24:55 AM »
Some amazing code Evgeniy. 8-)

As for the check character, I thought I followed this correctly but I'll go back a review my code.

Thanks
Code: [Select]
Code 128

The  check character  calculation method  is described  in the  article Code128
Specification.  We assume  that you  want to  find the  check digits  of string
MRV9012. Here is how it works:

Step One: Start with the first character in the message, assign weight  starting
with 1. The start character also has a weight of 1. since the actual encoding is
(START-A)(M)(R)(V)(CODE-C)(90)(12), we have total  7 characters in the  message.
Move from the left to right, and increment the weight by one. The value of  each
character can be found here.

Index     N/A       1      2      3     4        5      6
character START-A   M      R      V     CODE-C   90     12
value     103       45     50     54    99       90     12
weight    1         1      2      3     4        5      6

Step Two: Multiply the character value by weight and add the results together. (103*1+45*1+50*2+54*3+99*4+90*5+12*6)=1328

Step Three: Divide the total result by 103; get the remainder: 1328 mod 103 = 92

Step Four: Reverse look up the Code128 encoding table; since the second part  is
encoded in character C, we should look up on the character C set. The result  is
character (92).
« Last Edit: October 09, 2009, 09:28:35 AM by CAB »
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: (Challenge) Create a barcode generator
« Reply #22 on: October 09, 2009, 09:32:58 AM »
OK I think I found my mistake. Added where I should multiply & Multiplied where I should add.

More testing before I correct the code. 8-)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: (Challenge) Create a barcode generator
« Reply #23 on: October 09, 2009, 09:36:26 AM »
My code does not agree with you code.  :-o

I updated my code.
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: (Challenge) Create a barcode generator
« Reply #24 on: October 09, 2009, 10:05:28 AM »
OK, found another error, code corrected.
Should never have doubted you. :oops:


I'd like to have some of that coffee you're drinking.  :-)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: (Challenge) Create a barcode generator
« Reply #25 on: October 09, 2009, 03:13:44 PM »
I'd like to have some of that coffee you're drinking.  :-)

Main not quality, but quantity!
My size - 1.5 litres  :-)
For comparison, background - a disk dvd

VovKa

  • Water Moccasin
  • Posts: 1631
  • Ukraine
Re: (Challenge) Create a barcode generator
« Reply #26 on: October 09, 2009, 04:06:32 PM »

Main not quality, but quantity!
My size - 1.5 litres  :-)

that could be the first doping scandal among programmers :)

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: (Challenge) Create a barcode generator
« Reply #27 on: October 09, 2009, 04:16:53 PM »
Now that's a Man Size cup. :-)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: (Challenge) Create a barcode generator
« Reply #28 on: October 12, 2009, 07:02:09 AM »
Using PLINES create a barcode generator.

http://en.wikipedia.org/wiki/Code_128

Good luck!! 8-)


Hello, Mark! :)
Show your decision...