VintaSoft Barcode .NET SDK v11.0
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 Shared Sub TestCode39Barcode(value As String)
            ' create the barcode writer
            Dim writer As New BarcodeWriter()
    
            ' set barcode writer settings
            writer.Settings.Barcode = BarcodeType.Code39
            writer.Settings.Value = value
    
            ' get image of barcode without checksum
            Using barcodeWithoutChecksumImage As Image = writer.GetBarcodeAsBitmap()
                    ' set barcode writer settings
                    writer.Settings.Value = value & GenerateChecksum(value)
    
                    ' get image of barcode with custom checksum
                    Using barcodeWithMyChecksumImage As Image = writer.GetBarcodeAsBitmap()
                            ' create the barcode reader
                            Dim reader As New BarcodeReader()
    
                            ' set barcode reader settings
                            reader.Settings.ScanBarcodeTypes = 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 IBarcodeInfo() = reader.ReadBarcodes(barcodeWithoutChecksumImage)
                            Console.WriteLine("Scan (NoChecksum):")
                            For i As Integer = 0 To infos.Length - 1
                                    Console.WriteLine(infos(i).Value)
                            Next
    
                            ' read barcodes from image of barcode with checksum - one barcode will be found
                            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 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 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
            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
                  
    
    // The project, which uses this code, must have references to the following assemblies:
    // - Vintasoft.Barcode
    
    public static void TestCode39Barcode(string value)
    {
        // create the barcode writer
        Vintasoft.Barcode.BarcodeWriter writer = new Vintasoft.Barcode.BarcodeWriter();
    
        // set barcode writer settings
        writer.Settings.Barcode = Vintasoft.Barcode.BarcodeType.Code39;
        writer.Settings.Value = value;
    
        // get image of barcode without checksum
        using (System.Drawing.Image barcodeWithoutChecksumImage = writer.GetBarcodeAsBitmap())
        {
            // set barcode writer settings
            writer.Settings.Value = value + GenerateChecksum(value);
    
            // get image of barcode with custom checksum
            using (System.Drawing.Image barcodeWithMyChecksumImage = writer.GetBarcodeAsBitmap())
            {
                // 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);
    }