Set Barcode Reader Options

IronBarcode offers a range of settings within the BarcodeReaderOptions class to enable tailored and optimized barcode reading. These options allow you to trade-off resources for accuracy, control the reading scope, and optimize reading strategies. Here's an example demonstrating how to utilize these options effectively:

using IronBarCode; // Import the IronBarcode library

public class BarcodeReaderExample
{
    public static void Main()
    {
        // Initialize the BarcodeReaderOptions
        BarcodeReaderOptions options = new BarcodeReaderOptions();

        // Set up the reader options

        // Optimize for accuracy by allowing more resources for barcode reading
        options.TotalBarcodes = 1; // Stop after finding the first barcode
        options.TreatAllDecodersAs = BarcodeEncoding.AutoDetect; // Use automatic detection for barcode type

        // Focus the area of interest to improve efficiency or performance
        options.CropRegion = new System.Drawing.Rectangle(0, 0, 100, 100); // Define a crop region within the image

        // Limit the number of parallel threads (improves performance on multi-core systems)
        options.MaxThreads = 4;

        // Read the barcode from an image path and apply the customized options
        var results = BarcodeReader.ReadBarcodesFromFile("example.png", options);

        // Iterate through the results and output the barcode text
        foreach (var result in results)
        {
            Console.WriteLine($"Found barcode: {result.Text}");
        }
    }
}
using IronBarCode; // Import the IronBarcode library

public class BarcodeReaderExample
{
    public static void Main()
    {
        // Initialize the BarcodeReaderOptions
        BarcodeReaderOptions options = new BarcodeReaderOptions();

        // Set up the reader options

        // Optimize for accuracy by allowing more resources for barcode reading
        options.TotalBarcodes = 1; // Stop after finding the first barcode
        options.TreatAllDecodersAs = BarcodeEncoding.AutoDetect; // Use automatic detection for barcode type

        // Focus the area of interest to improve efficiency or performance
        options.CropRegion = new System.Drawing.Rectangle(0, 0, 100, 100); // Define a crop region within the image

        // Limit the number of parallel threads (improves performance on multi-core systems)
        options.MaxThreads = 4;

        // Read the barcode from an image path and apply the customized options
        var results = BarcodeReader.ReadBarcodesFromFile("example.png", options);

        // Iterate through the results and output the barcode text
        foreach (var result in results)
        {
            Console.WriteLine($"Found barcode: {result.Text}");
        }
    }
}
Imports IronBarCode ' Import the IronBarcode library

Public Class BarcodeReaderExample
	Public Shared Sub Main()
		' Initialize the BarcodeReaderOptions
		Dim options As New BarcodeReaderOptions()

		' Set up the reader options

		' Optimize for accuracy by allowing more resources for barcode reading
		options.TotalBarcodes = 1 ' Stop after finding the first barcode
		options.TreatAllDecodersAs = BarcodeEncoding.AutoDetect ' Use automatic detection for barcode type

		' Focus the area of interest to improve efficiency or performance
		options.CropRegion = New System.Drawing.Rectangle(0, 0, 100, 100) ' Define a crop region within the image

		' Limit the number of parallel threads (improves performance on multi-core systems)
		options.MaxThreads = 4

		' Read the barcode from an image path and apply the customized options
		Dim results = BarcodeReader.ReadBarcodesFromFile("example.png", options)

		' Iterate through the results and output the barcode text
		For Each result In results
			Console.WriteLine($"Found barcode: {result.Text}")
		Next result
	End Sub
End Class
$vbLabelText   $csharpLabel

Explanation:

  • BarcodeReaderOptions: This class provides options to customize the reading of barcodes, making the process more efficient and accurate based on specific needs.

  • TotalBarcodes: By setting this to 1, the reader will stop after finding the first barcode, improving performance when only one barcode is expected.

  • TreatAllDecodersAs: Automatically detects the type of barcode, simplifying the process when you have varied barcode formats.

  • CropRegion: Allows focusing on a specific area of the image, which can significantly improve reading times and accuracy when the barcode position is approximately known.

  • MaxThreads: Specifies the maximum number of threads to be used in parallel, enhancing performance on systems with multiple CPU cores.

This configuration offers a balance between resource use, speed, and accuracy, tailored to your specific barcode reading needs.