C♯で一度に複数のバーコードを読み取る方法
IronBarcode は、ExpectMultipleBarcodes = true を設定することで、画像やPDFから複数のバーコードを同時に読み取ることができ、物流、小売、在庫管理アプリケーションのデータ処理を簡素化します。 倉庫システム、販売時点管理アプリケーション、またはドキュメント処理ソリューションを構築する場合でも、IronBarcode の高度な読み取り機能は、信頼性とパフォーマンスを提供します。
この例では、IronBarcodeを使用することで、バーコードが含まれる画像をすばやくスキャンできることを示しています。 ExpectMultipleBarcodes = true と読みたいバーコードのタイプを設定するだけで、自動化されます。
-
1IronBarcode を NuGet Package Manager でインストールします
-
2このコード スニペットをコピーして実行します。
var results = IronBarCode.BarcodeReader.Read("image.png", new IronBarCode.BarcodeReaderOptions { ExpectMultipleBarcodes = true, ExpectBarcodeTypes = IronBarCode.BarcodeEncoding.AllOneDimensional });Dim results = IronBarCode.BarcodeReader.Read("image.png", New IronBarCode.BarcodeReaderOptions With {.ExpectMultipleBarcodes = True, .ExpectBarcodeTypes = IronBarCode.BarcodeEncoding.AllOneDimensional})Dim results = IronBarCode.BarcodeReader.Read("image.png", New IronBarCode.BarcodeReaderOptions With {.ExpectMultipleBarcodes = True, .ExpectBarcodeTypes = IronBarCode.BarcodeEncoding.AllOneDimensional}) -
3実際の環境でテストするためにデプロイする
今日からご自身のプロジェクトでIronBarcodeを使い始めるには、無料トライアルをお試しください。
最小限のワークフロー(5ステップ)
- 複数のバーコードを読み取るためのC#ライブラリをダウンロードする
Readメソッドを使用して、さまざまな画像形式からバーコード値を抽出します。ExpectMultipleBarcodesプロパティを使用して、単一または複数のバーコードの読み取りを設定します。- パフォーマンスを向上させるには、
ExpectMultipleBarcodesプロパティを false に設定します。 - バーコードの値を印刷
画像から複数のバーコードを読み取るにはどうすればよいですか?
デフォルトでは、IronBarcodeはドキュメントを連続してスキャンし、複数のバーコードを読み取ります。 しかし、複数の BarCode が存在する場合でも、1 つの BarCode 値しか返されない場合があります。 これに対処するには、以下のように、複数の BarCode を読み取れるように設定をカスタマイズします。 ExpectMultipleBarcodes プロパティは、BarcodeReaderOptions および PdfBarcodeReaderOptions クラスの両方に存在し、画像およびPDFドキュメント内のバーコードを読み取ることができます。

