C#で複数のバーコードを同時に読む方法

C#で複数の BarCode を一度に読み取る方法

This article was translated from English: Does it need improvement?
Translated
View the article in English

IronBarcodeは、複数のバーコードを期待 = trueを設定することで、画像やPDFから複数のバーコードを同時に読み取ることを可能にし、物流、小売、在庫管理アプリケーションのデータ処理を合理化します。 倉庫システム、小売POSアプリケーション、または文書処理ソリューションの構築のいずれにおいても、IronBarcodeの高度な読み取り機能は、必要な信頼性とパフォーマンスを提供します。

クイックスタート: 画像からすべてのバーコードを簡単に読み取る

この例では、IronBarcodeを使用することで、バーコードが含まれる画像をすばやくスキャンできることを示しています。 ExpectMultipleBarcodes=trueを設定するだけです。

Nuget Icon今すぐ NuGet で PDF を作成してみましょう:

  1. NuGet パッケージ マネージャーを使用して IronBarcode をインストールします

    PM > Install-Package BarCode

  2. このコード スニペットをコピーして実行します。

    var results = IronBarCode.BarcodeReader.Read("image.png", new IronBarCode.BarcodeReaderOptions { 複数のバーコードを期待 = true, ExpectBarcodeTypes = IronBarCode.BarcodeEncoding.AllOneDimensional });
  3. 実際の環境でテストするためにデプロイする

    今すぐ無料トライアルでプロジェクトに IronBarcode を使い始めましょう
    arrow pointer


画像から複数の BarCode を読み取るにはどうすればよいですか?

デフォルトでは、IronBarcodeはドキュメントを連続してスキャンし、複数のバーコードを読み取ります。 しかし、複数の BarCode が存在する場合でも、1 つの BarCode 値しか返されない場合があります。 これに対処するには、以下のように、複数の BarCode を読み取れるように設定をカスタマイズします。 ExpectMultipleBarcodesプロパティは、BarcodeReaderOptionsPdfBarcodeReaderOptionsクラスの両方に存在し、画像と PDF 文書の両方でバーコードを読み取るために使用できます。

A、B、Cとラベル付けされた3つのサンプルバーコードで、マルチバーコード読み取りデモ用の異なるバーパターンを示す
:path=/static-assets/barcode/content-code-examples/how-to/read-multiple-barcodes-read-multiple-barcodes.cs
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());
}
$vbLabelText   $csharpLabel

ExpectMultipleBarcodesをtrueに設定すると、IronBarcodeはドキュメント全体をスキャンして複数のバーコードを見つけ、BarcodeResults変数に格納します。 foreachループを使用すると、すべてのバーコード値に簡単にアクセスし、コンソールに表示することができます。

高度な複数の BarCode 読み取りシナリオ

複数の BarCode を扱う場合、追加設定が必要なシナリオに遭遇する可能性があります。 以下は、複雑な文書から異なるフォーマットの複数の BarCode を読み取る方法を示す包括的な例です:

using IronBarCode;
using System;
using System.Linq;

