Read Barcodes Asynchronously

We can read barcodes using the BarcodeReader class. The easiest method to use is the BarcodeReader.Read method. IronBarcode also has a ReadAsync method for multithreaded asynchronous programming.

Below is an example of how to use the BarcodeReader class to read a barcode from an image file. The code also demonstrates how to utilize some options from BarcodeReaderOptions to optimize the barcode reading process.

// Import the necessary namespace for IronBarcode
using IronBarCode;
using System.Threading.Tasks;
// Import the necessary namespace for IronBarcode
using IronBarCode;
using System.Threading.Tasks;
' Import the necessary namespace for IronBarcode
Imports IronBarCode
Imports System.Threading.Tasks
$vbLabelText   $csharpLabel
// Define a static class to contain our barcode reading functionality
public static class BarcodeReaderExample
{
    // Synchronously read and decode a barcode from an image file
    public static void ReadBarcodeFromFile(string filePath)
    {
        // Create a new BarcodeReader instance with default settings
        BarcodeReader reader = new BarcodeReader();

        // Customize reading options for better performance or accuracy
        BarcodeReaderOptions options = new BarcodeReaderOptions
        {
            ExpectMultipleBarcodes = false, // Set to true if expecting multiple barcodes in a single image
            EnhanceAccuracy = true,         // Enable enhanced accuracy, may slow down reading speed
            SpeedUp = false                 // Disable speed optimization for more accurate readings
        };

        // Read the barcode from the file using specified options
        BarcodeResult result = reader.Read(filePath, options);

        // Check if a barcode was found
        if (result != null && result.Success)
        {
            // Output the barcode value and format to the console
            Console.WriteLine($"Barcode found: {result.Text} with format {result.Format}");
        }
        else
        {
            Console.WriteLine("No barcode was detected in the image.");
        }
    }

    // Asynchronously read and decode a barcode from an image file
    public static async Task ReadBarcodeFromFileAsync(string filePath)
    {
        // Create a new BarcodeReader instance
        BarcodeReader reader = new BarcodeReader();

        // Customize reading options for the asynchronous read
        BarcodeReaderOptions options = new BarcodeReaderOptions
        {
            ExpectMultipleBarcodes = false,
            EnhanceAccuracy = true,
            SpeedUp = true // Enable speed optimization for faster readings in async scenarios
        };

        // Asynchronously read the barcode from the file
        BarcodeResult result = await reader.ReadAsync(filePath, options);

        // Check if a barcode was found
        if (result != null && result.Success)
        {
            // Output the barcode value and format to the console
            Console.WriteLine($"Asynchronously found barcode: {result.Text} with format {result.Format}");
        }
        else
        {
            Console.WriteLine("No barcode was detected in the image during async reading.");
        }
    }
}
// Define a static class to contain our barcode reading functionality
public static class BarcodeReaderExample
{
    // Synchronously read and decode a barcode from an image file
    public static void ReadBarcodeFromFile(string filePath)
    {
        // Create a new BarcodeReader instance with default settings
        BarcodeReader reader = new BarcodeReader();

        // Customize reading options for better performance or accuracy
        BarcodeReaderOptions options = new BarcodeReaderOptions
        {
            ExpectMultipleBarcodes = false, // Set to true if expecting multiple barcodes in a single image
            EnhanceAccuracy = true,         // Enable enhanced accuracy, may slow down reading speed
            SpeedUp = false                 // Disable speed optimization for more accurate readings
        };

        // Read the barcode from the file using specified options
        BarcodeResult result = reader.Read(filePath, options);

        // Check if a barcode was found
        if (result != null && result.Success)
        {
            // Output the barcode value and format to the console
            Console.WriteLine($"Barcode found: {result.Text} with format {result.Format}");
        }
        else
        {
            Console.WriteLine("No barcode was detected in the image.");
        }
    }

    // Asynchronously read and decode a barcode from an image file
    public static async Task ReadBarcodeFromFileAsync(string filePath)
    {
        // Create a new BarcodeReader instance
        BarcodeReader reader = new BarcodeReader();

        // Customize reading options for the asynchronous read
        BarcodeReaderOptions options = new BarcodeReaderOptions
        {
            ExpectMultipleBarcodes = false,
            EnhanceAccuracy = true,
            SpeedUp = true // Enable speed optimization for faster readings in async scenarios
        };

        // Asynchronously read the barcode from the file
        BarcodeResult result = await reader.ReadAsync(filePath, options);

        // Check if a barcode was found
        if (result != null && result.Success)
        {
            // Output the barcode value and format to the console
            Console.WriteLine($"Asynchronously found barcode: {result.Text} with format {result.Format}");
        }
        else
        {
            Console.WriteLine("No barcode was detected in the image during async reading.");
        }
    }
}
' Define a static class to contain our barcode reading functionality
Public Module BarcodeReaderExample
	' Synchronously read and decode a barcode from an image file
	Public Sub ReadBarcodeFromFile(ByVal filePath As String)
		' Create a new BarcodeReader instance with default settings
		Dim reader As New BarcodeReader()

		' Customize reading options for better performance or accuracy
		Dim options As New BarcodeReaderOptions With {
			.ExpectMultipleBarcodes = False,
			.EnhanceAccuracy = True,
			.SpeedUp = False
		}

		' Read the barcode from the file using specified options
		Dim result As BarcodeResult = reader.Read(filePath, options)

		' Check if a barcode was found
		If result IsNot Nothing AndAlso result.Success Then
			' Output the barcode value and format to the console
			Console.WriteLine($"Barcode found: {result.Text} with format {result.Format}")
		Else
			Console.WriteLine("No barcode was detected in the image.")
		End If
	End Sub

	' Asynchronously read and decode a barcode from an image file
	Public Async Function ReadBarcodeFromFileAsync(ByVal filePath As String) As Task
		' Create a new BarcodeReader instance
		Dim reader As New BarcodeReader()

		' Customize reading options for the asynchronous read
		Dim options As New BarcodeReaderOptions With {
			.ExpectMultipleBarcodes = False,
			.EnhanceAccuracy = True,
			.SpeedUp = True
		}

		' Asynchronously read the barcode from the file
		Dim result As BarcodeResult = Await reader.ReadAsync(filePath, options)

		' Check if a barcode was found
		If result IsNot Nothing AndAlso result.Success Then
			' Output the barcode value and format to the console
			Console.WriteLine($"Asynchronously found barcode: {result.Text} with format {result.Format}")
		Else
			Console.WriteLine("No barcode was detected in the image during async reading.")
		End If
	End Function
End Module
$vbLabelText   $csharpLabel

Key Points:

  • Synchronous vs Asynchronous Methods:

    • Read method is straightforward for use when immediate results are needed and is called on the main thread.
    • ReadAsync method is for asynchronous execution, allowing the program to continue with other tasks while waiting for the barcode reading to complete.
  • BarcodeReaderOptions:
    • ExpectMultipleBarcodes: If set to true, the reader will look for multiple barcodes in the image.
    • EnhanceAccuracy: Improves accuracy at the cost of speed.
    • SpeedUp: Increases reading efficiency, which might be useful for processing large numbers of images quickly. However, this might reduce accuracy if set to true.