using IronBarCode;
using System;
// Set the option to read multiple barcodes
BarcodeReaderOptions options = new BarcodeReaderOptions()
{
ExpectMultipleBarcodes = true,
ExpectBarcodeTypes = BarcodeEncoding.AllOneDimensional,
};
// Read barcode
var results = BarcodeReader.Read("testbc1.png", options);
foreach (var result in results)
{
Console.WriteLine(result.ToString());
}using IronBarCode;
using System;
// Set the option to read multiple barcodes
BarcodeReaderOptions options = new BarcodeReaderOptions()
{
ExpectMultipleBarcodes = true,
ExpectBarcodeTypes = BarcodeEncoding.AllOneDimensional,
};
// Read barcode
var results = BarcodeReader.Read("testbc1.png", options);
foreach (var result in results)
{
Console.WriteLine(result.ToString());
}Imports IronBarCode
Imports System
' Set the option to read multiple barcodes
Private options As New BarcodeReaderOptions() With {
.ExpectMultipleBarcodes = True,
.ExpectBarcodeTypes = BarcodeEncoding.AllOneDimensional
}
' Read barcode
Private results = BarcodeReader.Read("testbc1.png", options)
For Each result In results
Console.WriteLine(result.ToString())
Next resultImports IronBarCode
Imports System
' Set the option to read multiple barcodes
Private options As New BarcodeReaderOptions() With {
.ExpectMultipleBarcodes = True,
.ExpectBarcodeTypes = BarcodeEncoding.AllOneDimensional
}
' Read barcode
Private results = BarcodeReader.Read("testbc1.png", options)
For Each result In results
Console.WriteLine(result.ToString())
Next resultExpectMultipleBarcodes を true に設定すると、IronBarcode はドキュメント全体をスキャンして複数のバーコードを探し、それらを BarcodeResults 変数に保存します。 foreach ループを使用して、すべてのバーコード値に容易にアクセスし、コンソールに出力することができます。
高度な複数バーコードリーディングシナリオ
複数の BarCode を扱う場合、追加設定が必要なシナリオに遭遇する可能性があります。 以下は、複雑な文書から異なるフォーマットの複数の BarCode を読み取る方法を示す包括的な例です:
using IronBarCode;
using System;
using System.Linq;
// Configure advanced options for mixed barcode types
BarcodeReaderOptions advancedOptions = new BarcodeReaderOptions()
{
ExpectMultipleBarcodes = true,
// Read both 1D and 2D barcodes
ExpectBarcodeTypes = BarcodeEncoding.AllOneDimensional | BarcodeEncoding.QRCode | BarcodeEncoding.DataMatrix,
// Apply image correction filters for better accuracy
ImageFilters = new ImageFilterCollection() {
new SharpenFilter(),
new ContrastFilter()
},
// Set speed vs accuracy balance
Speed = ReadingSpeed.Balanced
};
// Read barcodes from the image
var imageResults = BarcodeReader.Read("mixed-barcodes.jpg", advancedOptions);
// Process results with error handling
foreach (var result in imageResults)
{
Console.WriteLine($"Barcode Type: {result.BarcodeType}");
Console.WriteLine($"Value: {result.Value}");
Console.WriteLine($"Page: {result.PageNumber}");
Console.WriteLine("---");
}using IronBarCode;
using System;
using System.Linq;
// Configure advanced options for mixed barcode types
BarcodeReaderOptions advancedOptions = new BarcodeReaderOptions()
{
ExpectMultipleBarcodes = true,
// Read both 1D and 2D barcodes
ExpectBarcodeTypes = BarcodeEncoding.AllOneDimensional | BarcodeEncoding.QRCode | BarcodeEncoding.DataMatrix,
// Apply image correction filters for better accuracy
ImageFilters = new ImageFilterCollection() {
new SharpenFilter(),
new ContrastFilter()
},
// Set speed vs accuracy balance
Speed = ReadingSpeed.Balanced
};
// Read barcodes from the image
var imageResults = BarcodeReader.Read("mixed-barcodes.jpg", advancedOptions);
// Process results with error handling
foreach (var result in imageResults)
{
Console.WriteLine($"Barcode Type: {result.BarcodeType}");
Console.WriteLine($"Value: {result.Value}");
Console.WriteLine($"Page: {result.PageNumber}");
Console.WriteLine("---");
}Imports IronBarCode
Imports System
Imports System.Linq
' Configure advanced options for mixed barcode types
Dim advancedOptions As New BarcodeReaderOptions() With {
.ExpectMultipleBarcodes = True,
' Read both 1D and 2D barcodes
.ExpectBarcodeTypes = BarcodeEncoding.AllOneDimensional Or BarcodeEncoding.QRCode Or BarcodeEncoding.DataMatrix,
' Apply image correction filters for better accuracy
.ImageFilters = New ImageFilterCollection() From {
New SharpenFilter(),
New ContrastFilter()
},
' Set speed vs accuracy balance
.Speed = ReadingSpeed.Balanced
}
' Read barcodes from the image
Dim imageResults = BarcodeReader.Read("mixed-barcodes.jpg", advancedOptions)
' Process results with error handling
For Each result In imageResults
Console.WriteLine($"Barcode Type: {result.BarcodeType}")
Console.WriteLine($"Value: {result.Value}")
Console.WriteLine($"Page: {result.PageNumber}")
Console.WriteLine("---")
NextImports IronBarCode
Imports System
Imports System.Linq
' Configure advanced options for mixed barcode types
Dim advancedOptions As New BarcodeReaderOptions() With {
.ExpectMultipleBarcodes = True,
' Read both 1D and 2D barcodes
.ExpectBarcodeTypes = BarcodeEncoding.AllOneDimensional Or BarcodeEncoding.QRCode Or BarcodeEncoding.DataMatrix,
' Apply image correction filters for better accuracy
.ImageFilters = New ImageFilterCollection() From {
New SharpenFilter(),
New ContrastFilter()
},
' Set speed vs accuracy balance
.Speed = ReadingSpeed.Balanced
}
' Read barcodes from the image
Dim imageResults = BarcodeReader.Read("mixed-barcodes.jpg", advancedOptions)
' Process results with error handling
For Each result In imageResults
Console.WriteLine($"Barcode Type: {result.BarcodeType}")
Console.WriteLine($"Value: {result.Value}")
Console.WriteLine($"Page: {result.PageNumber}")
Console.WriteLine("---")
Nextこの高度な例では、いくつかの重要な機能を紹介します:
- 混合バーコードフォーマットサポート: 異なるバーコードエンコードタイプの組み合わせ
- イメージ補正フィルター: 読み取り精度を向上させるために画像フィルターを使用する
- 読み取り速度の最適化: 読み取り速度オプションで速度と精度のバランスをとる
- 信頼度しきい値: 信頼度しきい値リーダーオプションで結果をフィルタリングする
パフォーマンスを向上させるために単一のバーコードを読み取るにはどうすればよいですか?
IronBarcodeは画像やPDFのシングルバーコード、マルチバーコードを読み取ります。 デフォルトでは、バーコードが1つしかない場合でも、エンジンはドキュメント全体をスキャンします。 単一のバーコードを読み取る場合のパフォーマンスを向上させるためには、ExpectMultipleBarcodes を false に設定してください。 これにより、最初の BarCode を検出した後、エンジンが文書全体をスキャンすることがなくなり、バーコードの検索が高速化されます。 以下のコードは、このアプローチを示しています。

