バーコードチェックサムの検証方法とC#でのフォーマット対応読み取りを使用する方法

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

バーコードのチェックサムは代替エラーを捕捉するために存在します — EAN-13ラベルで1つの桁を入れ替えるだけで、パッケージが誤った倉庫に送られる可能性があります。 フォーマット対応の読み取りは、デコーダーを期待されるシンボルに制約することでバックグラウンドノイズからの誤検出を排除し、関連のないフォーマット検知をスキップしてスキャン時間を短縮する第二の検証レイヤーを追加します。

IronBarcodeはデコード中にチェックサムの検証を暗黙的に行います — すべてのシンボルのチェックディジットアルゴリズムが自動的に実行され、検証に失敗したバーコードは呼び出しコードに到達する前に破棄されます。 RemoveFalsePositiveは二次検証パスを追加します。 この記事では、チェックサムの動作、フォーマット制約付きの読み込み、および組み合わせた検証パターンをカバーしています。

クイックスタート: チェックサムとフォーマット制約でバーコードを検証する

RemoveFalsePositiveで設定し、自動チェックサム検証を伴う期待されるシンボルに読み込みを制約します。

  1. IronBarcode をNuGetパッケージマネージャでインストール

    PM > Install-Package BarCode
  2. このコード スニペットをコピーして実行します。

    using IronBarCode;
    
    // Format-constrained read with false-positive removal
    var options = new BarcodeReaderOptions
    {
        ExpectBarcodeTypes = BarcodeEncoding.EAN13 | BarcodeEncoding.Code128,
        RemoveFalsePositive = true,
        Speed = ReadingSpeed.Balanced
    };
    
    BarcodeResults results = BarcodeReader.Read("label.png", options);
  3. 実際の環境でテストするためにデプロイする

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

    arrow pointer

バーコードチェックサムを検証する方法

IronBarcodeは各シンボルの仕様の一部としてデコード時にチェックサムを検証します。 ライブラリがEAN-13バーコードを読み取ると、Mod10チェックディジットが最初の12桁から計算され、13番目の桁と比較されます。 不一致はバーコードが静かに拒否される原因となります — BarcodeResultsコレクションには決して表示されません。 同じ原則がUPC-A、UPC-E、EAN-8、Code128、ITFなど、必須チェックディジット付きのすべてのフォーマットに適用されます。

この暗黙のモデルは、明示的なトグルを提供するライブラリとは異なります。 以下の表で2つのアプローチを比較します:

チェックサム検証モデルの比較 — IronBarcode対Aspose.BarCode
アスペクトIronBarcodeAspose.BarCode
検証トリガー自動 — 各デコード中に実行明示的 — `チェックサムValidation.On` / `Off` / `Default`
開発者のアクションが必要不要 — 無効なバーコードは結果から除外読み取り前に`BarcodeSettings.チェックサムValidation`を設定する必要あり
チェックサムを無効にする?公開されていない — 必須フォーマットには常にチェックサムが適用される可能 — `チェックサムValidation.Off`は検証をスキップ
オプションのチェックサムフォーマット(Code39)リーダーは`Confidence` + `RemoveFalsePositive`を使用して低品質の読み取りをフィルタします`Enableチェックサム.はい`で明示的に有効にする必要あり
失敗時の挙動バーコードが静かに結果から省略されるバーコードは手動検査用に個別のチェックサム値で表示される可能性あり

実際の結果として:IronBarcodeからBarcodeResultを受け取る呼び出しコードは、必須のチェックサムを持つフォーマットのためにチェックディジットが有効であると信頼できます。 設定手順を忘れることはなく、間違った状態に意図せずフラグを残すこともありません。

チェックサムがオプションのシンボルの場合 — 主な例はCode39 — ライブラリは明示的なチェックサムトグルではなく、信頼スコアリングとRemoveFalsePositiveメカニズムに依存します。 BarcodeResult商品説明プロパティはバーコードがどの程度信頼性を持ってデコードされたかを示し、BarcodeReaderOptions(デフォルト0.7)はML検出の敷居を設定します。

