Imports System.Drawing
Imports Vintasoft.Barcode
''' <summary>
''' Test that shows how to set the delegate for barcode verification and
''' verify found barcodes during the barcode recognition process.
''' </summary>
Class UserDefinedChecksumExample
''' <summary>
''' Runs the test.
''' </summary>
Public Shared Sub Run()
Dim value As String = "1234567890AbC"
' create the barcode writer
Dim writer As New BarcodeWriter()
' specify that writer must generate Code39 barcode
writer.Settings.Barcode = BarcodeType.Code39
' create the barcode reader
Using reader As New BarcodeReader()
' specify that reader must search for Code39 barcodes only
reader.Settings.ScanBarcodeTypes = BarcodeType.Code39
' specify that reader must search for barcode with 100% confidence only
reader.Settings.MinConfidence = 100
' specify that reader must use barcodes in automatic mode
reader.Settings.AutomaticRecognition = True
' specify that reader must search for 1 barcode
reader.Settings.ExpectedBarcodes = 1
' set the barcode value without checksum
writer.Settings.Value = value
' create image with barcode without checksum
Using barcodeNoChecksumImage As Image = writer.GetBarcodeAsBitmap()
' set the barcode value with checksum
writer.Settings.Value = value & GenerateChecksum(value)
' create image with barcode with checksum
Using barcodeWithMyChecksumImage As Image = writer.GetBarcodeAsBitmap()
' specify delegate to a method for barcode verification
reader.Settings.VerifyBarcodeMethod = AddressOf VerifyBarcodeMethod
' read barcodes from barcode image without checksum,
' barcode reader will not find barcode
Dim infos As IBarcodeInfo() = reader.ReadBarcodes(barcodeNoChecksumImage)
Console.WriteLine("Scan (NoChecksum):")
For i As Integer = 0 To infos.Length - 1
Console.WriteLine(infos(i).Value)
Next
' read barcodes from barcode image with checksum,
' barcode reader will find 1 barcode
infos = reader.ReadBarcodes(barcodeWithMyChecksumImage)
Console.WriteLine("Scan (MyChecksum):")
For i As Integer = 0 To infos.Length - 1
Console.WriteLine(infos(i).Value)
Next
End Using
End Using
End Using
End Sub
''' <summary>
''' Generates checksum modulo 1000.
''' </summary>
Private Shared Function GenerateChecksum(value As String) As String
Dim checkSum As Integer = 0
For i As Integer = 0 To value.Length - 1
checkSum += CByte(AscW(value(i))) * (i + 1)
checkSum = checkSum Mod 1000
Next
' result - [000..999]
Return checkSum.ToString().PadLeft(3, "0"C)
End Function
''' <summary>
''' Verifies the barcode during the barcode recognition process.
''' </summary>
Private Shared Sub VerifyBarcodeMethod(reader As BarcodeReader, barcodeInfo As IBarcodeInfo)
If TestChecksum(barcodeInfo.Value) Then
barcodeInfo.Confidence = 100
Else
barcodeInfo.Confidence = 0
End If
End Sub
''' <summary>
''' Tests checksum in barcode value.
''' </summary>
Private Shared Function TestChecksum(barcodeValue As String) As Boolean
Dim value As String = barcodeValue.Substring(0, barcodeValue.Length - 3)
Dim readChecksum As String = barcodeValue.Substring(barcodeValue.Length - 3)
Return readChecksum = GenerateChecksum(value)
End Function
End Class