using IronBarCode;
using System;
// Set the option to read single barcode
BarcodeReaderOptions options = new BarcodeReaderOptions()
{
ExpectMultipleBarcodes = false,
ExpectBarcodeTypes = BarcodeEncoding.AllOneDimensional,
};
// Read barcode
var results = BarcodeReader.Read("testbc1.png", options);
foreach (var result in results)
{
Console.WriteLine(result.ToString());
}using IronBarCode;
using System;
// Set the option to read single barcode
BarcodeReaderOptions options = new BarcodeReaderOptions()
{
ExpectMultipleBarcodes = false,
ExpectBarcodeTypes = BarcodeEncoding.AllOneDimensional,
};
// Read barcode
var results = BarcodeReader.Read("testbc1.png", options);
foreach (var result in results)
{
Console.WriteLine(result.ToString());
}Imports IronBarCode
Imports System
' Set the option to read single barcode
Private options As New BarcodeReaderOptions() With {
.ExpectMultipleBarcodes = False,
.ExpectBarcodeTypes = BarcodeEncoding.AllOneDimensional
}
' Read barcode
Private results = BarcodeReader.Read("testbc1.png", options)
For Each result In results
Console.WriteLine(result.ToString())
Next resultImports IronBarCode
Imports System
' Set the option to read single barcode
Private options As New BarcodeReaderOptions() With {
.ExpectMultipleBarcodes = False,
.ExpectBarcodeTypes = BarcodeEncoding.AllOneDimensional
}
' Read barcode
Private results = BarcodeReader.Read("testbc1.png", options)
For Each result In results
Console.WriteLine(result.ToString())
Next resultこの例では、以前と同じ複数バーコードの画像を使用しましたが、ExpectMultipleBarcodes を false に設定しました。 その結果、最初のバーコード値のみが返され、スキャンプロセスは最初のバーコードが取得されると停止します。
クロップ領域で単一バーコード読み取りを最適化する
単一バーコードの読み取り性能をさらに向上させるには、ExpectMultipleBarcodes = false 設定を クロップ領域指定と組み合わせてください。 このテクニックは、バーコードのおおよその位置がわかっている場合に特に役立ちます:
using IronBarCode;
using IronSoftware.Drawing;
using System;
using System.Linq;
// Define a crop region where the barcode is likely located
var cropRegion = new Rectangle(100, 100, 300, 200);
// Configure options for optimal single barcode reading
BarcodeReaderOptions optimizedOptions = new BarcodeReaderOptions()
{
ExpectMultipleBarcodes = false,
ExpectBarcodeTypes = BarcodeEncoding.Code128,
CropArea = cropRegion,
Speed = ReadingSpeed.Faster
};
// Read with optimized settings
var result = BarcodeReader.Read("product-label.png", optimizedOptions).FirstOrDefault();
if (result != null)
{
Console.WriteLine($"Barcode found: {result.Value}");
}using IronBarCode;
using IronSoftware.Drawing;
using System;
using System.Linq;
// Define a crop region where the barcode is likely located
var cropRegion = new Rectangle(100, 100, 300, 200);
// Configure options for optimal single barcode reading
BarcodeReaderOptions optimizedOptions = new BarcodeReaderOptions()
{
ExpectMultipleBarcodes = false,
ExpectBarcodeTypes = BarcodeEncoding.Code128,
CropArea = cropRegion,
Speed = ReadingSpeed.Faster
};
// Read with optimized settings
var result = BarcodeReader.Read("product-label.png", optimizedOptions).FirstOrDefault();
if (result != null)
{
Console.WriteLine($"Barcode found: {result.Value}");
}Imports IronBarCode
Imports IronSoftware.Drawing
Imports System
Imports System.Linq
' Define a crop region where the barcode is likely located
Dim cropRegion = New Rectangle(100, 100, 300, 200)
' Configure options for optimal single barcode reading
Dim optimizedOptions As New BarcodeReaderOptions() With {
.ExpectMultipleBarcodes = False,
.ExpectBarcodeTypes = BarcodeEncoding.Code128,
.CropArea = cropRegion,
.Speed = ReadingSpeed.Faster
}
' Read with optimized settings
Dim result = BarcodeReader.Read("product-label.png", optimizedOptions).FirstOrDefault()
If result IsNot Nothing Then
Console.WriteLine($"Barcode found: {result.Value}")
End IfImports IronBarCode
Imports IronSoftware.Drawing
Imports System
Imports System.Linq
' Define a crop region where the barcode is likely located
Dim cropRegion = New Rectangle(100, 100, 300, 200)
' Configure options for optimal single barcode reading
Dim optimizedOptions As New BarcodeReaderOptions() With {
.ExpectMultipleBarcodes = False,
.ExpectBarcodeTypes = BarcodeEncoding.Code128,
.CropArea = cropRegion,
.Speed = ReadingSpeed.Faster
}
' Read with optimized settings
Dim result = BarcodeReader.Read("product-label.png", optimizedOptions).FirstOrDefault()
If result IsNot Nothing Then
Console.WriteLine($"Barcode found: {result.Value}")
End If単一のバーコード読み取りはどれくらい速いですか?
ExpectMultipleBarcodes = false を設定することで、単一バーコードの読み取り効率が大幅に向上します。 パフォーマンスの向上は、高解像度画像を扱う場合や、非同期バーコード読み取りを高スループットアプリケーションで実装する場合に特に顕著です。
ExpectMultipleBarcodes = false を設定すると、エンジンが最初のバーコードを検出した時点で停止し、ドキュメント全体をスキャンする代わりに、単一バーコードのワークロードの読み取り時間が目立って短縮されます。 実際のパフォーマンスは、以下の条件によって異なります:
- 画像の解像度と複雑さ
- 画像に含まれる BarCode の数
- 選択されたバーコード形式
- 応用画像フィルタ
- ハードウェア仕様
複数バーコード読み取りのベストプラクティス
本番アプリケーションで複数の BarCode 読み取りを実装する場合は、以下のベストプラクティスを考慮してください:
- 期待されるバーコードタイプの指定:
BarcodeEncoding.Allの代わりに、期待するフォーマットのみを指定してください。 これにより、パフォーマンスが大幅に向上します。 - 適切な画像フォーマットの使用: より良い結果を得るために、高コントラストの画像を使用してください。 最適なバーコード画像を作成する方法について詳しく学んでください。
- 不完全なバーコードの処理: 現実世界のバーコードは破損しているか、印刷が悪い場合があります。 画像補正技術を使用して、読み取り成功率を向上させてください。
- ストリーム処理: 大量データの場合は、ストリームからの読み取りを検討してメモリ使用量を最適化してください。
- エラー処理: バーコードを読み取れないシナリオに備えて、適切なエラー処理を必ず実装してください。
try
{
var results = BarcodeReader.Read("barcodes.png", new BarcodeReaderOptions
{
ExpectMultipleBarcodes = true
});
if (!results.Any())
{
Console.WriteLine("No barcodes found in the image");
}
else
{
Console.WriteLine($"Found {results.Count()} barcodes");
}
}
catch (Exception ex)
{
Console.WriteLine($"Error reading barcodes: {ex.Message}");
// Log error for debugging
}try
{
var results = BarcodeReader.Read("barcodes.png", new BarcodeReaderOptions
{
ExpectMultipleBarcodes = true
});
if (!results.Any())
{
Console.WriteLine("No barcodes found in the image");
}
else
{
Console.WriteLine($"Found {results.Count()} barcodes");
}
}
catch (Exception ex)
{
Console.WriteLine($"Error reading barcodes: {ex.Message}");
// Log error for debugging
}Try
Dim results = BarcodeReader.Read("barcodes.png", New BarcodeReaderOptions With {
.ExpectMultipleBarcodes = True
})
If Not results.Any() Then
Console.WriteLine("No barcodes found in the image")
Else
Console.WriteLine($"Found {results.Count()} barcodes")
End If
Catch ex As Exception
Console.WriteLine($"Error reading barcodes: {ex.Message}")
' Log error for debugging
End TryTry
Dim results = BarcodeReader.Read("barcodes.png", New BarcodeReaderOptions With {
.ExpectMultipleBarcodes = True
})
If Not results.Any() Then
Console.WriteLine("No barcodes found in the image")
Else
Console.WriteLine($"Found {results.Count()} barcodes")
End If
Catch ex As Exception
Console.WriteLine($"Error reading barcodes: {ex.Message}")
' Log error for debugging
End Tryこれらのプラクティスに従い、IronBarcode の包括的な機能を活用することで、さまざまな業界やユースケースで複数バーコード読み取りシナリオを効率的に処理する信頼性の高いアプリケーションを構築できます。
よくある質問
IronBarcodeで複数のバーコードスキャンを有効にする方法は?
IronBarcodeで複数のバーコードスキャンを有効にするには、'ExpectMultipleBarcodes'プロパティをBarcodeReaderOptionsまたはPdfBarcodeReaderOptionsクラス内でtrueに設定します。
IronBarcodeのExpectMultipleBarcodesプロパティの目的は何ですか?
'ExpectMultipleBarcodes'プロパティは、IronBarcodeが画像またはPDFから複数のバーコードを同時に読み取ることを可能にし、物流や小売業のアプリケーションでデータ抽出を最適化します。
IronBarcodeは1Dおよび2Dバーコードの両方を読み取ることができますか?
はい、IronBarcodeは1Dと2Dの両方のバーコードを読み取ることができます。BarcodeReaderOptionsで、'ExpectBarcodeTypes'プロパティを設定することで、期待するバーコードの種類を指定できます。
IronBarcodeを使用して1つのバーコードを読み取る際のパフォーマンスを向上させる方法は?
単一のバーコードを読み取るときにパフォーマンスを向上させるには、BarcodeReaderOptionsで'ExpectMultipleBarcodes'をfalseに設定します。これにより、最初のバーコードを検出した後にスキャンプロセスが停止します。
異なるフォーマットの複数のバーコードを読み取るために利用可能な設定は何ですか?
IronBarcodeは、異なるバーコードのデコードタイプを組み合わせる、高度な画像補正フィルターを適用する、読み取り速度を調整するなど、複数のバーコードフォーマットを扱うための高度なオプションを提供します。
IronBarcodeで画像補正はどのようにバーコード読み取りを向上させますか?
シャープフィルターやコントラストフィルターなどの画像補正フィルターを適用することで、画像の質が悪い場合やバーコードが損傷している場合に、バーコード読み取りの精度を向上させることができます。
最適なバーコード読み取りパフォーマンスのために推奨される戦略は何ですか?
最適なパフォーマンスのためには、期待されるバーコードタイプを指定し、高コントラストの画像を使用し、画像補正技術を実装し、大量のバッチの場合はストリームから読み取り、エラー処理を含めます。
IronBarcodeでバーコードを読み取る際にエラーをどのように処理できますか?
バーコードが読み取れない場合を管理するためにtry-catchブロックを使用して適切なエラー処理を実装し、必要に応じてデバッグ用にエラーをログに記録します。
IronBarcodeでは大規模なバーコードバッチでストリーム処理がサポートされていますか?
はい、IronBarcodeはストリームからのバーコード読み取りをサポートしており、高スループットのアプリケーションや大きなバッチ処理においてメモリ使用を最適化するのに特に有用です。