VintaSoft Barcode .NET SDK v10.0
ThresholdMode Property (ReaderSettings)
Example 




Vintasoft.WpfBarcode Namespace > ReaderSettings Class : ThresholdMode Property
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:

Threshold can be:

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

Reference

ReaderSettings Class
ReaderSettings Members