VintaSoft Barcode .NET SDK v10.0
Vintasoft.Barcode.SymbologySubsets Namespace / BarcodeSymbologySubset Class
Members Example




In This Topic
    BarcodeSymbologySubset Class
    In This Topic
    An abstract base class that defines the barcode symbology subset.
    Object Model
    BarcodeSymbologySubset ClassBarcodeSymbologySubset Class
    Syntax
    'Declaration
     
    Public MustInherit Class BarcodeSymbologySubset 
    'Usage
     
    Dim instance As BarcodeSymbologySubset
    public abstract class BarcodeSymbologySubset 
    public __gc abstract class BarcodeSymbologySubset 
    public ref class BarcodeSymbologySubset abstract 
    Remarks
    This class must be used if it is necessary to define the barcode symbology subset of some basic barcode type.
    Here are examples of possible barcode symbology subsets:
    • Code39 barcodes with checksum modulo 256
    • Code128 barcodes, which contain 10 symbols in which 6 first symbols are digits
    • Code39 barcodes, which have height more than specified value
    • DataMatrix barcodes, which have values coded in specified format

    For defining the barcode symbology subset it is necessary to override the Decode method, which defines the algorithm for converting the source barcode symbology value into the barcode symbology subset value.
    The Encode(ValueItemBase,WriterSettings) method also can be overriden but this is not obligatory, this method defines the algorithm for converting the barcode symbology subset value into the source barcode symbology value.

    Usage of the barcode symbology subsets allows to classify the searched barcode as a barcode from the barcode symbology subset during the barcode recognition and this can greatly increase the barcode recognition speed and allows to use the ExpectedBarcodes property for specifying the expected barcode count.

    VintaSoft Barcode .NET SDK has the following built-in barcode symbology subsets:
    Example
    This example shows how to define a subset of Code39 barcodes with checksum modulo 256.
    Imports System.Drawing   
       
    Imports Vintasoft.Barcode.SymbologySubsets   
    Imports Vintasoft.Barcode   
    Imports Vintasoft.Barcode.BarcodeInfo   
       
    ''' <summary>   
    ''' Test that demonstrates how to generate an image with Code39 barcode with checksum modulo 256   
    ''' and how to read Code39 barcode with checksum modulo 256 in image.   
    ''' </summary>   
    Public NotInheritable Class Code39Checksum256SymbologyTest   
    	Private Sub New()   
    	End Sub   
       
    	''' <summary>   
    	''' Runs the test.   
    	''' </summary>   
    	Public Shared Sub Test()   
    		Test("TEST12345")   
    		Test("ABCDEF")   
    		Test("AB")   
    		Test("123456789")   
    	End Sub   
       
    	''' <summary>   
    	''' Runs the test for a single barcode value.   
    	''' </summary>   
    	Public Shared Sub Test(value As String)   
    		' create an object that defines Code39 barcodes with checksum modulo 256   
    		Dim code39Checksum256 As New Code39Checksum256Symbology()   
       
    		' create the barcode writer   
    		Dim writer As New BarcodeWriter()   
       
    		' specify that writer must generate Code39 barcode AND   
    		' set the barcode value with checksum   
    		code39Checksum256.Encode(value, writer.Settings)   
       
    		' create image with Code39 barcode with checksum   
    		Using imageWithCode39BarcodeWithChecksum As Image = writer.GetBarcodeAsBitmap()   
    			' specify that writer must generate Code39 barcode   
    			writer.Settings.Barcode = BarcodeType.Code39   
       
    			' set the barcode value without checksum   
    			writer.Settings.Value = value   
       
    			' create image with barcode without checksum   
    			Using imageWithCode39Barcode As Image = writer.GetBarcodeAsBitmap()   
    				' create the barcode reader   
    				Using reader As New BarcodeReader()   
    					' specify that reader must search for Code39 with checksum only   
       
    					reader.Settings.ScanBarcodeTypes = BarcodeType.None   
    					reader.Settings.ScanBarcodeSubsets.Add(code39Checksum256)   
       
    					' read barcode from image with barcode with checksum   
    					Dim infos As IBarcodeInfo() = reader.ReadBarcodes(imageWithCode39BarcodeWithChecksum)   
       
    					' show information about found barcode   
       
    					Dim subsetInfo As BarcodeSubsetInfo = DirectCast(infos(0), BarcodeSubsetInfo)   
    					Console.WriteLine("Detected '{0}' barcode, value = {1}, base value = {2}.", subsetInfo.BarcodeSubset.Name, subsetInfo.Value, subsetInfo.BaseBarcodeInfo.Value)   
       
       
    					' read barcodes from image with barcode without checksum   
    					infos = reader.ReadBarcodes(imageWithCode39Barcode)   
    					' if barcode is found   
    					If infos.Length <> 0 Then   
    						' throw exception   
    						Throw New ApplicationException()   
    					End If   
    				End Using   
    			End Using   
    		End Using   
    	End Sub   
    End Class   
       
    ''' <summary>   
    ''' Represents the barcode symbology subset that includes Code39 barcodes with checksum modulo 256.   
    ''' </summary>   
    Public Class Code39Checksum256Symbology   
    	Inherits BarcodeSymbologySubset   
       
    	#Region "Properties"   
       
    	''' <summary>   
    	''' Gets a name of barcode symbology subset.   
    	''' </summary>   
    	Public Overrides ReadOnly Property Name() As String   
    		Get   
    			Return "Code39 CH256"   
    		End Get   
    	End Property   
       
    	''' <summary>   
    	''' Gets a base barcode symbology type of this barcode symbology subset.   
    	''' </summary>   
    	Public Overrides ReadOnly Property BaseType() As BarcodeType   
    		Get   
    			Return BarcodeType.Code39   
    		End Get   
    	End Property   
       
    	#End Region   
       
       
       
    	#Region "Methods"   
       
    	''' <summary>   
    	''' Decodes the Code39 barcode value into Code39 barcode value with checksum modulo 256.   
    	''' </summary>   
    	''' <param name="info">Code39 barcode value.</param>   
    	''' <returns>Code39 barcode value with checksum modulo 256.</returns>   
    	Public Overrides Function Decode(info As IBarcodeInfo) As BarcodeSubsetInfo   
    		If info.Value IsNot Nothing AndAlso info.Value.Length >= 4 Then   
    			' get the barcode checksum   
    			Dim checksum As String = info.Value.Substring(info.Value.Length - 2)   
    			' get the barcode value   
    			Dim value As String = info.Value.Substring(0, info.Value.Length - 2)   
    			' if barcode checksum is correct   
    			If checksum = GenerateChecksum256(value) Then   
    				Dim result As New BarcodeSubsetInfo(info, Me, New TextValueItem(value))   
    				result.Confidence = 100   
    				Return result   
    			End If   
    		End If   
    		Return Nothing   
    	End Function   
       
    	''' <summary>   
    	''' Encodes the Code39 barcode value with checksum modulo 256 into the Code39 barcode value   
    	''' using provided barcode writer settings and   
    	''' saves the source barcode symbology value in the barcode writer settings.   
    	''' </summary>   
    	''' <param name="value">Code39 barcode value with checksum modulo 256 to encode.</param>   
    	''' <param name="settings">Barcode writer settings.</param>   
    	Public Overrides Sub Encode(value As ValueItemBase, settings As WriterSettings)   
    		Dim textValue As String = value.ToString()   
    		If textValue.Length < 2 Then   
    			Throw New WriterSettingsException(WriterSettingsExceptionType.ExpectedNSymbolsInBarcodeValue, "minimum two symbols")   
    		End If   
       
    		' specify the barcode symbology type   
    		settings.Barcode = Me.BaseType   
    		' add checksum to the barcode value   
    		settings.Value = String.Format("{0}{1}", textValue, GenerateChecksum256(textValue))   
    	End Sub   
       
    	''' <summary>   
    	''' Calculates simple checksum modulo 256.   
    	''' </summary>   
    	''' <param name="value">Text value.</param>   
    	''' <returns>Checksum in HEX format.</returns>   
    	Private Shared Function GenerateChecksum256(value As String) As String   
    		Dim checksum As Integer = 0   
    		For i As Integer = 0 To value.Length - 1   
    			Dim symbol As Integer = AscW(value(i)) Mod 39   
    			If i Mod 2 = 0 Then   
    				checksum += symbol   
    			Else   
    				checksum += symbol * 3   
    			End If   
    		Next   
    		checksum = checksum Mod 256   
    		Return checksum.ToString("X2")   
    	End Function   
       
    	#End Region   
       
    End Class   
       
    ' This code example produces the following output:    
    '        
    '   Detected 'Code39 CH256' barcode, value = TEST12345, base value = TEST12345E3.   
    '   Detected 'Code39 CH256' barcode, value = ABCDEF, base value = ABCDEF59.   
    '   Detected 'Code39 CH256' barcode, value = AB, base value = AB6B.   
    '   Detected 'Code39 CH256' barcode, value = 123456789, base value = 123456789EE.   
    '
    using System;
    using System.Drawing;
                
    using Vintasoft.Barcode.SymbologySubsets;
    using Vintasoft.Barcode;
    using Vintasoft.Barcode.BarcodeInfo;
                
    /// <summary>
    /// Test that demonstrates how to generate an image with Code39 barcode with checksum modulo 256
    /// and how to read Code39 barcode with checksum modulo 256 in image.
    /// </summary>
    public static class Code39Checksum256SymbologyTest
    {
                
        /// <summary>
        /// Runs the test.
        /// </summary>
        public static void Test()
        {
            Test("TEST12345");
            Test("ABCDEF");
            Test("AB");
            Test("123456789");
        }
                
        /// <summary>
        /// Runs the test for a single barcode value.
        /// </summary>
        public static void Test(string value)
        {
            // create an object that defines Code39 barcodes with checksum modulo 256
            Code39Checksum256Symbology code39Checksum256 = new Code39Checksum256Symbology();
                
            // create the barcode writer
            BarcodeWriter writer = new BarcodeWriter();
                
            // specify that writer must generate Code39 barcode AND
            // set the barcode value with checksum
            code39Checksum256.Encode(value, writer.Settings);
                
            // create image with Code39 barcode with checksum
            using (Image imageWithCode39BarcodeWithChecksum = writer.GetBarcodeAsBitmap())
            {
                // specify that writer must generate Code39 barcode
                writer.Settings.Barcode = BarcodeType.Code39;
                
                // set the barcode value without checksum
                writer.Settings.Value = value;
                
                // create image with barcode without checksum
                using (Image imageWithCode39Barcode = writer.GetBarcodeAsBitmap())
                {
                    // create the barcode reader
                    using (BarcodeReader reader = new BarcodeReader())
                    {
                        // specify that reader must search for Code39 with checksum only
                
                        reader.Settings.ScanBarcodeTypes = BarcodeType.None;
                        reader.Settings.ScanBarcodeSubsets.Add(code39Checksum256);
                
                        // read barcode from image with barcode with checksum
                        IBarcodeInfo[] infos = reader.ReadBarcodes(imageWithCode39BarcodeWithChecksum);
                
                        // show information about found barcode
                
                        BarcodeSubsetInfo subsetInfo = ((BarcodeSubsetInfo)infos[0]);
                        Console.WriteLine("Detected '{0}' barcode, value = {1}, base value = {2}.",
                            subsetInfo.BarcodeSubset.Name,
                            subsetInfo.Value,
                            subsetInfo.BaseBarcodeInfo.Value);
                
                
                        // read barcodes from image with barcode without checksum
                        infos = reader.ReadBarcodes(imageWithCode39Barcode);
                        // if barcode is found
                        if (infos.Length != 0)
                            // throw exception
                            throw new ApplicationException();
                    }
                }
            }
        }
    }
                
    /// <summary>
    /// Represents the barcode symbology subset that includes Code39 barcodes with checksum modulo 256.
    /// </summary>
    public class Code39Checksum256Symbology : BarcodeSymbologySubset
    {
                
        #region Properties
                
        /// <summary>
        /// Gets a name of barcode symbology subset.
        /// </summary>
        public override string Name
        {
            get
            {
                return "Code39 CH256";
            }
        }
                
        /// <summary>
        /// Gets a base barcode symbology type of this barcode symbology subset.
        /// </summary>
        public override BarcodeType BaseType
        {
            get
            {
                return BarcodeType.Code39;
            }
        }
                
        #endregion
                
                
                
        #region Methods
                
        /// <summary>
        /// Decodes the Code39 barcode value into Code39 barcode value with checksum modulo 256.
        /// </summary>
        /// <param name="info">Code39 barcode value.</param>
        /// <returns>Code39 barcode value with checksum modulo 256.</returns>
        public override BarcodeSubsetInfo Decode(IBarcodeInfo info)
        {
            if (info.Value != null && info.Value.Length >= 4)
            {
                // get the barcode checksum
                string checksum = info.Value.Substring(info.Value.Length - 2);
                // get the barcode value
                string value = info.Value.Substring(0, info.Value.Length - 2);
                // if barcode checksum is correct
                if (checksum == GenerateChecksum256(value))
                {
                    BarcodeSubsetInfo result = new BarcodeSubsetInfo(info, this, new TextValueItem(value));
                    result.Confidence = 100;
                    return result;
                }
            }
            return null;
        }
                
        /// <summary>
        /// Encodes the Code39 barcode value with checksum modulo 256 into the Code39 barcode value
        /// using provided barcode writer settings and
        /// saves the source barcode symbology value in the barcode writer settings.
        /// </summary>
        /// <param name="value">Code39 barcode value with checksum modulo 256 to encode.</param>
        /// <param name="settings">Barcode writer settings.</param>
        public override void Encode(ValueItemBase value, WriterSettings settings)
        {
            string textValue = value.ToString();
            if (textValue.Length < 2)
                throw new WriterSettingsException(
                    WriterSettingsExceptionType.ExpectedNSymbolsInBarcodeValue, 
                    "minimum two symbols");
                
            // specify the barcode symbology type
            settings.Barcode = this.BaseType;
            // add checksum to the barcode value
            settings.Value = string.Format("{0}{1}", textValue, GenerateChecksum256(textValue));
        }
                
        /// <summary>
        /// Calculates simple checksum modulo 256.
        /// </summary>
        /// <param name="value">Text value.</param>
        /// <returns>Checksum in HEX format.</returns>
        private static string GenerateChecksum256(string value)
        {
            int checksum = 0;
            for (int i = 0; i < value.Length; i++)
            {
                int symbol = (int)value[i] % 39;
                if (i % 2 == 0)
                    checksum += symbol;
                else
                    checksum += symbol * 3;
            }
            checksum = checksum % 256;
            return checksum.ToString("X2");
        }
                
        #endregion
                
    }
                
    /* This code example produces the following output: 
         
       Detected 'Code39 CH256' barcode, value = TEST12345, base value = TEST12345E3.
       Detected 'Code39 CH256' barcode, value = ABCDEF, base value = ABCDEF59.
       Detected 'Code39 CH256' barcode, value = AB, base value = AB6B.
       Detected 'Code39 CH256' barcode, value = 123456789, base value = 123456789EE.
    */
    Inheritance Hierarchy

    System.Object
       Vintasoft.Barcode.SymbologySubsets.BarcodeSymbologySubset
          Vintasoft.Barcode.SymbologySubsets.BarcodeSymbologyWithChecksum
          Vintasoft.Barcode.SymbologySubsets.Code32BarcodeSymbology
          Vintasoft.Barcode.SymbologySubsets.Code39ExtendedBarcodeSymbology
          Vintasoft.Barcode.SymbologySubsets.DeutschePost2Of5BaseSymbology
          Vintasoft.Barcode.SymbologySubsets.DhlAwbBarcodeSymbology
          Vintasoft.Barcode.SymbologySubsets.EanVelocityBarcodeSymbology
          Vintasoft.Barcode.SymbologySubsets.GS1.FedExGround96BarcodeSymbology
          Vintasoft.Barcode.SymbologySubsets.GS1.GS1BarcodeSymbologySubset
          Vintasoft.Barcode.SymbologySubsets.IsbnBarcodeSymbology
          Vintasoft.Barcode.SymbologySubsets.IssnBarcodeSymbology
          Vintasoft.Barcode.SymbologySubsets.JanBarcodeSymbology
          Vintasoft.Barcode.SymbologySubsets.NumlyNumberBarcodeSymbology
          Vintasoft.Barcode.SymbologySubsets.OpcBarcodeSymbology
          Vintasoft.Barcode.SymbologySubsets.PpnBarcodeSymbology
          Vintasoft.Barcode.SymbologySubsets.PznBarcodeSymbology
          Vintasoft.Barcode.SymbologySubsets.RoyalMailMailmark.MailmarkCmdmBarcodeSymbology
          Vintasoft.Barcode.SymbologySubsets.Sscc18BarcodeSymbology
          Vintasoft.Barcode.SymbologySubsets.SwissPostParcelBarcodeSymbology
          Vintasoft.Barcode.SymbologySubsets.VicsBolBarcodeSymbology
          Vintasoft.Barcode.SymbologySubsets.VicsScacProBarcodeSymbology
          Vintasoft.Barcode.SymbologySubsets.VinSymbology
          Vintasoft.Barcode.SymbologySubsets.XFACompressed.XFACompressedBarcodeSymbologySubset

    Requirements

    Target Platforms: Windows 8, Windows 7, Windows Vista, Windows XP, Windows Server 2012, Windows Server 2008, Windows Server 2003

    See Also