//:path=/static-assets/barcode/content-code-examples/how-to/checksum-and-format-validation/checksum-confidence.cs
using IronBarCode;

var options = new BarcodeReaderOptions
{
    ExpectBarcodeTypes = BarcodeEncoding.AllOneDimensional,
    RemoveFalsePositive = true,
    ConfidenceThreshold = 0.85,
    Speed = ReadingSpeed.Detailed
};

BarcodeResults results = BarcodeReader.Read("warehouse-rack.png", options);

foreach (BarcodeResult result in results)
{
    // Every result here has passed checksum validation (mandatory formats)
    // and exceeded the 85% confidence threshold (all formats)
    Console.WriteLine($"[{result.BarcodeType}] {result.値} — Confidence: {result.Confidence}%");
}

if (results.Count == 0)
{
    Console.Error.WriteLine("No valid barcodes found. Possible causes:");
    Console.Error.WriteLine("  - Check digit mismatch (barcode silently rejected)");
    Console.Error.WriteLine("  - Confidence below 85% threshold");
    Console.Error.WriteLine("  - Format not in ExpectBarcodeTypes");
}
//:path=/static-assets/barcode/content-code-examples/how-to/checksum-and-format-validation/checksum-confidence.cs
using IronBarCode;

var options = new BarcodeReaderOptions
{
    ExpectBarcodeTypes = BarcodeEncoding.AllOneDimensional,
    RemoveFalsePositive = true,
    ConfidenceThreshold = 0.85,
    Speed = ReadingSpeed.Detailed
};

BarcodeResults results = BarcodeReader.Read("warehouse-rack.png", options);

foreach (BarcodeResult result in results)
{
    // Every result here has passed checksum validation (mandatory formats)
    // and exceeded the 85% confidence threshold (all formats)
    Console.WriteLine($"[{result.BarcodeType}] {result.値} — Confidence: {result.Confidence}%");
}

if (results.Count == 0)
{
    Console.Error.WriteLine("No valid barcodes found. Possible causes:");
    Console.Error.WriteLine("  - Check digit mismatch (barcode silently rejected)");
    Console.Error.WriteLine("  - Confidence below 85% threshold");
    Console.Error.WriteLine("  - Format not in ExpectBarcodeTypes");
}
$vbLabelText   $csharpLabel

0.7以上にConfidenceThresholdを上げることは、オプションのチェックサムシンボルに対する"より厳しいチェックサム適用"への最も近い相当です。 閾値を下回るがデコードされたバーコードは結果から除外され、固定チェックサム検証を補完する調整可能な品質ゲートを提供します。


フォーマット対応バーコード読み取りを使用する方法

BarcodeEncoding列挙型はフラグ型で、ビット単位のOR演算子で複数のフォーマットを組み合わせることができます。 ExpectBarcodeTypesを設定すると、他の全てのフォーマットの検知ルーチンをスキップして、リーダーをそれらのフォーマットに制限します。

一般的なBarcodeEncoding値
カテゴリ翻訳内容チェックサム
`BarcodeEncoding.All`メタすべてのサポートされているフォーマットをスキャン(デフォルト)フォーマットごと
`BarcodeEncoding.AllOneDimensional`メタ積み重ねを含むすべての線形(1D)フォーマットフォーマットごと
`BarcodeEncoding.AllTwoDimensional`メタすべてのマトリックス/グリッド(2D)フォーマットフォーマットごと
`BarcodeEncoding.Code128`1D高密度アルファベット — ロジスティクス、配送必須(重み付きMod103)
`BarcodeEncoding.EAN13`1D小売商品識別 — 13桁必須(Mod10)
`BarcodeEncoding.QRCode`2D高容量マトリックス — URL、構造化データリード・ソロモンECC
`BarcodeEncoding.Code39`1Dアルファベット — 防衛、自動車オプション(Mod43)
`BarcodeEncoding.UPCA`1D北米の小売 — 12桁必須(Mod10)
`BarcodeEncoding.DataMatrix`2Dコンパクトマトリックス — エレクトロニクス、製薬リード・ソロモンECC
`BarcodeEncoding.PDF417`2D積層 — IDカード、輸送リード・ソロモンECC

