Post by Rod on Apr 26, 2018 10:04:14 GMT -5
This is a corrected program for creating Code128 barcodes. I had the checkdigit routine wrong in earlier code. Also the printing needed to boxfilled rather than lines so that correctly sized printing gets thick black lines. I have a barcode wand so I know it works now.
'code128 barcode drawing
'http://www.barcodeisland.com/code128.phtml
nomainwin
'these are constants for symbols used to define which table is used,
'change of table mid code and the quiet starting zone and stop symbol.
quietZone$ = "00000000000"
startAsymbol$ = "11010000100" '103
startBsymbol$ = "11010010000" '104
startCsymbol$ = "11010011100" '105
codeCsymbol$ = "10111011110" '99
codeBsymbol$ = "10111101110" '100
codeAsymbol$ = "11101011110" '101
stopsymbol$ = "1100011101011"
'initial checksum values
startAvalue =103
startBvalue =104
startCvalue =105
'there are three tables a has uppercase alpha and control characters
'b has upper and lower case alpha, c is just for packing numeric
'data two characters to one byte
dim barcodeimage$(100)'barcode pattern
dim barcodevalues(100,2)'barcode value 'a,b,c table asc value
'load character set A,B and C
for i= 0 to 95
read a,b,c,b$
barcodeimage$(i)=b$
barcodevalues(i,0)=a
barcodevalues(i,1)=b
barcodevalues(i,2)=c
next
'open a small window
graphicbox #bc.gb1, 20, 20, 300, 30
graphicbox #bc.gb2, 20, 100, 300, 30
textbox #bc.tb1, 20, 60, 300, 25
textbox #bc.tb2, 20, 150, 300, 25
button #bc.b1, "Print",[print],UL 340, 20, 40,20
WindowWidth = 400
WindowHeight = 300
open "Code128 Bar Code" for window as #bc
print #bc, "trapclose [quit]"
'first lets build a simple, uncompressed barcode
'We are using mixed upper and lower case so we choose
'table B to encode the message.
'the checksum starts as the initial startsymbol value for startBvalue
'to that we add the value of every subsequent symbol, times
'its position in the message. Then mod 103 the total
message$="Wikipedia"
'start checksum calculation
checksum=startBvalue
barcode$=""
for n = 1 to len(message$)
'find the character in table B
for v=0 to 95
if barcodevalues(v,1)=asc(mid$(message$,n,1)) then
checksum = checksum + n * v
barcode$ = barcode$ + barcodeimage$(v)
exit for
end if
next
next
'calculate modulus checkdigit value
checksum = checksum mod 103
'create total barcode image
barcode$=quietZone$+startBsymbol$+barcode$+barcodeimage$(checksum)+stopsymbol$
'draw barcode
'use box filled so that size changes on printer get thick black lines
#bc.gb1 "down ; fill white ; backcolor black ; color black"
x=0
for n = 1 to len(barcode$)
if mid$(barcode$,n,1)="1" then
#bc.gb1 "place ";x;" 0 ; boxfilled ";x+1;" 40"
end if
x=x+1
next
#bc.gb1 "flush"
#bc.tb1 message$
'build a compressed image that packs numeric data
'this solution packs the numeric digits two digits to one byte, so we
'will choose table C
barcode$=""
message$="123456789123"
checksum=startCvalue
pos=1
'now add the numbers in pairs
for n = 1 to 11 step 2
checksum = checksum + pos* val(mid$(message$,n,2))
barcode$ = barcode$ + barcodeimage$(val(mid$(message$,n,2)))
pos=pos+1
next
'calculate modulus checkdigit value
checksum = checksum mod 103
'create total barcode image
barcode$=quietZone$+startCsymbol$+barcode$+barcodeimage$(checksum)+stopsymbol$
'draw barcode
#bc.gb2 "down ; fill white ; backcolor black ; color black"
x=0
for n = 1 to len(barcode$)
if mid$(barcode$,n,1)="1" then
#bc.gb2 "place ";x;" 0 ; boxfilled ";x+1;" 40"
end if
x=x+1
next
#bc.gb2 "flush"
#bc.tb2 message$
wait
[print]
#bc.gb1 "print 400"
#bc.gb2 "print 400"
wait
[quit]
close #bc
end
'barcode value and image table
' a b c barcode
data 32,32,0,"11011001100"'space
data 33,33,1,"11001101100"'!
data 34,34,2,"11001100110"'"
data 35,35,3,"10010011000"'#
data 36,36,4,"10010011000"'$
data 37,37,5,"10001001100"'%
data 38,38,6,"10011001000"'&
data 39,39,7,"10011000100"''
data 40,40,8,"10001100100"'(
data 41,41,9,"11001001000"')
data 42,42,10,"11001000100"'*
data 43,43,11,"11000100100"'+
data 44,44,12,"10110011100"',
data 45,45,13,"10011011100"'-
data 46,46,14,"10011001110"'.
data 47,47,15,"10111001100"'/
data 48,48,16,"10011101100"'0
data 49,49,17,"10011100110"'1
data 50,50,18,"11001110010"'2
data 51,51,19,"11001011100"'3
data 52,52,20,"11001001110"'4
data 53,53,21,"11011100100"'5
data 54,54,22,"11001110100"'6
data 55,55,23,"11101101110"'7
data 56,56,24,"11101001100"'8
data 57,57,25,"11100101100"'9
data 58,58,26,"11100100110"':
data 59,59,27,"11101100100"';
data 60,60,28,"11100110100"'<
data 61,61,29,"11100110010"'=
data 62,62,30,"11011011000"'>
data 63,63,31,"11011000110"'?
data 64,64,32,"11000110110"'@
data 65,65,33,"10100011000"'A
data 66,66,34,"10001011000"'B
data 67,67,35,"10001000110"'C
data 68,68,36,"10110001000"'D
data 69,69,37,"10001101000"'E
data 70,70,38,"10001100010"'F
data 71,71,39,"11010001000"'G
data 72,72,40,"11000101000"'H
data 73,73,41,"11000100010"'I
data 74,74,42,"10110111000"'J
data 75,75,43,"10110001110"'K
data 76,76,44,"10001101110"'L
data 77,77,45,"10111011000"'M
data 78,78,46,"10111000110"'N
data 79,79,47,"10001110110"'O
data 80,80,48,"11101110110"'P
data 81,81,49,"11010001110"'Q
data 82,82,50,"11000101110"'R
data 83,83,51,"11011101000"'S
data 84,84,52,"11011100010"'T
data 85,85,53,"11011101110"'U
data 86,86,54,"11101011000"'V
data 87,87,55,"11101000110"'W
data 88,88,56,"11100010110"'X
data 89,89,57,"11101101000"'Y
data 90,90,58,"11101100010"'Z
data 91,91,59,"11100011010"'[
data 92,92,60,"11101111010"'\
data 93,93,61,"11001000010"']
data 94,94,62,"11110001010"'^
data 95,95,63,"10100110000"'_
data 0,96,64,"10100001100"'NULL ` table a then table b
data 1,97,65,"10010110000"'SOH a
data 2,98,66,"10010000110"'STX b
data 3,99,67,"10000101100"'ETX c
data 4,100,68,"10000100110"'EOT d
data 5,101,69,"10110010000"'ENQ e
data 6,102,70,"10110000100"'ACK f
data 7,103,71,"10011010000"'BELL g
data 8,104,72,"10011000010"'BS h
data 9,105,73,"10000110100"'HT i
data 10,106,74,"10000110010"'LF j
data 11,107,75,"11000010010"'VT k
data 12,108,76,"11001010000"'FF l
data 13,109,77,"11110111010"'CR m
data 14,110,78,"11000010100"'SO n
data 15,111,79,"10001111010"'SI o
data 16,112,80,"10100111100"'DLE p
data 17,113,81,"10010111100"'DC1 q
data 18,114,82,"10010011110"'DC2 r
data 19,115,83,"10111100100"'DC3 s
data 20,116,84,"10011110100"'DC4 t
data 21,117,85,"10011110010"'NAK u
data 22,118,86,"11110100100"'SYN v
data 23,119,87,"11110010100"'ETB w
data 24,120,88,"11110010010"'CAN x
data 25,121,89,"11011011110"'EM y
data 26,122,90,"11011110110"'SUB z
data 27,123,91,"11110110110"'ESC {
data 28,124,92,"10101111000"'FS |
data 29,125,93,"10100011110"'GS }
data 30,126,94,"10001011110"'RS ~
data 31,127,95,"10111101000"'US DEL
'the remaining codes as constants in the program
'not all are implemented
'96=fnc3
'97=fnc2
'98=shift
'99=code c
'100=code b
'101=code a
'102=fnc1
'103=start a
'104=start b
'105=start c