// Configure advanced options for mixed barcode types
BarcodeReaderOptions advancedOptions = new BarcodeReaderOptions()
{
    複数のバーコードを期待 = 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 from various sources
var imageResults = BarcodeReader.Read("mixed-barcodes.jpg", advancedOptions);
var pdfResults = BarcodeReader.ReadPdf("document-with-barcodes.pdf", advancedOptions);

// Process results with error handling
foreach (var result in imageResults)
{
    Console.WriteLine($"Barcode Type: {result.BarcodeType}");
    Console.WriteLine($"Value: {result.Value}");
    Console.WriteLine($"Confidence: {result.Confidence}%");
    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()
{
    複数のバーコードを期待 = 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 from various sources
var imageResults = BarcodeReader.Read("mixed-barcodes.jpg", advancedOptions);
var pdfResults = BarcodeReader.ReadPdf("document-with-barcodes.pdf", advancedOptions);

// Process results with error handling
foreach (var result in imageResults)
{
    Console.WriteLine($"Barcode Type: {result.BarcodeType}");
    Console.WriteLine($"Value: {result.Value}");
    Console.WriteLine($"Confidence: {result.Confidence}%");
    Console.WriteLine($"Page: {result.PageNumber}");
    Console.WriteLine("---");
}
$vbLabelText   $csharpLabel

この高度な例では、いくつかの重要な機能を紹介します:

パフォーマンスを向上させるために単一の BarCode を読み取るにはどうすればよいですか?

IronBarcodeは画像やPDFのシングルバーコード、マルチバーコードを読み取ります。 デフォルトでは、バーコードが1つしかない場合でも、エンジンはドキュメント全体をスキャンします。 単一の BarCode を読み取る際のパフォーマンスを向上させるには、ExpectMultipleBarcodes を false に設定してください。 これにより、最初の BarCode を検出した後、エンジンが文書全体をスキャンすることがなくなり、バーコードの検索が高速化されます。 以下のコードは、このアプローチを示しています。

バーコード読み取りデモ用のA、B、Cとラベル付けされた3つの同じサンプルバーコード
:path=/static-assets/barcode/content-code-examples/how-to/read-multiple-barcodes-read-single-barcode.cs
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());
}
$vbLabelText   $csharpLabel

この例では、前と同じ複数の BarCode を持つ画像を使用しますが、ExpectMultipleBarcodes を false に設定します。 その結果、最初のバーコード値のみが返され、スキャンプロセスは最初のバーコードが取得されると停止します。

クロップ領域による単一 BarCode 読み取りの最適化

単一バーコードを読み取る際のパフォーマンスをさらに向上させるには、複数のバーコードを期待 = false 設定と crop region specification を組み合わせてください。 このテクニックは、バーコードのおおよその位置がわかっている場合に特に役立ちます:

using IronBarCode;
using IronSoftware.Drawing;

// 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()
{
    複数のバーコードを期待 = 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}");
    Console.WriteLine($"Read time: {result.ReadTime}ms");
}
using IronBarCode;
using IronSoftware.Drawing;

// 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()
{
    複数のバーコードを期待 = 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}");
    Console.WriteLine($"Read time: {result.ReadTime}ms");
}
$vbLabelText   $csharpLabel

シングルバーコード読み取りはどれくらい速いですか?

ExpectMultipleBarcodes を false に設定すると、単一バーコードの読み取り効率が大幅に向上します。 パフォーマンスの向上は、高解像度の画像を扱う場合や、高スループットのアプリケーションで非同期バーコード読み取りを実装する場合に特に顕著です。

提供されたコードスニペットを使用すると、同じマシンでExpectMultipleBarcodesをtrueとfalseに設定した場合のパフォーマンス差の大まかな推定は次のとおりです。

複数のバーコードを期待 = true複数のバーコードを期待 = false
00.91秒00.10秒

これは、単一の BarCode を読み取る際に、約 9 倍のパフォーマンス向上を意味します。 実際のパフォーマンスは、以下の条件によって異なります:

  • 画像の解像度と複雑さ
  • 画像に含まれる BarCode の数
  • 選択されたバーコード形式
  • 応用画像フィルタ
  • ハードウェア仕様

複数の BarCode を読み取るためのベストプラクティス

本番アプリケーションで複数の BarCode 読み取りを実装する場合は、以下のベストプラクティスを考慮してください:

1.Specify Expected Barcode Types: BarcodeEncoding.Allを使用する代わりに、期待するフォーマットのみを指定してください。 これにより、パフォーマンスが大幅に向上します。

2.適切な画像形式を使用する:最良の結果を得るためには、コントラストの高い画像を使用してください。 最適な BarCode 画像の作成については、こちらをご覧ください。

3.不完全なBarCodeを扱う:実世界のBarCodeは破損していたり、印刷が不完全だったりすることがあります。 画像修正技術を使用して、読解の成功率を高めます。

4.ストリーム処理:大きなバッチでは、ストリームからの読み取りを検討して、メモリ使用量を最適化します。

5.エラー処理:BarCodeが読み取れないシナリオに対しては、常に適切なエラー処理を実装してください:

