LEADTOOLS Barcode (Leadtools.Barcode assembly)
LEAD Technologies, Inc

ErrorMode Property

Example 







Indicate how to handle errors when reading barcodes. .NET support WinRT support Silverlight support
Syntax
public BarcodeReaderErrorMode ErrorMode {get; set;}
'Declaration
 
Public Property ErrorMode As BarcodeReaderErrorMode
'Usage
 
Dim instance As BarcodeReader
Dim value As BarcodeReaderErrorMode
 
instance.ErrorMode = value
 
value = instance.ErrorMode
public BarcodeReaderErrorMode ErrorMode {get; set;}
ObjectiveC Syntax
Java Syntax
 get_ErrorMode();
set_ErrorMode(value);
public:
property BarcodeReaderErrorMode ErrorMode {
   BarcodeReaderErrorMode get();
   void set (    BarcodeReaderErrorMode value);
}

Property Value

An BarcodeReaderErrorMode enumeration member that indicates how to handle errors when reading barcodes. Default value is BarcodeReaderErrorMode.Default.
Remarks

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 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 Visual Basic) or an empty array of BarcodeData type.

Example
Copy CodeCopy Code  
Private myErrors As List(Of Exception)  ' List of errors
   Public Sub BarcodeReader_ErrorModeExample()
      Dim imageFileName As String = Path.Combine(LEAD_VARS.ImagesDir, "Barcode1.tif")

      ' Create a Barcode engine
      Dim engine As New BarcodeEngine()

      ' Get the Barcode reader instance
      Dim reader As BarcodeReader = engine.Reader

      ' Load the image
      Using codecs As New RasterCodecs()
         Using image As RasterImage = codecs.Load(imageFileName, 0, CodecsLoadByteOrder.BgrOrGray, 1, 1)
            myErrors = New List(Of Exception)()

            ' Disable the errors
            reader.ErrorMode = BarcodeReaderErrorMode.IgnoreAll

            ' Subscribe to the ReadSymbology event
            AddHandler reader.ReadSymbology, AddressOf myReader_ReadSymbology
            ' Read all barcodes in the image
            reader.ReadBarcodes(image, LogicalRectangle.Empty, 0, Nothing)
            RemoveHandler reader.ReadSymbology, AddressOf myReader_ReadSymbology

            ' Show the errors
            If myErrors.Count > 0 Then
               Console.WriteLine("Errors encountered:")
               For Each err As Exception In myErrors
                  Console.WriteLine(err.Message)
               Next
            End If
         End Using
      End Using
   End Sub

   Private Sub myReader_ReadSymbology(ByVal sender As Object, ByVal e As BarcodeReadSymbologyEventArgs)
      ' If we encounter an error, save it to our list
      If Not IsNothing(e.Error) Then
         myErrors.Add(e.Error)
      End If
   End Sub

Public NotInheritable Class LEAD_VARS
   Public Const ImagesDir As String = "C:\Users\Public\Documents\LEADTOOLS Images"