期待されるフォーマットを指定すると2つの利点がもたらされます:リーダーは不要なシンボルの検出ルーチンをスキップし(スキャン速度を改善)、予期しないタイプのバーコードは、たとえ画像に物理的に存在していても結果から除外されます。 この第二の特性がフォーマット認識の検証です — リーダーはパイプラインが期待するものだけを返します。

//:path=/static-assets/barcode/content-code-examples/how-to/checksum-and-format-validation/format-constrained.cs
using IronBarCode;

// Scenario: shipping labels contain only Code128 barcodes
var constrainedOptions = new BarcodeReaderOptions
{
    ExpectBarcodeTypes = BarcodeEncoding.Code128,
    Speed = ReadingSpeed.Faster,
    ExpectMultipleBarcodes = false
};

// Scenario: auto-detect all formats (default behavior)
var broadOptions = new BarcodeReaderOptions
{
    ExpectBarcodeTypes = BarcodeEncoding.All,
    Speed = ReadingSpeed.Detailed,
    ExpectMultipleBarcodes = true
};

string imagePath = "shipping-label.png";

// Constrained read — faster, only returns Code128 results
BarcodeResults constrained = BarcodeReader.Read(imagePath, constrainedOptions);
Console.WriteLine($"Constrained: {constrained.Count} Code128 barcode(s) found");

// Broad read — slower, returns all detected formats
BarcodeResults broad = BarcodeReader.Read(imagePath, broadOptions);
Console.WriteLine($"Broad: {broad.Count} barcode(s) found across all formats");

foreach (BarcodeResult result in broad)
{
    Console.WriteLine($"  [{result.BarcodeType}] {result.値}");
}
//:path=/static-assets/barcode/content-code-examples/how-to/checksum-and-format-validation/format-constrained.cs
using IronBarCode;

// Scenario: shipping labels contain only Code128 barcodes
var constrainedOptions = new BarcodeReaderOptions
{
    ExpectBarcodeTypes = BarcodeEncoding.Code128,
    Speed = ReadingSpeed.Faster,
    ExpectMultipleBarcodes = false
};

// Scenario: auto-detect all formats (default behavior)
var broadOptions = new BarcodeReaderOptions
{
    ExpectBarcodeTypes = BarcodeEncoding.All,
    Speed = ReadingSpeed.Detailed,
    ExpectMultipleBarcodes = true
};

string imagePath = "shipping-label.png";

// Constrained read — faster, only returns Code128 results
BarcodeResults constrained = BarcodeReader.Read(imagePath, constrainedOptions);
Console.WriteLine($"Constrained: {constrained.Count} Code128 barcode(s) found");

// Broad read — slower, returns all detected formats
BarcodeResults broad = BarcodeReader.Read(imagePath, broadOptions);
Console.WriteLine($"Broad: {broad.Count} barcode(s) found across all formats");

foreach (BarcodeResult result in broad)
{
    Console.WriteLine($"  [{result.BarcodeType}] {result.値}");
}
$vbLabelText   $csharpLabel

画像が予期しないフォーマットのバーコードを含む場合 — Code128のみを持つべき配送ラベルにQRコードがある — 制約されたリーダーは例外をスローするのではなく結果をゼロにします。 これは設計によるものです:フォーマットの不一致はデータレベルの問題であり、エラー条件ではありません。 フォーマット制約付きの読み取りからの空の結果を検証シグナルとして扱い、調査のために不一致をログに記録すべきです。

複数のバーコードタイプを処理するパイプラインの場合(例:EAN-13商品コードとCode128トラッキング番号の両方を持つ荷札)、期待されるフォーマットを組み合わせてください:

