Leadtools.Barcode名前空間 > BarcodeReaderクラス :ErrorModeプロパティ |
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;}
@property (nonatomic, assign) LTBarcodeReaderErrorMode errorMode
public BarcodeReaderErrorMode getErrorMode() public void setErrorMode(BarcodeReaderErrorMode value)
<br/>get_ErrorMode();<br/>set_ErrorMode(value);<br/>Object.defineProperty('ErrorMode');
public: property BarcodeReaderErrorMode ErrorMode { BarcodeReaderErrorMode get(); void set ( BarcodeReaderErrorMode value); }
BarcodeReaderオブジェクトによってバーコードを読み取るときにエラーが発生する場合があります。デフォルトでは、エラーが発生すると例外がスローされます。この例外では通常、エラーに関するより詳細な情報を提供するため、BarcodeException型のBarcodeException.CodeにいずれかのBarcodeExceptionCode列挙体メンバが設定されます。これは、デフォルト動作です。
たとえば次のような場合など、この動作が望ましくないことがあります。
アプリケーションが「見つかったバーコードが正しい場合」という条件に基づいてバーコードを複合化しようとしている場合。破損したバーコードがある場合のアクションは、「これを無視して続行する」です。この場合、エラーモードをBarcodeReaderErrorMode.IgnoreAllに設定することができます。
画像から複数のバーコードを読み取るアプリケーションで、アクションが「破損したバーコードは無視し、見つかった有効なバーコードのみを返す」である場合。この場合は、エラーモードをBarcodeReaderErrorMode.IgnoreAllに設定できます。さらにエラー発生時に通知を受け、後で使用するために例外を保存しておくため、BarcodeReader.ReadSymbologyイベントをサブスクライブできます。これらのエラーは、すべてのバーコードの読み取りが終わって読み取りメソッドが戻った後に処理できます。
LEADTOOLS C#とVBバーコードデモは、ErrorModeの値をBarcodeReaderErrorMode.IgnoreAllに変更し、BarcodeReader.ReadSymbologyイベントを使用して発生したエラーをリストボックスに表示します。
画像にバーコードが1つも見つからなかった場合は、BarcodeReaderから例外はスローされないことに注意してください。代わりに、BarcodeReader.ReadBarcodeとBarcodeReader.ReadBarcodesメソッドは、null(Visual BasicではNothing)またはBarcodeData型の空の配列を返します。
このサンプルは、imageからすべてのバーコードを読む前に、BarcodeReader.ErrorModeの値を変更します。読み取り操作が戻った後に発生したエラーを表示します。
Imports Leadtools Imports Leadtools.Codecs Imports Leadtools.Forms Imports Leadtools.Barcode Imports Leadtools.ImageProcessing 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
using Leadtools; using Leadtools.Codecs; using Leadtools.Forms; using Leadtools.Barcode; using Leadtools.ImageProcessing; 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"; }
using Leadtools; using Leadtools.Codecs; using Leadtools.Barcode; using Leadtools.ImageProcessing; private List<BarcodeException> myErrors; // List of errors 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 BarcodeException barcodeException = BarcodeException.FromHResult(e.HResult); if (barcodeException != null) { myErrors.Add(barcodeException); } }
using Leadtools; using Leadtools.Codecs; using Leadtools.Forms; using Leadtools.Barcode; using Leadtools.ImageProcessing; using Leadtools.Examples; 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); } }
Imports Leadtools Imports Leadtools.Codecs Imports Leadtools.Forms Imports Leadtools.Barcode Imports Leadtools.ImageProcessing Imports Leadtools.ImageProcessing.Color 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