try
{
    var results = BarcodeReader.Read("barcodes.png", new BarcodeReaderOptions 
    { 
        複数のバーコードを期待 = 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 
    { 
        複数のバーコードを期待 = 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
}
$vbLabelText   $csharpLabel

これらのプラクティスに従い、IronBarcodeの包括的な機能を活用することで、様々な業界やユースケースにおける複数のバーコード読み取りシナリオを効率的に処理する堅牢なアプリケーションを構築することができます。

よくある質問

C# で 1 つの画像から複数の BarCode を読み取る方法を教えてください。

IronBarcodeでは、BarcodeReaderOptionsでExpectMultipleBarcodes = trueを設定することで、1つの画像から複数のバーコードを読み取ることができます。これにより、IronBarcodeはドキュメント全体をスキャンし、見つかったすべてのバーコードをBarCodeResultsコレクションとして返すことができます。

画像内のすべての BarCode をスキャンする最速の方法は?

最速のアプローチはIronBarcodeのReadメソッドをExpectMultipleBarcodes = trueで使用することです: var results = IronBarCode.BarcodeReader.Read("image.png", new IronBarCode.BarcodeReaderOptions { ExpectMultipleBarcodes = true }).この最小限のコードは複雑な設定なしにすべてのバーコード値を抽出します。

画像だけでなく、PDF文書から複数のBarCodeを読み取ることはできますか?

はい、IronBarcodeは画像とPDFドキュメントの両方から複数のバーコードを読み取ることをサポートしています。ExpectMultipleBarcodesプロパティはBarCodeReaderOptionsとPdfBarcodeReaderOptionsの両クラスで利用可能で、任意のドキュメントタイプに対して複数のバーコード読み取りを設定することができます。

ExpectMultipleBarcodes を true に設定しないとどうなりますか?

デフォルトでは、IronBarcodeは複数のバーコードについてドキュメントを継続的にスキャンします。しかし、場合によっては複数のバーコードが存在しても1つのバーコード値しか返されないことがあります。ExpectMultipleBarcodes = trueを明示的に設定することで、IronBarcodeはドキュメント内のすべてのバーコードをスキャンして返します。

複数の BarCode を読み取った後、個々の BarCode 値にアクセスするにはどうすればよいですか?

IronBarcodeで複数のバーコードを読み取った後、結果はBarCodeResults変数に格納されます。foreachループを使ってコレクションを繰り返し、各バーコードの値、テキスト、フォーマットのプロパティを処理することで、個々のバーコードの値に簡単にアクセスできます。

複数の BarCode を読み取ることは、小売や物流のアプリケーションに適していますか?

IronBarcodeの複数バーコード読み取り機能は、小売POSシステム、倉庫管理、物流追跡、在庫管理アプリケーションに最適です。出荷ラベル、製品カタログ、在庫シートのすべてのバーコードを同時に効率的にスキャンすることで、データ処理を合理化します。

複数のバーコードを読み取る際に、どのバーコードタイプを検索するかを指定できますか?

はい、IronBarcodeではExpectBarcodeTypesプロパティを使って予想されるバーコードタイプを指定することができます。スキャンのパフォーマンスを最適化するために、AllOneDimensional、QRCode、またはサポートされているバーコードタイプの任意の組み合わせのような特定のフォーマットをスキャンするように設定できます。

ExpectMultipleBarcodes の設定はスキャンのパフォーマンスに影響しますか?

ExpectMultipleBarcodes = falseを設定すると、ドキュメントにバーコードが1つしか存在しないことが分かっている場合にパフォーマンスを向上させることができます。IronBarcodeは最初のバーコードを見つけるとスキャンを停止し、シングルバーコードシナリオの高速化を実現すると同時に、必要に応じてマルチバーコードの読み取りにも柔軟に対応します。

Hairil Hasyimi Bin Omar
ソフトウェアエンジニア
すべての優れたエンジニアのように、ハイリルは熱心な学習者です。彼はC#、Python、Javaの知識を磨き、その知識を活用してIron Softwareのチームメンバーに価値を追加しています。ハイリルはマレーシアのマラ工科大学からIron Softwareのチームに参加し、化学およびプロセス工学の学士号を取得しました。
準備はできましたか?
Nuget ダウンロード 2,035,202 | バージョン: 2025.12 リリース