Since 1970 every product we buy used to have a UPC or EAN barcode. This codes are still used to identify products and scanned at Point Of Sale(POS) counter.
Now every phone comes with a high resolution camera both at front and rear which can be used to scan any type of 1D or 2D barcodes using scanner apps.
To understand the structure of UPC or EAN barcode, refer to the page. This page give all the necessary structure and encoding along with check sum digit calculation.
This can be easily translated into a simple computer program using GW-BASIC or QBASIC
10 DIM p$(29), c%(12), q$(9), m%(100)
20 INPUT " Enter the Number : "; u$
30 FOR i% = 0 TO 9
40 READ n$, s$
50 n% = ASC(n$)
60 s% = ASC(s$) XOR &H3F
70 WHILE n% <> 0
80 p$(i%) = RIGHT$(STR$(n% MOD 2), 1) + p$(i%)
90 p$(i% + 10) = RIGHT$(STR$(n% MOD 2 XOR 1), 1) + p$(i% + 10)
100 p$(i% + 20) = p$(i% + 20) + LEFT$(p$(i%), 1)
110 q$(i%) = RIGHT$(STR$(s% MOD 2 + 1), 1) + q$(i%)
120 n% = n% \ 2
130 s% = s% \ 2
140 WEND
150 NEXT i%
160 FOR i% = 0 TO 11
170 c%(i%) = VAL(MID$(u$, i% + 1, 1))
180 X% = X% + c%(i%) * (((i% + 1) MOD 2) + (3 * (i% MOD 2)))
190 NEXT i%
200 c%(i%) = (10 - X% MOD 10) MOD 10
210 FOR i% = 1 TO 6
220 l$ = l$ + p$(VAL(MID$(q$(c%(0)), i% + 1, 1)) * 10 + c%(i%))
230 r$ = r$ + p$(c%(i% + 6))
240 NEXT i%
250 f$ = "606" + l$ + "06060" + r$ + "606"
260 KEY OFF: SCREEN 2
270 PRINT LEFT$(u$, 12) + RIGHT$(STR$(c%(12)), 1)
280 GET (0, 0)-(13 * 8, 8), m%
290 SCREEN 11: CLS
300 REM PAINT (0, 0)
310 PUT (15, 35), m%
320 FOR i% = 1 TO 95
330 b% = VAL(MID$(f$, i%, 1))
340 LINE (20 + i%, 5)-(20 + i%, 25 + b%), SGN(b%) XOR 0
350 NEXT i%
360 WHILE INKEY$ = ""
370 WEND
380 KEY ON: SCREEN 0
390 END
400 DATA r,?,f,4,l,2,B,1,\,",",N,&,P,#,D,*,H,),t,%If we run this program using QBASIC inside DOSBox or QB64 under Windows10/11 we will be prompted to enter the FIRST 12 digits. The 13th digit is the Check Sum and will be automatically calculated by the above program. We can just press Alt+Enter for full page display. We can easily scan and confirm the output using a barcode scanner or using a smartphone camera using a reader app.
The sample image for the EAN code 123456789012 will appear as below. The last digit 8 is the check sum and the program automatically caluculates from the first 12 digits.
The above output can be read by any barcode scanner or smartphone apps.
CODABAR:Refer to the page for understanding the structure and encoding of CODABAR. The following program is written in QBASIC which can run with QB64 or QBasic.
Dim P$(20), A$(20), G%(999)
For I = 1 To 20
Read X$
A$(I) = Left$(X$, 1)
X% = Val("&H" + Right$(X$, Len(X$) - 1))
For N% = 1 To 7
P$(I) = Right$(Str$(X% Mod 2), 1) + P$(I)
X% = X% \ 2
Next N%
Next I
Input " Enter The Number : "; U$
U$ = "A" + U$ + "B"
For I = 1 To Len(U$)
For J = 1 To 20
If A$(J) = Mid$(U$, I, 1) Then F$ = F$ + P$(J) + "0"
Next J
Next I
Screen 2
Print U$
Get (0, 0)-(8 * Len(U$), 14), G%()
Put (0, 0), G%(), Xor
Paint (0, 0)
For X = 1 To Len(F$)
S = S + 2 * Val(Mid$(F$, X, 1))
Next X
Z = 320 - .5 * (Len(F$) + S)
For I = 1 To Len(F$)
For J = 0 To 2 * Val(Mid$(F$, I, 1))
Line (Z, 5)-(Z, 25), I Mod 2 Xor 1
Z = Z + 1
Next J
Next I
Put ((320 - Len(U$) * 4), 30), G%()
Sleep
End ' codabar printer program.
Data 03,16,29,360,412,542,621,724,830,948
Data -c,$18,":45",/51,.54,+1f,A1a,Bb,C29,DeThe codabar generated for the numbers 1357902468 looks like below.
We can use any 1D barcode reader or smartphone scanner to see the results. Additional "A" and "B" are check digits at the starting and the end.
CODE39:Refer to the page for understanding the structure and encoding of CODE39. The following program written in GW-BASIC and runs on QB64 or QBasic
10 Rem TITLE : BARCODE PRINTER PROGRAM - CODE 3 OF 9
20 Rem TESTED WITH WINDOWS 10 HOME AND WINDOWS 11 OS
30 Rem TESTED WITH QB64 and VERIFIED THE OUTPUT WITH CODE39 SCANNER
40 Dim C$(44), P$(44), G%(1000)
50 For I = 0 To 43
60 Read C$(I), X$
70 C% = Val("&H" + X$)
80 For N% = 9 To 0 Step -1
90 P$(I) = Right$(Str$(C% Mod 2), 1) + P$(I)
100 C% = C% \ 2
110 Next N%
120 Next I
130 Cls
140 Def Seg = &H40
150 K% = Peek(&H17)
160 Poke &H17, (K% Or &H40)
170 Locate 12, 20: Print "Valid Characters are 0-9,A-Z,.,-,+,/,$,% and SPACE"
180 Locate 13, 20: Print "To Add Check Digit Enter the Data Starting with -$"
190 Locate 14, 20: Input "Enter the String: "; U$
200 If Len(U$) >= 30 Then Print "Enter String with < 30 Characters": End
210 For I = 1 To Len(U$)
220 For J = 0 To 43
230 If C$(J) = Mid$(U$, I, 1) Then F$ = F$ + P$(J): S = S + J
240 Next J
250 Next I
260 Y = (S - 75) Mod 43
270 If InStr(U$, "-$") = 1 Then U$ = U$ + C$(Y): F$ = F$ + P$(Y)
280 Screen 2
290 KEY Off: Cls
300 Print C$(43) + U$ + C$(43)
310 Get (0, 0)-(8 * (Len(U$) + 2), 14), G%()
320 Cls
330 Paint (1, 1)
340 For I = 1 To Len(F$) + 20
350 For J = Val(Mid$(P$(43) + F$ + P$(43), I, 1)) * 2 To 0 Step -1
360 Line (Z, 5)-(Z, 10 + Len(U$)), I Mod 2 Xor 0
370 Z = Z + 1
380 Next J
390 Next I
400 Put ((8 * (Len(U$) + 2)) / 2, 15 + Len(U$)), G%()
410 Poke &H17, K%
420 Def Seg
430 While InKey$ = ""
440 Wend
450 KEY On: Screen 0
460 End
470 Data 0,34,1,121,2,61,3,160,4,31,5,130,6,70,7,25,8,124,9,64
480 Data A,109,B,49,C,148,D,19,E,118,F,58,G,D,H,10C,I,4C,J,1C,K,103,L,43
490 Data M,142,N,13,O,112,P,52,Q,7,R,106,S,46,T,16,U,181,V,C1,W,1C0,X,91
500 Data Y,190,Z,D0,-,85,.,185," ",C4,$,A8,/,A2,+,8A,%,2A,*,94Code 39 can have alphabet, number or symbols. The barcode generated for HACKSTER 2026 looks like below
The output barcode can be scanned with any 1D scanner or smartphone apps.
CODE25 or ITF:Refer to the page for understanding the structure and encoding of CODE25. The following program is written in QBASIC and can be run using QB64 or QBasic
DIM P$(0 TO 9), G%(&H3FF)
FOR I = 1 TO 12
I = I + (I MOD 2 + I \ 2 MOD 2 + I \ 4 MOD 2 + I \ 8 MOD 2) \ 3
N = I
P = P XOR P
Y = (1 + Y) MOD 10
DO
P$(Y) = P$(Y) + RIGHT$(STR$(N MOD 2), 1)
P = P XOR N MOD 2
N = N \ 2
LOOP UNTIL LEN(P$(Y)) = 4
P$(Y) = P$(Y) + RIGHT$(STR$(P), 1)
NEXT I
INPUT " Enter the No: "; U$
U$ = RIGHT$(CHR$(&H30) + U$, LEN(U$) + LEN(U$) MOD 2)
FOR K = 1 TO LEN(U$) STEP 2
OP$ = OP$ + P$(VAL(MID$(U$, K, 1)))
EP$ = EP$ + P$(VAL(MID$(U$, K + 1, 1)))
NEXT K
FOR L = 1 TO LEN(OP$)
FP$ = FP$ + MID$(OP$, L, 1) + MID$(EP$, L, 1)
NEXT L
SCREEN 2
PRINT U$
GET (0, 0)-(8 * LEN(U$), 14), G%
PUT (0, 0), G%
PAINT (0, 0)
Z = 320 - INT(((LEN(U$) * 9) + 9) / 2)
FOR I = 1 TO 7 + LEN(FP$)
FOR J = 0 TO VAL(MID$("0000" + FP$ + "100", I, 1)) * 2
LINE (Z, 5)-(Z, 25), NOT I MOD 2 EQV 1
Z = Z + 1
NEXT J
NEXT I
PUT (320 - (LEN(U$) * 4), 30), G%
SLEEP
ENDThe output for the input text of 1234567890 will look like below
The above output can be scanned by any 1D barcode scanner or smartphone app.
SOFTWARE REPO:The individual file can be found at the Github page for single file download.
The precompiled EXE versions for both DOSBox and QB64 can be downloaded from HERE
We can also refer to the test run and scan results at the repo.
How to RUN:The easiest way to run the source code is download QB64 and open the file and press F5 or RUN. QB64 will compile the code and prompt to enter the user input and after entering the input and pressing ENTER, the result will appear on the screen. For better experience press Alt+ENTER for full screen before entering the user input.
We can also run the same program using DOSBox for WINDOWS. We also need to have Microsoft QBASIC V4.5 for compiling and running the program.
So it is better to use QB64 which is easier and faster way to start.
PS: These programs were developed in the year 2000 under DOS 6.22 and WINDOWS 95/98 as a hobby.


Comments