|
'This class provides functions to decode a barcode encoded in "3of9 code" (or code39)
'Code 3 of 9 means that 3 out of 9 bars are wide - the other ones are narrow
Public Class Code39
Private BCtoParse As Barcode
Private CleanedBCString As String
Public CleanedBC As Barcode
Private myDataString As String = ""
'this our decoded value
Public ReadOnly Property DataString()
Get
DataString = myDataString
End Get
End Property
'this is the barcode (see barcode.vb) that has to be decoded
Public Property BC2Parse() As Barcode
Get
BC2Parse = BCtoParse
End Get
Set(ByVal Value As Barcode)
BCtoParse = Value
End Set
End Property
Public Sub New(ByVal newRawBarcode As Barcode)
BCtoParse = newRawBarcode
CleanedBC = New Barcode
End Sub
Public Sub New()
BCtoParse = Nothing
CleanedBC = New Barcode
End Sub
'Function to get the DataString
Public Function GetCode39() As String
If BCtoParse Is Nothing Then
GetCode39 = "No RawBC"
Exit Function
End If
CleanFor39()
ParseNow()
GetCode39 = myDataString
End Function
Public Function GetCode39(ByVal RawBarcode As Barcode) As String
BCtoParse = RawBarcode
CleanFor39()
ParseNow()
GetCode39 = myDataString
End Function
'Clean the Code a bit more than the Barcode.CleanSimple()
'A Code39 only has wide or narrow bars - no need for analog values anymore
Private Sub CleanFor39()
Dim i As Int32
CleanedBCString = "" 'this sting will contain the cleaned barcode
CleanedBC.SamplesClear()
CleanedBC.FirstColor = Barcode.BlackWhite.Black
'assume: a narrow bar is 1 units long and a wide is 3 units long (standard code39)
'--> the average bar length in a complete bar is around 1.6 units
'--> we can use the average length to determine between wide & narrow
For i = 0 To BCtoParse.Samples.GetUpperBound(0)
If BCtoParse.Samples(i) > BCtoParse.AverageTick Then
CleanedBCString = CleanedBCString & "w"
CleanedBC.SamplesAdd(3)
ElseIf BCtoParse.Samples(i) > 0 And BCtoParse.Samples(i) <= BCtoParse.AverageTick Then
CleanedBCString = CleanedBCString & "n"
CleanedBC.SamplesAdd(1)
End If
Next
End Sub
'Parse the cleaned barcode string
Private Sub ParseNow()
Dim i As Int32
Dim Pattern(8) As Char 'a code39 character is alway 9 bars long
myDataString = ""
Try
For i = 0 To CleanedBCString.Length - 1 Step 10
CleanedBCString.CopyTo(i, Pattern, 0, 9) 'now parse 9 bars
myDataString = myDataString & ParsePattern(New String(Pattern))
Next
Catch ex As Exception 'we need this catch in case that the length is no multible of 9
End Try
'repair trick
'the first and last character in code39 are always '*'
'so if we have an unknown character in this place but the next char is ok we
'suppose it's just a small read error and replace the ? with a *
'i did this to get better results when showing my program to my teacher... ;)
If myDataString.Length > 1 Then
If myDataString.StartsWith("?") And myDataString.Chars(1) <> "?" Then
myDataString = myDataString.Remove(0, 1)
myDataString = "*" & myDataString
End If
If myDataString.EndsWith("?") And myDataString.Chars(myDataString.Length - 2) <> "?" Then
myDataString = myDataString.Remove(myDataString.Length - 1, 1)
myDataString = myDataString & "*"
End If
End If
End Sub
'Parse the Pattern of the 3of9 Code
Private Function ParsePattern(ByVal Pattern As String) As String
Select Case Pattern
Case "wnnwnnnnw"
Return "1"
Case "nnwwnnnnw"
Return "2"
Case "wnwwnnnnn"
Return "3"
Case "nnnwwnnnw"
Return "4"
Case "wnnwwnnnn"
Return "5"
Case "nnwwwnnnn"
Return "6"
Case "nnnwnnwnw"
Return "7"
Case "wnnwnnwnn"
Return "8"
Case "nnwwnnwnn"
Return "9"
Case "nnnwwnwnn"
Return "0"
Case "wnnnnwnnw"
Return "A"
Case "nnwnnwnnw"
Return "B"
Case "wnwnnwnnn"
Return "C"
Case "nnnnwwnnw"
Return "D"
Case "wnnnwwnnn"
Return "E"
Case "nnwnwwnnn"
Return "F"
Case "nnnnnwwnw"
Return "G"
Case "wnnnnwwnn"
Return "H"
Case "nnwnnwwnn"
Return "I"
Case "nnnnwwwnn"
Return "J"
Case "wnnnnnnww"
Return "K"
Case "nnwnnnnww"
Return "L"
Case "wnwnnnnwn"
Return "M"
Case "nnnnwnnww"
Return "N"
Case "wnnnwnnwn"
Return "O"
Case "nnwnwnnwn"
Return "P"
Case "nnnnnnwww"
Return "Q"
Case "wnnnnnwwn"
Return "R"
Case "nnwnnnwwn"
Return "S"
Case "nnnnwnwwn"
Return "T"
Case "wwnnnnnnw"
Return "U"
Case "nwwnnnnnw"
Return "V"
Case "wwwnnnnnn"
Return "W"
Case "nwnnwnnnw"
Return "X"
Case "wwnnwnnnn"
Return "Y"
Case "nwwnwnnnn"
Return "Z"
Case "nwnnnnwnw"
Return "-"
Case "wwnnnnwnn"
Return "."
Case "nwwnnnwnn"
Return " "
Case "nwnnwnwnn"
Return "*"
Case "nwnwnwnnn"
Return "$"
Case "nwnwnnnwn"
Return "/"
Case "nwnnnwnwn"
Return "+"
Case "nnnwnwnwn"
Return "%"
Case Else
Return "?"
End Select
End Function
End Class
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.