End Class
private List<Exception> myErrors;   // List of errors
   public void BarcodeReader_ErrorModeExample()
   {
      string imageFileName = Path.Combine(LEAD_VARS.ImagesDir, "Barcode1.tif");

      // Create a Barcode engine
      BarcodeEngine engine = new BarcodeEngine();

      // Get the Barcode reader instance
      BarcodeReader reader = engine.Reader;

      // Load the image
      using(RasterCodecs codecs = new RasterCodecs())
      {
         using(RasterImage image = codecs.Load(imageFileName, 0, CodecsLoadByteOrder.BgrOrGray, 1, 1))
         {
            myErrors = new List<Exception>();

            // Disable the errors
            reader.ErrorMode = BarcodeReaderErrorMode.IgnoreAll;

            // Subscribe to the ReadSymbology event
            reader.ReadSymbology += new EventHandler<BarcodeReadSymbologyEventArgs>(myReader_ReadSymbology);
            // Read all barcodes in the image
            reader.ReadBarcodes(image, LogicalRectangle.Empty, 0, null);
            reader.ReadSymbology -= new EventHandler<BarcodeReadSymbologyEventArgs>(myReader_ReadSymbology);

            // Show the errors
            if(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 list
      if(e.Error != null)
      {
         myErrors.Add(e.Error);
      }
   }

static class LEAD_VARS
{
   public const string ImagesDir = @"C:\Users\Public\Documents\LEADTOOLS Images";
}
private List<BarcodeException> myErrors;   // List of errors
[TestMethod]
public async Task BarcodeReader_ErrorModeExample()
{
   string imageFileName = @"Assets\Barcode1.tif";
   // Create a Barcode engine
   BarcodeEngine engine = new BarcodeEngine();

   // Get the Barcode reader instance
   BarcodeReader reader = engine.Reader;

   // Load the image
   using(RasterCodecs codecs = new RasterCodecs())
   {
      StorageFile loadFile = await Tools.AppInstallFolder.GetFileAsync(imageFileName);
      using(RasterImage image = await codecs.LoadAsync(LeadStreamFactory.Create(loadFile)))
      {
         myErrors = new List<BarcodeException>();

         // Disable the errors
         reader.ErrorMode = BarcodeReaderErrorMode.IgnoreAll;

         // Subscribe to the ReadSymbology event
         reader.ReadSymbology += new EventHandler<BarcodeReadSymbologyEventArgs>(myReader_ReadSymbology);
         // Read all barcodes in the image
         reader.ReadBarcodes(image, LeadRectHelper.Empty, 0, null);
         reader.ReadSymbology -= new EventHandler<BarcodeReadSymbologyEventArgs>(myReader_ReadSymbology);

         // Show the errors
         if(myErrors.Count > 0)
         {
            Debug.WriteLine("Errors encountered:");
            foreach(BarcodeException error in myErrors)
            {
               Debug.WriteLine(error.Message);
            }
         }
      }
   }
}

private void myReader_ReadSymbology(object sender, BarcodeReadSymbologyEventArgs e)
{
   // If we encounter an error, save it to our list
   if(e.Error != null)
   {
      myErrors.Add(e.Error);
   }
}
private List<Exception> myErrors;   // List of errors
public void BarcodeReader_ErrorModeExample(RasterImage image)
{
   // Create a Barcode engine
   BarcodeEngine engine = new BarcodeEngine();
   // Get the Barcode reader instance
   BarcodeReader reader = engine.Reader;

   // Load the image
   RasterCodecs codecs = new RasterCodecs();

   myErrors = new List<Exception>();

   // Disable the errors
   reader.ErrorMode = BarcodeReaderErrorMode.IgnoreAll;

   // Subscribe to the ReadSymbology event
   reader.ReadSymbology += new EventHandler<BarcodeReadSymbologyEventArgs>(myReader_ReadSymbology);
   // Read all barcodes in the image
   reader.ReadBarcodes(image, LogicalRectangle.Empty, 0, null);
   reader.ReadSymbology -= new EventHandler<BarcodeReadSymbologyEventArgs>(myReader_ReadSymbology);

   // Show the errors
   if(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 list
   if(e.Error != null)
   {
      myErrors.Add(e.Error);
   }
}
Private myErrors As List(Of Exception) ' List of errors
Public Sub BarcodeReader_ErrorModeExample(ByVal image As RasterImage)
   ' Create a Barcode engine
   Dim engine As BarcodeEngine = New BarcodeEngine()
   ' Get the Barcode reader instance
   Dim reader As BarcodeReader = engine.Reader

   ' Load the image
   Dim codecs As RasterCodecs = New RasterCodecs()

   myErrors = New List(Of Exception)()

   ' Disable the errors
   reader.ErrorMode = BarcodeReaderErrorMode.IgnoreAll

   ' Subscribe to the ReadSymbology event
   AddHandler reader.ReadSymbology, AddressOf myReader_ReadSymbology
   ' Read all barcodes in the image
   reader.ReadBarcodes(image, LogicalRectangle.Empty, 0, Nothing)
   RemoveHandler reader.ReadSymbology, AddressOf myReader_ReadSymbology

   ' Show the errors
   If myErrors.Count > 0 Then
      Console.WriteLine("Errors encountered:")
      For Each [error] As Exception In myErrors
         Console.WriteLine([error].Message)
      Next
   End If
End Sub

Private Sub myReader_ReadSymbology(ByVal sender As Object, ByVal e As BarcodeReadSymbologyEventArgs)
   ' If we encounter an error, save it to our list
   If Not e.Error Is Nothing Then
      myErrors.Add(e.Error)
   End If
End Sub
Requirements

Target Platforms: Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

See Also

Reference

BarcodeReader Class
BarcodeReader Members

 

 


Products | Support | Contact Us | Copyright Notices

© 2006-2012 All Rights Reserved. LEAD Technologies, Inc.

Leadtools.Barcode requires a Barcode Module license and unlock key. For more information, refer to: LEADTOOLS Toolkit Features