ThresholdMode Property (ReaderSettings)
In This Topic
Gets or sets a mode of threshold detection.
Syntax
Property Value
Default value is
ThresholdMode.Automatic.
Remarks
VintasoftColor, palette or gray image (VintasoftColor 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 VintasoftColor image to black-white image with Threshold value and reads barcodes from image. - Range of threshold's values (ThresholdMode = ThresholdMode.Iterations).
Library converts the VintasoftColor 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 Vintasoft.Imaging
Imports Vintasoft.Barcode
''' <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 barcodeImage As VintasoftBitmap = ImageCodecs.[Default].Decode("test1.jpg")
ReadBarcodesUsingIterationsThresholdMode(barcodeImage)
End Using
End Sub
''' <summary>
''' Reads barcodes from image using several iterations with different thresholds.
''' </summary>
Private Shared Sub ReadBarcodesUsingIterationsThresholdMode(barcodeImage As VintasoftBitmap)
' 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.Horizontal Or ScanDirection.Vertical
' 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.AutomaticRecognition = False
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 Vintasoft.Imaging;
using Vintasoft.Barcode;
/// <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 (VintasoftBitmap barcodeImage = ImageCodecs.Default.Decode("test1.jpg"))
{
ReadBarcodesUsingIterationsThresholdMode(barcodeImage);
}
}
/// <summary>
/// Reads barcodes from image using several iterations with different thresholds.
/// </summary>
static void ReadBarcodesUsingIterationsThresholdMode(VintasoftBitmap 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.Horizontal | ScanDirection.Vertical;
// 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.AutomaticRecognition = false;
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: .NET9; .NET 8; .NET 7; .NET 6; .NET Framework 4.8, 4.7, 4.6, 4.5, 4.0, 3.5
See Also