var options = new BarcodeReaderOptions
{
    ExpectBarcodeTypes = BarcodeEncoding.EAN13 | BarcodeEncoding.Code128,
    ExpectMultipleBarcodes = true
};
var options = new BarcodeReaderOptions
{
    ExpectBarcodeTypes = BarcodeEncoding.EAN13 | BarcodeEncoding.Code128,
    ExpectMultipleBarcodes = true
};
$vbLabelText   $csharpLabel

リーダーは画像に存在する他のシンボルを無視しつつ、両方のタイプのバーコードを見つけて返します。 返された各BarcodeResult.BarcodeTypeはデコードされたフォーマットを識別し、ダウンストリームのルーティングロジックを可能にします。

チェックサム検証をサポートするシンボルはどれですか?

すべてのバーコードフォーマットが同じ方法でチェックサムを使用するわけではありません。 次の表は一般的なシンボルをそのエラー検出特性にマッピングし、セキュリティスコアとフィルタリングスコアを各フォーマットにどの程度厳しく設定するかを示します:

シンボルごとのチェックサム特性
シンボルチェックサムタイプ必須か?推奨事項
EAN-13 / EAN-8Mod10はいデフォルト設定で十分 — チェックサムは常に適用されます
UPC-A / UPC-EMod10はいデフォルト設定で十分 — 書き込み中に自動修正されます
Code128加重Mod103はいデフォルトの設定で十分 — 仕様書に従って必須
Code39Mod43オプション`ConfidenceThreshold`を0.8以上に上げ、`RemoveFalsePositive`を有効にする
コーダバーMod16オプションCode39と同じ — 信頼性を品質ゲートとして使用
ITFMod10オプションインターリーブフォーマットのために`RemoveFalsePositive`を有効にする
QRCode / DataMatrixリード・ソロモンECC組み込み誤り訂正は構造的です — 追加の設定は不要
PDF417リード・ソロモンECC組み込みQR / DataMatrixと同じ — 誤り訂正は固有

2Dシンボル(QR、DataMatrix、PDF417)に対しては、誤り訂正はエンコーディング構造自体の一部であり、これらのフォーマットは単純なチェックディジットに頼らずに部分的な損傷から復元できます。 ML検出フェーズにはConfidenceThresholdがそのまま適用されますが、デコードステップはシンボルの内蔵された冗長性から利益を得ます。


チェックサムをフォーマット制約と組み合わせる方法

実稼働パターンでは、BarcodeReaderOptionsオブジェクトとして設定します。 これらのプロパティは共同で、フォーマット制約が検索範囲を狭め、(暗黙の)チェックサム検証がデータの整合性を確保し、信頼スコアリングが限界デコードをフィルタリングし、誤検出の削除が二次検証パスを追加する層化された検証ゲートを形成します。

//:path=/static-assets/barcode/content-code-examples/how-to/checksum-and-format-validation/combined-validation.cs
using IronBarCode;

// Production configuration: retail POS scanning with EAN-13 and UPC-A
var options = new BarcodeReaderOptions
{
    // Layer 1: Format constraint — only retail symbologies
    ExpectBarcodeTypes = BarcodeEncoding.EAN13 | BarcodeEncoding.UPCA | BarcodeEncoding.UPCE,

    // Layer 2: Confidence threshold — reject marginal decodes
    ConfidenceThreshold = 0.8,

    // Layer 3: False-positive removal — double-scan verification
    RemoveFalsePositive = true,

    // Performance tuning
    Speed = ReadingSpeed.Balanced,
    ExpectMultipleBarcodes = false,
    MinScanLines = 3
};

string[] scanFiles = Directory.GetFiles("pos-scans/", "*.png");

