Basic Barcode Recognition Parameters
Overview
Aspose.BarCode for .NET provides class BarCodeReader that enables barcode recognition from given images for more than 60 symbologies. In general, to perform barcode reading, first, it is necessary to specify the source of an image with barcodes to be detected as a file, a bitmap object, or a stream. Then, it is required to set target symbologies in the DecodeType parameter; otherwise, barcode recognition will take more time, as using the default setting DecodeType.AllSupportedTypes implies looking over all supported symbologies to check for their presence in the source barcode image. In addition, developers can explicitly set the target region/regions in an image for barcode reading. It can be done using a .NET Rectangle object and allows avoiding the unnecessary search for barcodes in the image areas with no barcodes presented by default.
Basic Recognition Parameters
To read barcodes presented in an image using Aspose.BarCode for .NET, the following steps need to be taken:
- Specify the source to read barcodes (from an image file, a stream, or a bitmap), e.g. set the path to a source image
- Select target symbologies for barcode recognition. By default, the DecodeType property is set to DecodeType.AllSupportedTypes; in this way, the barcode reading procedure will cover all supported barcode types. Such an approach leads to extra time costs required to complete barcode recognition.
In Aspose.BarCode for .NET, barcode recognition is performed through the ReadBarCodes method that returns recognition results in an array of the BarCodeResult type.
The following code snippet explains how to specify several target barcode types to be recognized in a single image, in this case, PDF417, QR Code, Code 39, and Code 128, as can be seen in the sample image provided below. As such, barcodes that do not correspond to the target symbologies (DataMatrix and RM4SCC barcodes in the given example) will be ignored.
using (BarCodeReader reader = new BarCodeReader($"{path}multiple_codes.png", DecodeType.Pdf417, DecodeType.DataMatrix, DecodeType.QR,
DecodeType.Code39Extended, DecodeType.Code128, DecodeType.RM4SCC))
{
Console.WriteLine("ReadSimpleExample:");
foreach (BarCodeResult result in reader.ReadBarCodes())
Console.WriteLine($"{result.CodeTypeName}:{result.CodeText}");
}
Get Recognition Results
To get barcode recognition results, it is necessary to call the ReadBarCodes method that will output an array of the BarCodeResult type. At the same time, it is possible to get access to the current barcode reading output by using the FoundBarCodes property that stores recognition results or the FoundCount parameter that returns the number of barcodes that have been processed.
The following code sample illustrates how to load recognition results for the image provided above.
using (BarCodeReader reader = new BarCodeReader($"{path}multiple_codes.png", DecodeType.Pdf417, DecodeType.DataMatrix, DecodeType.QR,
DecodeType.Code39Extended, DecodeType.Code128, DecodeType.RM4SCC))
{
//Read barcodes
reader.ReadBarCodes();
Console.WriteLine("ReadFoundBarCodes:");
Console.WriteLine($"BarCodes count:{reader.FoundCount.ToString()}");
foreach (BarCodeResult result in reader.FoundBarCodes)
Console.WriteLine($"{result.CodeTypeName}:{result.CodeText}");
}
Manual and Timeout Methods of Recognition Abortion
Class BarCodeReader allows aborting the recognition process in case it is not further feasible. It can be done in two ways. The first option is to use the TimeOut property to interrupt the recognition process in case of exceeding the timeout value. By default, TimeOut is set to 0.
The second way is to call the Abort method in case the recognition process has been launched in the other stream. This method does not block the entire process and returns control immediately.
Both recognition abortion ways result in throwing an exception called RecognitionAbortedException in the case when the recognition process has not been completed.
The following code snippet demonstrates an example of aborting the recognition process with the exception “Recognition is aborted. Execution time: 342 ms”.
using (BarCodeReader reader = new BarCodeReader($"{path}multiple_codes.png", DecodeType.Pdf417, DecodeType.DataMatrix, DecodeType.QR,
DecodeType.Code39Extended, DecodeType.Code128, DecodeType.RM4SCC))
{
Console.WriteLine("ReadWithTimeout:");
reader.Timeout = 50;
try
{
reader.ReadBarCodes();
}
catch(RecognitionAbortedException e)
{
Console.WriteLine(e.Message);
}
}