Indicate how to handle errors when reading barcodes.
public BarcodeReaderErrorMode ErrorMode { get; set; } Public Property ErrorMode As BarcodeReaderErrorMode @property (nonatomic, assign) LTBarcodeReaderErrorMode errorMode public BarcodeReaderErrorMode getErrorMode()public void setErrorMode(BarcodeReaderErrorMode value)
public:property BarcodeReaderErrorMode ErrorMode {BarcodeReaderErrorMode get();void set ( BarcodeReaderErrorMode );}
An BarcodeReaderErrorMode enumeration member that indicates how to handle errors when reading barcodes. The default value is BarcodeReaderErrorMode.Default.
The BarcodeReader object might encounter errors when reading barcodes. By default, when an error is encountered, an exception is thrown, usually of type BarcodeException with the BarcodeException.Code set to one of the BarcodeExceptionCode enumeration members for more details regarding the error. This is the default behavior.
Sometimes, this behavior is not the desired one, for example:
The application is trying to decode barcodes on an "if found and correct" basis. If there were corrupted barcodes, the action is to ignore them and to continue. In this case, you can set the error mode to BarcodeReaderErrorMode.IgnoreAll.
The application is reading multiple barcodes from an image, and the action is to ignore the corrupted barcodes and return only the valid ones found. In this case, you can set the error mode to BarcodeReaderErrorMode.IgnoreAll and subscribe to the BarcodeReader.ReadSymbology event to get a notification when an error occurs and save the exception for later use. You can then handle these errors when all barcodes are read and the read method returns.
The LEADTOOLS C# and VB Barcode Demo changes the value of ErrorMode to BarcodeReaderErrorMode.IgnoreAll and uses the BarcodeReader.ReadSymbology event to show the errors encountered in a list box.
Note that when the BarcodeReader does not find any barcodes in the image, no exception is thrown. Instead, BarcodeReader.ReadBarcode and BarcodeReader.ReadBarcodes methods will return null (Nothing in VB) or an empty array of BarcodeData type.
This example changes the value of BarcodeReader.ErrorMode before reading all the barcodes from an image. It will then show any errors encountered after the read operation returns.
using Leadtools;using Leadtools.Codecs;using Leadtools.Barcode;using Leadtools.ImageProcessing;private List<Exception> myErrors; // List of errorspublic void BarcodeReader_ErrorModeExample(){string imageFileName = Path.Combine(LEAD_VARS.ImagesDir, "Barcode1.tif");// Create a Barcode engineBarcodeEngine engine = new BarcodeEngine();// Get the Barcode reader instanceBarcodeReader reader = engine.Reader;// Load the imageusing (RasterCodecs codecs = new RasterCodecs()){using (RasterImage image = codecs.Load(imageFileName, 0, CodecsLoadByteOrder.BgrOrGray, 1, 1)){myErrors = new List<Exception>();// Disable the errorsreader.ErrorMode = BarcodeReaderErrorMode.IgnoreAll;// Subscribe to the ReadSymbology eventreader.ReadSymbology += new EventHandler<BarcodeReadSymbologyEventArgs>(myReader_ReadSymbology);// Read all barcodes in the imagereader.ReadBarcodes(image, LeadRect.Empty, 0, null);reader.ReadSymbology -= new EventHandler<BarcodeReadSymbologyEventArgs>(myReader_ReadSymbology);// Show the errorsif (myErrors.Count > 0){Console.WriteLine("Errors encountered:");foreach (Exception error in myErrors){Console.WriteLine(error.Message);}}}}}private void myReader_ReadSymbology(object sender, BarcodeReadSymbologyEventArgs e){// If we encounter an error, save it to our listif (e.Error != null){myErrors.Add(e.Error);}}static class LEAD_VARS{public const string ImagesDir = @"C:\Users\Public\Documents\LEADTOOLS Images";}
Imports LeadtoolsImports Leadtools.CodecsImports Leadtools.BarcodeImports Leadtools.ImageProcessingPrivate myErrors As List(Of Exception) ' List of errorsPublic Sub BarcodeReader_ErrorModeExample()Dim imageFileName As String = Path.Combine(LEAD_VARS.ImagesDir, "Barcode1.tif")' Create a Barcode engineDim engine As New BarcodeEngine()' Get the Barcode reader instanceDim reader As BarcodeReader = engine.Reader' Load the imageUsing codecs As New RasterCodecs()Using image As RasterImage = codecs.Load(imageFileName, 0, CodecsLoadByteOrder.BgrOrGray, 1, 1)myErrors = New List(Of Exception)()' Disable the errorsreader.ErrorMode = BarcodeReaderErrorMode.IgnoreAll' Subscribe to the ReadSymbology eventAddHandler reader.ReadSymbology, AddressOf myReader_ReadSymbology' Read all barcodes in the imagereader.ReadBarcodes(image, LeadRect.Empty, 0, Nothing)RemoveHandler reader.ReadSymbology, AddressOf myReader_ReadSymbology' Show the errorsIf myErrors.Count > 0 ThenConsole.WriteLine("Errors encountered:")For Each err As Exception In myErrorsConsole.WriteLine(err.Message)NextEnd IfEnd UsingEnd UsingEnd SubPrivate Sub myReader_ReadSymbology(ByVal sender As Object, ByVal e As BarcodeReadSymbologyEventArgs)' If we encounter an error, save it to our listIf Not IsNothing(e.Error) ThenmyErrors.Add(e.Error)End IfEnd SubPublic NotInheritable Class LEAD_VARSPublic Const ImagesDir As String = "C:\Users\Public\Documents\LEADTOOLS Images"End Class
c#[Silverlight C# Example]using Leadtools;using Leadtools.Codecs;using Leadtools.Forms;using Leadtools.Barcode;using Leadtools.ImageProcessing;using Leadtools.Examples;private List<Exception> myErrors; // List of errorspublic void BarcodeReader_ErrorModeExample(RasterImage image){// Create a Barcode engineBarcodeEngine engine = new BarcodeEngine();// Get the Barcode reader instanceBarcodeReader reader = engine.Reader;// Load the imageRasterCodecs codecs = new RasterCodecs();myErrors = new List<Exception>();// Disable the errorsreader.ErrorMode = BarcodeReaderErrorMode.IgnoreAll;// Subscribe to the ReadSymbology eventreader.ReadSymbology += new EventHandler<BarcodeReadSymbologyEventArgs>(myReader_ReadSymbology);// Read all barcodes in the imagereader.ReadBarcodes(image, LogicalRectangle.Empty, 0, null);reader.ReadSymbology -= new EventHandler<BarcodeReadSymbologyEventArgs>(myReader_ReadSymbology);// Show the errorsif (myErrors.Count > 0){Console.WriteLine("Errors encountered:");foreach (Exception error in myErrors){Console.WriteLine(error.Message);}}}private void myReader_ReadSymbology(object sender, BarcodeReadSymbologyEventArgs e){// If we encounter an error, save it to our listif (e.Error != null){myErrors.Add(e.Error);}}vb[Silverlight VB Example]Imports LeadtoolsImports Leadtools.CodecsImports Leadtools.FormsImports Leadtools.BarcodeImports Leadtools.ImageProcessingImports Leadtools.ImageProcessing.ColorPrivate myErrors As List(Of Exception) ' List of errorsPublic Sub BarcodeReader_ErrorModeExample(ByVal image As RasterImage)' Create a Barcode engineDim engine As BarcodeEngine = New BarcodeEngine()' Get the Barcode reader instanceDim reader As BarcodeReader = engine.Reader' Load the imageDim codecs As RasterCodecs = New RasterCodecs()myErrors = New List(Of Exception)()' Disable the errorsreader.ErrorMode = BarcodeReaderErrorMode.IgnoreAll' Subscribe to the ReadSymbology eventAddHandler reader.ReadSymbology, AddressOf myReader_ReadSymbology' Read all barcodes in the imagereader.ReadBarcodes(image, LogicalRectangle.Empty, 0, Nothing)RemoveHandler reader.ReadSymbology, AddressOf myReader_ReadSymbology' Show the errorsIf myErrors.Count > 0 ThenConsole.WriteLine("Errors encountered:")For Each [error] As Exception In myErrorsConsole.WriteLine([error].Message)NextEnd IfEnd SubPrivate Sub myReader_ReadSymbology(ByVal sender As Object, ByVal e As BarcodeReadSymbologyEventArgs)' If we encounter an error, save it to our listIf Not e.Error Is Nothing ThenmyErrors.Add(e.Error)End IfEnd Sub
Help Collections
Raster .NET | C API | C++ Class Library | HTML5 JavaScript
Document .NET | C API | C++ Class Library | HTML5 JavaScript
Medical .NET | C API | C++ Class Library | HTML5 JavaScript
Medical Web Viewer .NET
Multimedia
Direct Show .NET | C API | Filters
Media Foundation .NET | C API | Transforms
Supported Platforms
.NET, Java, Android, and iOS/macOS Assemblies
Imaging, Medical, and Document
C API/C++ Class Libraries
Imaging, Medical, and Document
HTML5 JavaScript Libraries
Imaging, Medical, and Document