foreach (string file in scanFiles)
{
    BarcodeResults results = BarcodeReader.Read(file, options);

    if (results.Count == 0)
    {
        // No barcode passed all validation layers
        Console.Error.WriteLine($"REJECT {Path.GetFileName(file)}: "
            + "no valid EAN-13/UPC barcode (checksum, confidence, or format mismatch)");
        continue;
    }

    BarcodeResult primary = results.First();

    // Post-read assertion: verify the decoded format matches expectations
    if (primary.BarcodeType != BarcodeEncoding.EAN13
        && primary.BarcodeType != BarcodeEncoding.UPCA
        && primary.BarcodeType != BarcodeEncoding.UPCE)
    {
        Console.Error.WriteLine($"UNEXPECTED FORMAT {Path.GetFileName(file)}: "
            + $"got {primary.BarcodeType}, expected EAN-13/UPC");
        continue;
    }

    Console.WriteLine($"OK {Path.GetFileName(file)}: "
        + $"[{primary.BarcodeType}] {primary.値} — {primary.Confidence}%");
}
//:path=/static-assets/barcode/content-code-examples/how-to/checksum-and-format-validation/combined-validation.cs
using IronBarCode;

// Production configuration: retail POS scanning with EAN-13 and UPC-A
var options = new BarcodeReaderOptions
{
    // Layer 1: Format constraint — only retail symbologies
    ExpectBarcodeTypes = BarcodeEncoding.EAN13 | BarcodeEncoding.UPCA | BarcodeEncoding.UPCE,

    // Layer 2: Confidence threshold — reject marginal decodes
    ConfidenceThreshold = 0.8,

    // Layer 3: False-positive removal — double-scan verification
    RemoveFalsePositive = true,

    // Performance tuning
    Speed = ReadingSpeed.Balanced,
    ExpectMultipleBarcodes = false,
    MinScanLines = 3
};

string[] scanFiles = Directory.GetFiles("pos-scans/", "*.png");

foreach (string file in scanFiles)
{
    BarcodeResults results = BarcodeReader.Read(file, options);

    if (results.Count == 0)
    {
        // No barcode passed all validation layers
        Console.Error.WriteLine($"REJECT {Path.GetFileName(file)}: "
            + "no valid EAN-13/UPC barcode (checksum, confidence, or format mismatch)");
        continue;
    }

    BarcodeResult primary = results.First();

    // Post-read assertion: verify the decoded format matches expectations
    if (primary.BarcodeType != BarcodeEncoding.EAN13
        && primary.BarcodeType != BarcodeEncoding.UPCA
        && primary.BarcodeType != BarcodeEncoding.UPCE)
    {
        Console.Error.WriteLine($"UNEXPECTED FORMAT {Path.GetFileName(file)}: "
            + $"got {primary.BarcodeType}, expected EAN-13/UPC");
        continue;
    }

    Console.WriteLine($"OK {Path.GetFileName(file)}: "
        + $"[{primary.BarcodeType}] {primary.値} — {primary.Confidence}%");
}
$vbLabelText   $csharpLabel

MinScanLines = 3の設定は1Dバーコードが有効と見なされるために同意するスキャンラインの最小数を引き上げます — デフォルトは2です。この値を増やすことは、ノイズのあるスキャンラインが誤読を引き起こす可能性を減らしますが、細いまたは部分的に破損したバーコードを見逃す可能性があります。 清潔に印刷されたラベルを持つ小売POS環境のためには、3はスループットに影響を与えることなく検証ゲートを引き締める保守的な選択です。

読み取り後のBarcodeTypeアサーションは、多層防御パターンです。 すでにExpectBarcodeTypesがリーダーを制約している一方で、この明示的なチェックは意図を文書化し、将来のライブラリバージョンや設定の揺らぎでエッジケースを捉えます。 このアサーションは実行時にコストがかからず、違反時に明確な診断メッセージを提供します。

