VintaSoft Barcode .NET SDK v10.0
Vintasoft.WpfBarcode Namespace / ReaderSettings Class / ThresholdMode Property
Example




In This Topic
    ThresholdMode Property (ReaderSettings)
    In This Topic
    Gets or sets a mode of threshold detection.
    Syntax
    'Declaration
     
    <DefaultValueAttribute()>
    <DescriptionAttribute("Mode of threshold detection.")>
    Public Property ThresholdMode As ThresholdMode
    'Usage
     
    Dim instance As ReaderSettings
    Dim value As ThresholdMode
     
    instance.ThresholdMode = value
     
    value = instance.ThresholdMode
    [DefaultValue()]
    [Description("Mode of threshold detection.")]
    public ThresholdMode ThresholdMode {get; set;}
    [DefaultValue()]
    [Description("Mode of threshold detection.")]
    public: __property ThresholdMode get_ThresholdMode();
    public: __property void set_ThresholdMode( 
       ThresholdMode value
    );
    [DefaultValue()]
    [Description("Mode of threshold detection.")]
    public:
    property ThresholdMode ThresholdMode {
       ThresholdMode get();
       void set (    ThresholdMode value);
    }

    Property Value

    Default value is ThresholdMode.Automatic.
    Remarks
    Color, palette or gray image (Color image) is converted to black-white image before barcode reading.

    Here is a list of steps of image converting algorithm:
    • Calculates the sum of components of color (T) for each pixel by following formula: T = R + G + B
    • Pixel color is changed to black color if T is less than Threshold value
    • Pixel color is changed to white color if T is more or equal than Threshold value

    Threshold can be:
    • Calculated automatically (ThresholdMode = ThresholdMode.Automatic). Used by default.
    • Set manually (ThresholdMode = ThresholdMode.Manual).
      Library converts the Color image to black-white image with Threshold value and reads barcodes from image.
    • Range of threshold's values (ThresholdMode = ThresholdMode.Iterations).
      Library converts the Color image to N black-white images and reads barcodes from images, where N = ThresholdIterations, ThresholdMin - left bound of range, ThresholdMax - right bound of range. Barcode reading process will be interrupted if ExpectedBarcodes value is reached.

    You should use the ThresholdMode.Automatic mode only if you read barcodes from many "different" images because this feature decreases barcode reading speed.
    You should use the ThresholdMode.Manual mode if you read barcodes from "similar" images.
    Example
    Here is a simple example that demonstrates how to detect barcodes in a color image with difficult-to-locate threshold.
    Imports System.IO   
    Imports System.Windows.Media.Imaging   
       
    Imports Vintasoft.WpfBarcode   
       
       
    ''' <summary>   
    ''' Test that shows how to read barcodes from image   
    ''' using several iterations with different thresholds.   
    ''' </summary>   
    Class ThresholdModeExample   
       
    	''' <summary>   
    	''' Runs the test.   
    	''' </summary>   
    	Public Shared Sub Test()   
    		Using imageFileStream As Stream = New FileStream("test1.jpg", FileMode.Open, FileAccess.Read)   
    			Dim barcodeImage As New BitmapImage()   
    			barcodeImage.BeginInit()   
    			barcodeImage.StreamSource = imageFileStream   
    			barcodeImage.EndInit()   
       
    			ReadBarcodesUsingIterationsThresholdMode(barcodeImage)   
    		End Using   
    	End Sub   
       
    	''' <summary>   
    	''' Reads barcodes from image using several iterations with different thresholds.   
    	''' </summary>   
    	Private Shared Sub ReadBarcodesUsingIterationsThresholdMode(barcodeImage As BitmapSource)   
    		' create barcode reader   
    		Using reader As New BarcodeReader()   
    			' specify that reader must search for Code39, Code128 and DataMatrix barcodes only   
    			reader.Settings.ScanBarcodeTypes = BarcodeType.Code39 Or BarcodeType.Code128 Or BarcodeType.DataMatrix   
       
    			' specify that reader must search for horizontal and vertical barcodes only   
    			reader.Settings.ScanDirection = ScanDirection.LeftToRight Or ScanDirection.RightToLeft   
       
    			' specify that reader must search for 3 barcodes   
    			reader.Settings.ExpectedBarcodes = 3   
       
    			' specify that reader must use 9 iterations for barcode reading,   
    			' minimum threshold is 200, maximum threashold 600, threshold step is 50   
    			reader.Settings.ThresholdMode = ThresholdMode.Iterations   
    			reader.Settings.ThresholdIterations = 9   
    			reader.Settings.ThresholdMin = 200   
    			reader.Settings.ThresholdMax = 600   
       
    			' read barcodes from image   
    			Dim infos As IBarcodeInfo() = reader.ReadBarcodes(barcodeImage)   
       
    			' show barcode recognition results   
       
    			Console.WriteLine(String.Format("Recognition time {0} ms.", reader.RecognizeTime.TotalMilliseconds))   
       
    			If infos.Length = 0 Then   
    				Console.WriteLine("No barcodes found.")   
    			Else   
    				Console.WriteLine(String.Format("{0} barcodes found:", infos.Length))   
    				Console.WriteLine()   
    				For i As Integer = 0 To infos.Length - 1   
    					Dim info As IBarcodeInfo = infos(i)   
    					Console.WriteLine(String.Format("[{0}:{1}]", i, info.BarcodeType))   
    					Console.WriteLine(String.Format("Value:      {0}", info.Value))   
    					Console.WriteLine(String.Format("Confidence: {0}%", Math.Round(info.Confidence)))   
    					Console.WriteLine(String.Format("Threshold:  {0}", info.Threshold))   
    					Console.WriteLine(String.Format("Region:     {0}", info.Region))   
    					Console.WriteLine()   
    				Next   
    			End If   
    		End Using   
    	End Sub   
    End Class
    using System;
    using System.IO;
    using System.Windows.Media.Imaging;
                
    using Vintasoft.WpfBarcode;
                
                
    /// <summary>
    /// Test that shows how to read barcodes from image
    /// using several iterations with different thresholds.
    /// </summary>
    class ThresholdModeExample
    {
                
        /// <summary>
        /// Runs the test.
        /// </summary>
        public static void Test()
        {
            using (Stream imageFileStream = new FileStream("test1.jpg", FileMode.Open, FileAccess.Read))
            {
                BitmapImage barcodeImage = new BitmapImage();
                barcodeImage.BeginInit();
                barcodeImage.StreamSource = imageFileStream;
                barcodeImage.EndInit();
                
                ReadBarcodesUsingIterationsThresholdMode(barcodeImage);
            }
        }
                
        /// <summary>
        /// Reads barcodes from image using several iterations with different thresholds.
        /// </summary>
        static void ReadBarcodesUsingIterationsThresholdMode(BitmapSource barcodeImage)
        {
            // create barcode reader
            using (BarcodeReader reader = new BarcodeReader())
            {
                // specify that reader must search for Code39, Code128 and DataMatrix barcodes only
                reader.Settings.ScanBarcodeTypes =
                    BarcodeType.Code39 |
                    BarcodeType.Code128 |
                    BarcodeType.DataMatrix;
                
                // specify that reader must search for horizontal and vertical barcodes only
                reader.Settings.ScanDirection = ScanDirection.LeftToRight | ScanDirection.RightToLeft;
                
                // specify that reader must search for 3 barcodes
                reader.Settings.ExpectedBarcodes = 3;
                
                // specify that reader must use 9 iterations for barcode reading,
                // minimum threshold is 200, maximum threashold 600, threshold step is 50
                reader.Settings.ThresholdMode = ThresholdMode.Iterations;
                reader.Settings.ThresholdIterations = 9;
                reader.Settings.ThresholdMin = 200;
                reader.Settings.ThresholdMax = 600;
                
                // read barcodes from image
                IBarcodeInfo[] infos = reader.ReadBarcodes(barcodeImage);
                
                // show barcode recognition results
                
                Console.WriteLine(string.Format("Recognition time {0} ms.",
                    reader.RecognizeTime.TotalMilliseconds));
                
                if (infos.Length == 0)
                {
                    Console.WriteLine("No barcodes found.");
                }
                else
                {
                    Console.WriteLine(string.Format("{0} barcodes found:", infos.Length));
                    Console.WriteLine();
                    for (int i = 0; i < infos.Length; i++)
                    {
                        IBarcodeInfo info = infos[i];
                        Console.WriteLine(string.Format("[{0}:{1}]", i, info.BarcodeType));
                        Console.WriteLine(string.Format("Value:      {0}", info.Value));
                        Console.WriteLine(string.Format("Confidence: {0}%", Math.Round(info.Confidence)));
                        Console.WriteLine(string.Format("Threshold:  {0}", info.Threshold));
                        Console.WriteLine(string.Format("Region:     {0}", info.Region));
                        Console.WriteLine();
                    }
                }
            }
        }
    }
    Requirements

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

    See Also