as long as i know, your program is wrong making UPC-A barcodes; it is not so simple.
Code: Select all
; *** EAN13 and UPC-A BARCODE ***
; by Psychophanta - 2006
;NOTE: Encoding a UPC-A symbol is identical to encoding a EAN-13,
; a "0" is simply inserted in front of the UPC-A code itself (i.e., if the barcode is 075678164125,
; a zero is inserted before the code, making the EAN-13 symbol 0075678164125).
; *************************** EAN13 and UPC-A BARCODE USAGE ****************************
;*NumberSystem.s: The number system consists of two digits (sometimes three digits)
; which identify the country (or economic region) numbering authority which assigned the manufacturer code.
; Any number system which starts with the digit 0 is a UPC-A barcode.
;*ManufacturerCode.s: The manufacturer code is a unique code assigned to each manufacturer by the numbering
; authority indicated by the number system code. All products produced by a given company will use the same
; manufacturer code.
;*ProductCode.s: The product code is a unique code assigned by the manufacturer. Unlike the manufacturer code,
; which must be assigned by the UCC, the manufacturer is free to assign product codes to each of their
; products without consulting any other organization. Since the UCC will already have guaranteed that the
; manufacturer code is unique, the manufacturer need only make sure that they do not repeat their own product codes.
;*CheckDigit.s: The check digit is an additional digit used to verify that a barcode has been scanned correctly.
; Since a scan can produce incorrect data due to inconsistent scanning speed, print imperfections, or a host
; of other problems, it is useful to verify that the rest of the data in the barcode has been correctly interpreted.
; The check digit is calculated based on the rest of the digits of the barcode. Normally, if the check digit is
; the same as the value of the check digit based on the data that has been scanned, there is a high level of
; confidence that the barcode was scanned correctly.
;*size.l: Width in pixels of one bar
;*target.s: Location and file name (without extension)
Procedure.b EAN13barcode(NumberSystem.s,ManufacturerCode.s,ProductCode.s,CheckDigit.s,size.l,target.s)
; *** Check Input ***
If Len(NumberSystem)<2 Or Len(NumberSystem)>3 Or Len(ManufacturerCode)>5 Or Len(ManufacturerCode)<4 Or Len(ProductCode)<>5 Or Len(CheckDigit)<>1 Or size<1 Or target=""
ProcedureReturn 0
EndIf
If (Len(NumberSystem)=2 And Len(ManufacturerCode)<>5) Or (Len(NumberSystem)=3 And Len(ManufacturerCode)<>4)
Debug "Bad Number"
ProcedureReturn 0
EndIf
;Test Check Digit:
out.s=NumberSystem+ManufacturerCode+ProductCode
For x.b=1 To 12:t.b=~x&1
CheckDigitShouldbe.l+Val(Mid(out,x,1))*Pow(3,t)
Next
CheckDigitShouldbe.l=10-CheckDigitShouldbe.l%10
If CheckDigitShouldbe<>Val(CheckDigit)
Debug "Bad Checksum"
ProcedureReturn 0
EndIf
NumberSystem=Left(out,2):ManufacturerCode=Mid(out,3,5)
; *** Barcode Digits ***
; Rigth Digits:
Dim digitsr.s(9)
digitsr(0)="1110010"
digitsr(1)="1100110"
digitsr(2)="1101100"
digitsr(3)="1000010"
digitsr(4)="1011100"
digitsr(5)="1001110"
digitsr(6)="1010000"
digitsr(7)="1000100"
digitsr(8)="1001000"
digitsr(9)="1110100"
; Left Digits Odd:
Dim digitslodd.s(9)
digitslodd(0)="0001101"
digitslodd(1)="0011001"
digitslodd(2)="0010011"
digitslodd(3)="0111101"
digitslodd(4)="0100011"
digitslodd(5)="0110001"
digitslodd(6)="0101111"
digitslodd(7)="0111011"
digitslodd(8)="0110111"
digitslodd(9)="0001011"
; Left Digits Even:
Dim digitsleven.s(9)
digitsleven(0)="0100111"
digitsleven(1)="0110011"
digitsleven(2)="0011011"
digitsleven(3)="0100001"
digitsleven(4)="0011101"
digitsleven(5)="0111001"
digitsleven(6)="0000101"
digitsleven(7)="0010001"
digitsleven(8)="0001001"
digitsleven(9)="0010111"
; *** CONVERSION ***
out.s="101" ; <- Left sentinel
out.s+digitslodd(Val(Right(NumberSystem,1)))
Select Left(NumberSystem,1)
Case "0"
out.s+digitslodd(Val(Mid(ManufacturerCode,1,1)))
out.s+digitslodd(Val(Mid(ManufacturerCode,2,1)))
out.s+digitslodd(Val(Mid(ManufacturerCode,3,1)))
out.s+digitslodd(Val(Mid(ManufacturerCode,4,1)))
out.s+digitslodd(Val(Mid(ManufacturerCode,5,1)))
Case "1"
out.s+digitslodd(Val(Mid(ManufacturerCode,1,1)))
out.s+digitsleven(Val(Mid(ManufacturerCode,2,1)))
out.s+digitslodd(Val(Mid(ManufacturerCode,3,1)))
out.s+digitsleven(Val(Mid(ManufacturerCode,4,1)))
out.s+digitsleven(Val(Mid(ManufacturerCode,5,1)))
Case "2"
out.s+digitslodd(Val(Mid(ManufacturerCode,1,1)))
out.s+digitsleven(Val(Mid(ManufacturerCode,2,1)))
out.s+digitsleven(Val(Mid(ManufacturerCode,3,1)))
out.s+digitslodd(Val(Mid(ManufacturerCode,4,1)))
out.s+digitsleven(Val(Mid(ManufacturerCode,5,1)))
Case "3"
out.s+digitslodd(Val(Mid(ManufacturerCode,1,1)))
out.s+digitsleven(Val(Mid(ManufacturerCode,2,1)))
out.s+digitsleven(Val(Mid(ManufacturerCode,3,1)))
out.s+digitsleven(Val(Mid(ManufacturerCode,4,1)))
out.s+digitslodd(Val(Mid(ManufacturerCode,5,1)))
Case "4"
out.s+digitsleven(Val(Mid(ManufacturerCode,1,1)))
out.s+digitslodd(Val(Mid(ManufacturerCode,2,1)))
out.s+digitslodd(Val(Mid(ManufacturerCode,3,1)))
out.s+digitsleven(Val(Mid(ManufacturerCode,4,1)))
out.s+digitsleven(Val(Mid(ManufacturerCode,5,1)))
Case "5"
out.s+digitsleven(Val(Mid(ManufacturerCode,1,1)))
out.s+digitsleven(Val(Mid(ManufacturerCode,2,1)))
out.s+digitslodd(Val(Mid(ManufacturerCode,3,1)))
out.s+digitslodd(Val(Mid(ManufacturerCode,4,1)))
out.s+digitsleven(Val(Mid(ManufacturerCode,5,1)))
Case "6"
out.s+digitsleven(Val(Mid(ManufacturerCode,1,1)))
out.s+digitsleven(Val(Mid(ManufacturerCode,2,1)))
out.s+digitsleven(Val(Mid(ManufacturerCode,3,1)))
out.s+digitslodd(Val(Mid(ManufacturerCode,4,1)))
out.s+digitslodd(Val(Mid(ManufacturerCode,5,1)))
Case "7"
out.s+digitsleven(Val(Mid(ManufacturerCode,1,1)))
out.s+digitslodd(Val(Mid(ManufacturerCode,2,1)))
out.s+digitsleven(Val(Mid(ManufacturerCode,3,1)))
out.s+digitslodd(Val(Mid(ManufacturerCode,4,1)))
out.s+digitsleven(Val(Mid(ManufacturerCode,5,1)))
Case "8"
out.s+digitsleven(Val(Mid(ManufacturerCode,1,1)))
out.s+digitslodd(Val(Mid(ManufacturerCode,2,1)))
out.s+digitsleven(Val(Mid(ManufacturerCode,3,1)))
out.s+digitsleven(Val(Mid(ManufacturerCode,4,1)))
out.s+digitslodd(Val(Mid(ManufacturerCode,5,1)))
Case "9"
out.s+digitsleven(Val(Mid(ManufacturerCode,1,1)))
out.s+digitsleven(Val(Mid(ManufacturerCode,2,1)))
out.s+digitslodd(Val(Mid(ManufacturerCode,3,1)))
out.s+digitsleven(Val(Mid(ManufacturerCode,4,1)))
out.s+digitslodd(Val(Mid(ManufacturerCode,5,1)))
EndSelect
out.s+"01010" ; Middle bars
For x.b=1 To 5
out.s+digitsr(Val(Mid(ProductCode,x,1)))
Next
out.s+digitsr(Val(CheckDigit)) ; <- checkdigit
out.s+"101" ; Right sentinel
; *** DRAWING ***
width=(Len(out)+size*2)*size:height=width/2
CreateImage(0,width,height)
StartDrawing(ImageOutput(0))
Box(0,0,width,height,$ffffff)
For x.b=1 To Len(out)
If Mid(out,x,1)="0"
color=$ffffff
ElseIf Mid(out,x,1)="1"
color=$000000
EndIf
If x<4 Or x>Len(out)-4 Or (x>Len(out)/2-2 And x<Len(out)/2+3)
Box(size*(x+size-1),size*2,size,height-4*size,color)
Else
Box(size*(x+size-1),size*2,size,height-8*size,color)
EndIf
Next
StopDrawing()
SaveImage(0,target+".bmp")
FreeImage(0)
ProcedureReturn 1
EndProcedure
;Example:
EAN13barcode("54","49000","00099","6",2,"c:\EAN13_BARCODE")