さらなる調整のために、Speedプロパティでリーダーが適用する計算労力を制御します。 ReadingSpeed.Fasterは、一部の画像前処理手順をスキップし、清潔に印刷されたラベルに適しています。 ReadingSpeed.ExtremeDetailは、より多くの画像フィルターと回転試行を段階的に適用し、長いスキャン時間のコストで損傷したり薄暗い状態の画像からバーコードを復元できます。


次のステップは何ですか?

この記事では、IronBarcodeの暗黙のチェックサム検証モデル、フォーマット制約付き読取りのためのMinScanLinesを品質ゲート層として使用する複合検証パターンをカバーしました。

さらに読みたい場合は、以下のリソースを参照してください:

無料トライアルライセンスを入手してライブ環境での全機能をテストするか、パイプラインが生産の準備ができているときにライセンスオプションを表示します。

よくある質問

バーコードチェックサムバリデーションとは何ですか?

バーコードチェックサムバリデーションとは、計算されたチェックサムをバーコード内にエンコードされた値と照合することでバーコードデータの正確性を保証するプロセスです。これはスキャンプロセス中のエラーを検出するのに役立ちます。

IronBarcodeはチェックサムバリデーションをどのように処理しますか?

IronBarcodeはバーコードデータのチェックサムを計算し、それをエンコードされたチェックサムと照合してデータ整合性を保証することで、スキャンプロセス中に暗黙的にチェックサムバリデーションを処理します。

BarcodeEncodingフィルターとは何ですか?

IronBarcodeのBarcodeEncodingフィルターは、特定のバーコード形式をスキャン中に読み取るか無視するかを指定でき、特定のバーコード形式に焦点を当てることで、より正確で効率的なバーコード処理を可能にします。

IronBarcodeは複合チェックを実行できますか?

はい、IronBarcodeはチェックサムとフォーマットの両方をスキャンプロセス中にチェックすることで、複合チェックを実行し、有効かつ正しくフォーマットされたバーコードのみを処理することができます。

IronBarcodeを使用してC#でバーコードの読み取りをフォーマットによって制約することは可能ですか?

はい、IronBarcodeを使用すると、含めたいまたは除外したいフォーマットを指定して、アプリケーションで関連性のあるバーコードタイプのみを処理することができます。

バーコード処理において形式認識読み取りが重要な理由は何ですか?

形式認識読み取りは、アプリケーションが特定のタイプのバーコードのみを処理できるため、無関係またはサポートされないバーコード形式を無視することで速度と精度を向上させます。

IronBarcodeで形式認識読み取りを実装するにはどうすればよいですか?

IronBarcodeで形式認識読み取りを実装するには、BarcodeEncodingフィルターを使用して読み取りたいバーコード形式を指定します。これはライブラリのAPIを通じて実行され、バーコードスキャンの要件に対する正確な制御を可能にします。

IronBarcodeを使用したバーコードバリデーションの利点は何ですか?

IronBarcodeは、強力なチェックサム検証、形式認識読み取り、幅広いバーコード規格に対応する能力を提供しており、バーコード処理において高い正確性と柔軟性を保証します。

Darrius Serrant
フルスタックソフトウェアエンジニア(WebOps)

Darrius Serrantは、マイアミ大学でコンピュータサイエンスの学士号を取得し、Iron SoftwareでフルスタックWebOpsマーケティングエンジニアとして働いています。若い頃からコーディングに惹かれ、コンピューティングを神秘的かつアクセス可能なものとし、創造性と問題解決のための完璧な媒体と考えていました。

Iron Softwareでは、新しいものを創造することと、複雑なコンセプトをより理解しやすくすることを楽しんでいます。Resident Developerの一人として、次世代に専門知識を共有するために、学生を教えることにも志願しました。

Darriusにとって、その仕事は価値があり、実際の影響があるため、満足感があります。

準備はできましたか?
Nuget ダウンロード 2,108,094 | バージョン: 2026.3 リリース
Still Scrolling Icon

まだスクロールしていますか?

すぐに証拠が欲しいですか? PM > Install-Package BarCode
サンプルを実行する 文字列が BarCode になるのを見る。