VintaSoft Barcode .NET SDK 14.3: Documentation for .NET developer
In This Topic
    How to use custom checksum in barcode?
    In This Topic
    SDK allows to specify method that validates and marks found barcode as good or bad barcode
    (ReaderSettings.VerifyBarcodeMethod property).

    Example: Here is an example that shows how to add custom checksum (at base 1000) to Code 39 barcode and verify checksum while barcode reading.
    public static void TestCode39Barcode(string value)
    {
        // create the barcode writer
        using (Vintasoft.Barcode.BarcodeWriter writer = new Vintasoft.Barcode.BarcodeWriter())
        {
            // set barcode writer settings
            writer.Settings.Barcode = Vintasoft.Barcode.BarcodeType.Code39;
            writer.Settings.Value = value;
            writer.Settings.ValueVisible = false;
    
            // get image of barcode without checksum
            using (Vintasoft.Imaging.VintasoftBitmap barcodeWithoutChecksumImage = writer.GetBarcodeAsVintasoftBitmap())
            {
                // set barcode writer settings
                writer.Settings.Value = value + GenerateChecksum(value);
    
                // get image of barcode with custom checksum
                using (Vintasoft.Imaging.VintasoftBitmap barcodeWithMyChecksumImage = writer.GetBarcodeAsVintasoftBitmap())
                {
                    // create the barcode reader
                    Vintasoft.Barcode.BarcodeReader reader = new Vintasoft.Barcode.BarcodeReader();
    
                    // set barcode reader settings
                    reader.Settings.ScanBarcodeTypes = Vintasoft.Barcode.BarcodeType.Code39;
                    reader.Settings.MinConfidence = 100;
                    reader.Settings.AutomaticRecognition = true;
                    reader.Settings.ExpectedBarcodes = 1;
    
                    // set delegate to a method that verifies the barcode value
                    reader.Settings.VerifyBarcodeMethod = VerifyBarcodeMethod;
    
                    // read barcodes from image of barcode without checksum - barcodes will not be found
                    Vintasoft.Barcode.IBarcodeInfo[] infos = reader.ReadBarcodes(barcodeWithoutChecksumImage);
                    System.Console.WriteLine("Scan (NoChecksum):");
                    for (int i = 0; i < infos.Length; i++)
                        System.Console.WriteLine(infos[i].Value);
    
                    // read barcodes from image of barcode with checksum - one barcode will be found
                    infos = reader.ReadBarcodes(barcodeWithMyChecksumImage);
                    System.Console.WriteLine("Scan (MyChecksum):");
                    for (int i = 0; i < infos.Length; i++)
                        System.Console.WriteLine(infos[i].Value);
                }
            }
        }
    }
    
    /// <summary>
    /// Generates the checksum at base 1000.
    /// </summary>
    static string GenerateChecksum(string value)
    {
        int checkSum = 0;
        for (int i = 0; i < value.Length; i++)
        {
            checkSum += ((byte)value[i]) * i;
            checkSum %= 1000;
        }
        // result - [000..999]
        return checkSum.ToString().PadLeft(3, '0');
    }
    
    /// <summary>
    /// Verifies the barcode value.
    /// </summary>
    static void VerifyBarcodeMethod(Vintasoft.Barcode.BarcodeReader reader, Vintasoft.Barcode.IBarcodeInfo barcodeInfo)
    {
        if (TestChecksum(barcodeInfo.Value))
            barcodeInfo.Confidence = 100;
        else
            barcodeInfo.Confidence = 0;
    }
    
    /// <summary>
    /// Tests checksum in barcode value.
    /// </summary>
    static bool TestChecksum(string barcodeValue)
    {
        if (barcodeValue.Length < 4)
            return false;
        string value = barcodeValue.Substring(0, barcodeValue.Length - 3);
        string readChecksum = barcodeValue.Substring(barcodeValue.Length - 3);
        return readChecksum == GenerateChecksum(value);
    }
    
    Public Shared Sub TestCode39Barcode(value As String)
        ' create the barcode writer
        Using writer As New Vintasoft.Barcode.BarcodeWriter()
            ' set barcode writer settings
            writer.Settings.Barcode = Vintasoft.Barcode.BarcodeType.Code39
            writer.Settings.Value = value
            writer.Settings.ValueVisible = False
    
            ' get image of barcode without checksum
            Using barcodeWithoutChecksumImage As Vintasoft.Imaging.VintasoftBitmap = writer.GetBarcodeAsVintasoftBitmap()
                ' set barcode writer settings
                writer.Settings.Value = value & GenerateChecksum(value)
    
                ' get image of barcode with custom checksum
                Using barcodeWithMyChecksumImage As Vintasoft.Imaging.VintasoftBitmap = writer.GetBarcodeAsVintasoftBitmap()
                    ' create the barcode reader
                    Dim reader As New Vintasoft.Barcode.BarcodeReader()
    
                    ' set barcode reader settings
                    reader.Settings.ScanBarcodeTypes = Vintasoft.Barcode.BarcodeType.Code39
                    reader.Settings.MinConfidence = 100
                    reader.Settings.AutomaticRecognition = True
                    reader.Settings.ExpectedBarcodes = 1
    
                    ' set delegate to a method that verifies the barcode value
                    reader.Settings.VerifyBarcodeMethod = AddressOf VerifyBarcodeMethod
    
                    ' read barcodes from image of barcode without checksum - barcodes will not be found
                    Dim infos As Vintasoft.Barcode.IBarcodeInfo() = reader.ReadBarcodes(barcodeWithoutChecksumImage)
                    System.Console.WriteLine("Scan (NoChecksum):")
                    For i As Integer = 0 To infos.Length - 1
                        System.Console.WriteLine(infos(i).Value)
                    Next
    
                    ' read barcodes from image of barcode with checksum - one barcode will be found
                    infos = reader.ReadBarcodes(barcodeWithMyChecksumImage)
                    System.Console.WriteLine("Scan (MyChecksum):")
                    For i As Integer = 0 To infos.Length - 1
                        System.Console.WriteLine(infos(i).Value)
                    Next
                End Using
            End Using
        End Using
    End Sub
    
    ''' <summary>
    ''' Generates the checksum at base 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
            checkSum = checkSum Mod 1000
        Next
        ' result - [000..999]
        Return checkSum.ToString().PadLeft(3, "0"C)
    End Function
    
    ''' <summary>
    ''' Verifies the barcode value.
    ''' </summary>
    Private Shared Sub VerifyBarcodeMethod(reader As Vintasoft.Barcode.BarcodeReader, barcodeInfo As Vintasoft.Barcode.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
        If barcodeValue.Length < 4 Then
            Return False
        End If
        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