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;
// 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.");
}
}
}
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 totrue
, 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 totrue
.