バーコードのチェックサムを検証し、C#でフォーマット対応の読み取りを使用する方法
バーコードのチェックサムは、置換エラーの検出に役立ちます。 例えば、EAN-13ラベルの数字が1桁入れ替わっているだけで、荷物が間違った倉庫に送られてしまう可能性があります。 フォーマット認識型読み取りは、デコーダーを想定されるシンボル体系に制限することで、追加の検証レイヤーを提供します。 この手法は、不要なフォーマット検出器をスキップすることで、バックグラウンドノイズによる誤検出を減らし、スキャン時間を短縮します。
IronBarcodeは、デコード時にチェックサム検証を自動的に実行します。 各シンボル体系のチェックデジットアルゴリズムはデフォルトで実行され、検証に失敗したバーコードは結果が返される前に破棄されます。 BarcodeReaderOptions.ExpectBarcodeTypes プロパティは読み取りを特定のフォーマットに制限し、RemoveFalsePositive は曖昧な読み取りのために追加のスキャンを実行します。
このガイドでは、バーコードのチェックサムを検証し、読み取りを想定されるフォーマットに制約し、BarcodeReaderOptions を使用して両方の手法を多層的な品質検証に統合する方法を説明します。
BarcodeReaderOptions を ExpectBarcodeTypes と RemoveFalsePositive で構成し、想定されるシンボロジに読み取りを制約し、自動チェックサム検証を行います。
using IronBarCode;
// Format-constrained read with false-positive removal.
// Limit the decoder to EAN-13 and Code128; checksums are
// validated automatically and failures are silently discarded.
var options = new BarcodeReaderOptions
{
ExpectBarcodeTypes = BarcodeEncoding.EAN13 | BarcodeEncoding.Code128,
RemoveFalsePositive = true,
Speed = ReadingSpeed.Balanced
};
BarcodeResults results = BarcodeReader.Read("label.png", options);Imports IronBarCode
' Format-constrained read with false-positive removal.
' Limit the decoder to EAN-13 and Code128; checksums are
' validated automatically and failures are silently discarded.
Dim options As New BarcodeReaderOptions With {
.ExpectBarcodeTypes = BarcodeEncoding.EAN13 Or BarcodeEncoding.Code128,
.RemoveFalsePositive = True,
.Speed = ReadingSpeed.Balanced
}
Dim results As BarcodeResults = BarcodeReader.Read("label.png", options)Imports IronBarCode
' Format-constrained read with false-positive removal.
' Limit the decoder to EAN-13 and Code128; checksums are
' validated automatically and failures are silently discarded.
Dim options As New BarcodeReaderOptions With {
.ExpectBarcodeTypes = BarcodeEncoding.EAN13 Or BarcodeEncoding.Code128,
.RemoveFalsePositive = True,
.Speed = ReadingSpeed.Balanced
}
Dim results As BarcodeResults = BarcodeReader.Read("label.png", options)最小限のワークフロー(5ステップ)
- NuGetからIronBarcodeライブラリをダウンロードします
BarcodeReaderOptionsインスタンスを作成しますExpectBarcodeTypesをパイプラインに存在するシンボル体系に設定しますRemoveFalsePositiveを有効にして追加検証を行いますBarcodeReader.Readを呼び出してデコードします。チェックサムはデコード中に自動的に検証されます。
バーコードのチェックサムを検証する方法は?
IronBarcodeは、各シンボル体系の仕様に従って、デコード中にチェックサムを検証します。 例えば、EAN-13バーコードを読み取る場合、Mod10チェックデジットは最初の12桁から計算され、13桁目と比較されます。 数字が一致しない場合、バーコードは通知なく拒否され、BarcodeResults コレクションには表示されません。 この方法は、UPC-A、UPC-E、EAN-8、Code128、ITFなど、チェックデジットが必須となるすべてのフォーマットに適用されます。
この暗黙のモデルは、明示的な切り替えを公開するライブラリと異なります。 以下の表は、2つのアプローチを比較しています:
| 比較項目 | IronBarcode | Aspose.BarCode |
|---|---|---|
| 検証トリガー | 自動; デコード時に毎回実行されます | 明示的:ChecksumValidation.On / Off / Default |
| 開発者に必要な操作 | なし。無効なバーコードは結果から除外されます。 | 読み取り前にBarcodeSettings.ChecksumValidationを設定してください。 |
| チェックサム無効化 | 公開されていません。必須フォーマットでは常にチェックサムが適用されます。 | 可能です。ChecksumValidation.Offで検証をスキップします。 |
| オプションのチェックサムフォーマット(Code39) | Confidence + RemoveFalsePositiveを使用して低品質の読み取り結果をフィルタリングします。 | EnableChecksum.はいで明示的に有効にします |
| 失敗時の動作 | バーコードは結果から通知なく除外されます | バーコードには、手動検査用のチェックサム値が別に表示される場合があります。 |
Code39のようなオプションのチェックサムを持つシンボロジの場合、ライブラリはチェックサムの切り替えではなく、信頼度スコアリングとRemoveFalsePositiveを使用します。
入力
Code128倉庫ラックラベル(成功経路)とバーコードのない空白画像(失敗経路)。

warehouse-rack.png(成功パス)

blank-no-barcode.png(失敗パス - バーコードなし)
using IronBarCode;
// Constrain reads to 1D formats and enable secondary verification.
// ConfidenceThreshold rejects decodes where the ML detector falls below 85%,
// acting as a quality gate for optional-checksum symbologies like Code39.
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)
{
// Each result has passed checksum validation (mandatory formats)
// and the 85% confidence threshold, so no additional filtering is needed.
Console.WriteLine($"[{result.BarcodeType}] {result.Value} page={result.PageNumber}");
}
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");
}using IronBarCode;
// Constrain reads to 1D formats and enable secondary verification.
// ConfidenceThreshold rejects decodes where the ML detector falls below 85%,
// acting as a quality gate for optional-checksum symbologies like Code39.
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)
{
// Each result has passed checksum validation (mandatory formats)
// and the 85% confidence threshold, so no additional filtering is needed.
Console.WriteLine($"[{result.BarcodeType}] {result.Value} page={result.PageNumber}");
}
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");
}Imports IronBarCode
' Constrain reads to 1D formats and enable secondary verification.
' ConfidenceThreshold rejects decodes where the ML detector falls below 85%,
' acting as a quality gate for optional-checksum symbologies like Code39.
Dim options As New BarcodeReaderOptions With {
.ExpectBarcodeTypes = BarcodeEncoding.AllOneDimensional,
.RemoveFalsePositive = True,
.ConfidenceThreshold = 0.85,
.Speed = ReadingSpeed.Detailed
}
Dim results As BarcodeResults = BarcodeReader.Read("warehouse-rack.png", options)
For Each result As BarcodeResult In results
' Each result has passed checksum validation (mandatory formats)
' and the 85% confidence threshold, so no additional filtering is needed.
Console.WriteLine($"[{result.BarcodeType}] {result.Value} page={result.PageNumber}")
Next
If results.Count = 0 Then
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")
End IfImports IronBarCode
' Constrain reads to 1D formats and enable secondary verification.
' ConfidenceThreshold rejects decodes where the ML detector falls below 85%,
' acting as a quality gate for optional-checksum symbologies like Code39.
Dim options As New BarcodeReaderOptions With {
.ExpectBarcodeTypes = BarcodeEncoding.AllOneDimensional,
.RemoveFalsePositive = True,
.ConfidenceThreshold = 0.85,
.Speed = ReadingSpeed.Detailed
}
Dim results As BarcodeResults = BarcodeReader.Read("warehouse-rack.png", options)
For Each result As BarcodeResult In results
' Each result has passed checksum validation (mandatory formats)
' and the 85% confidence threshold, so no additional filtering is needed.
Console.WriteLine($"[{result.BarcodeType}] {result.Value} page={result.PageNumber}")
Next
If results.Count = 0 Then
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")
End If出力
成功パス

倉庫ラックのバーコードはページ0でRACK-A1-LOT-7382として読み取られました。信頼度がしきい値の85%を超え、チェックサム検証を通過したので、BarcodeResultsに表示されます。
失敗パス

ConfidenceThreshold をデフォルトの0.7より大きい値に引き上げると、Code39のようなオプションのチェックサムを持つシンボロジに対して検証基準がさらに厳しくなります。
チェックサム検証が完了したら、次のステップは、パイプラインが想定するバーコード形式にリーダーを制限することです。
フォーマット対応のバーコード読み取りを使用する方法は?
BarcodeEncoding 列挙型はフラグタイプであり、ビット単位のOR演算子を使用して複数のフォーマットを組み合わせることができます。 ExpectBarcodeTypes を設定すると、リーダーはそのフォーマットに限定され、他のフォーマットの検出をスキップします。
| 値 | カテゴリ | 説明 | チェックサム |
|---|---|---|---|
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 |
速度以外にも、フォーマットセットを制限することは検証ゲートとして機能します。リストにないシンボル体系のバーコードは、画像内に物理的に存在していても、結果から除外されます。
入力
Code128の配送ラベル(成功経路)と、Code128のみという制約に一致しないQRコード(失敗経路)。

shipping-label.png(成功パス - Code128の制約に一致)

qr-format-mismatch.png(失敗パス - QRがCode128のみのフィルターで拒否されました)
using IronBarCode;
// Constrained read: only Code128 barcodes are returned.
// Faster because the reader skips all other format detectors.
var constrainedOptions = new BarcodeReaderOptions
{
ExpectBarcodeTypes = BarcodeEncoding.Code128,
Speed = ReadingSpeed.Faster,
ExpectMultipleBarcodes = false
};
// Broad read: all supported formats are scanned.
// Useful for verification or when the image format is unknown.
var broadOptions = new BarcodeReaderOptions
{
ExpectBarcodeTypes = BarcodeEncoding.All,
Speed = ReadingSpeed.Detailed,
ExpectMultipleBarcodes = true
};
string imagePath = "shipping-label.png";
BarcodeResults constrained = BarcodeReader.Read(imagePath, constrainedOptions);
Console.WriteLine($"Constrained: {constrained.Count} Code128 barcode(s) found");
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.Value}");
}using IronBarCode;
// Constrained read: only Code128 barcodes are returned.
// Faster because the reader skips all other format detectors.
var constrainedOptions = new BarcodeReaderOptions
{
ExpectBarcodeTypes = BarcodeEncoding.Code128,
Speed = ReadingSpeed.Faster,
ExpectMultipleBarcodes = false
};
// Broad read: all supported formats are scanned.
// Useful for verification or when the image format is unknown.
var broadOptions = new BarcodeReaderOptions
{
ExpectBarcodeTypes = BarcodeEncoding.All,
Speed = ReadingSpeed.Detailed,
ExpectMultipleBarcodes = true
};
string imagePath = "shipping-label.png";
BarcodeResults constrained = BarcodeReader.Read(imagePath, constrainedOptions);
Console.WriteLine($"Constrained: {constrained.Count} Code128 barcode(s) found");
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.Value}");
}Imports IronBarCode
' Constrained read: only Code128 barcodes are returned.
' Faster because the reader skips all other format detectors.
Dim constrainedOptions As New BarcodeReaderOptions With {
.ExpectBarcodeTypes = BarcodeEncoding.Code128,
.Speed = ReadingSpeed.Faster,
.ExpectMultipleBarcodes = False
}
' Broad read: all supported formats are scanned.
' Useful for verification or when the image format is unknown.
Dim broadOptions As New BarcodeReaderOptions With {
.ExpectBarcodeTypes = BarcodeEncoding.All,
.Speed = ReadingSpeed.Detailed,
.ExpectMultipleBarcodes = True
}
Dim imagePath As String = "shipping-label.png"
Dim constrained As BarcodeResults = BarcodeReader.Read(imagePath, constrainedOptions)
Console.WriteLine($"Constrained: {constrained.Count} Code128 barcode(s) found")
Dim broad As BarcodeResults = BarcodeReader.Read(imagePath, broadOptions)
Console.WriteLine($"Broad: {broad.Count} barcode(s) found across all formats")
For Each result As BarcodeResult In broad
Console.WriteLine($" [{result.BarcodeType}] {result.Value}")
NextImports IronBarCode
' Constrained read: only Code128 barcodes are returned.
' Faster because the reader skips all other format detectors.
Dim constrainedOptions As New BarcodeReaderOptions With {
.ExpectBarcodeTypes = BarcodeEncoding.Code128,
.Speed = ReadingSpeed.Faster,
.ExpectMultipleBarcodes = False
}
' Broad read: all supported formats are scanned.
' Useful for verification or when the image format is unknown.
Dim broadOptions As New BarcodeReaderOptions With {
.ExpectBarcodeTypes = BarcodeEncoding.All,
.Speed = ReadingSpeed.Detailed,
.ExpectMultipleBarcodes = True
}
Dim imagePath As String = "shipping-label.png"
Dim constrained As BarcodeResults = BarcodeReader.Read(imagePath, constrainedOptions)
Console.WriteLine($"Constrained: {constrained.Count} Code128 barcode(s) found")
Dim broad As BarcodeResults = BarcodeReader.Read(imagePath, broadOptions)
Console.WriteLine($"Broad: {broad.Count} barcode(s) found across all formats")
For Each result As BarcodeResult In broad
Console.WriteLine($" [{result.BarcodeType}] {result.Value}")
Next出力
成功パス

配送ラベルにはSHIP-2024-00438の値があります。 制約付き読み取りでは、Code128がフィルターで指定されたフォーマットであるため、すぐに検出されます。また、広範囲読み取りでも、全フォーマットを対象にしても同じ結果が確認されます。
失敗パス

制約付き読み取りの結果が空の場合、それはエラーではなく検証シグナルです。 不一致点を記録して、レビューのために保管してください。
バーコードの種類が混在するパイプライン(例:EAN-13製品コードとCode128追跡番号の両方が記載された梱包明細書)の場合は、想定されるフォーマットを組み合わせます。
using IronBarCode;
// Combine multiple format flags with | to scan for more than one symbology
// in a single pass. Each BarcodeResult.BarcodeType identifies which format
// was decoded, enabling downstream routing logic per symbology.
var options = new BarcodeReaderOptions
{
ExpectBarcodeTypes = BarcodeEncoding.EAN13 | BarcodeEncoding.Code128,
ExpectMultipleBarcodes = true
};using IronBarCode;
// Combine multiple format flags with | to scan for more than one symbology
// in a single pass. Each BarcodeResult.BarcodeType identifies which format
// was decoded, enabling downstream routing logic per symbology.
var options = new BarcodeReaderOptions
{
ExpectBarcodeTypes = BarcodeEncoding.EAN13 | BarcodeEncoding.Code128,
ExpectMultipleBarcodes = true
};Imports IronBarCode
' Combine multiple format flags with Or to scan for more than one symbology
' in a single pass. Each BarcodeResult.BarcodeType identifies which format
' was decoded, enabling downstream routing logic per symbology.
Dim options As New BarcodeReaderOptions With {
.ExpectBarcodeTypes = BarcodeEncoding.EAN13 Or BarcodeEncoding.Code128,
.ExpectMultipleBarcodes = True
}Imports IronBarCode
' Combine multiple format flags with Or to scan for more than one symbology
' in a single pass. Each BarcodeResult.BarcodeType identifies which format
' was decoded, enabling downstream routing logic per symbology.
Dim options As New BarcodeReaderOptions With {
.ExpectBarcodeTypes = BarcodeEncoding.EAN13 Or BarcodeEncoding.Code128,
.ExpectMultipleBarcodes = True
}返されたBarcodeResult.BarcodeTypeはどのフォーマットがデコードされたかを識別し、下流でのルーティングを可能にします。
どのシンボロジーがチェックサム検証をサポートしているか?
すべてのバーコードフォーマットが同じ方法でチェックサムを使用しているわけではありません。 以下の表は一般的なシンボロジーとそのエラー検出特性をマッピングし、各フォーマットごとにどの程度厳しくConfidenceThresholdおよびRemoveFalsePositiveを設定するかを示します。
| シンボロジー | チェックサムタイプ | 必須? | 推奨事項 |
|---|---|---|---|
| EAN-13 / EAN-8 | Mod10 | はい | デフォルト設定で十分です。チェックサムは常に適用されます。 |
| UPC-A / UPC-E | Mod10 | はい | デフォルト設定で十分です。書き込み時にチェックデジットが自動修正されます。 |
| Code128 | 重み付けMod103 | はい | デフォルト設定で十分です。仕様上必須です。 |
| Code39 | Mod43 | オプション | ConfidenceThresholdを0.8以上に引き上げ、RemoveFalsePositiveを有効にします |
| コーダバー | Mod16 | オプション | Code39と同様。品質検証に信頼度を使用します。 |
| ITF | Mod10 | オプション | インタリーブフォーマットにRemoveFalsePositiveを有効にします |
| QRコード / データマトリックス | リード・ソロモンECC | 常に必須 | 構造に組み込まれたエラー訂正です。追加の設定は不要です。 |
| PDF417 | リード・ソロモンECC | 常に必須 | QR/DataMatrixと同様。エラー訂正機能が内蔵されています。 |
QRコード、DataMatrix、PDF417などの2次元シンボル体系では、エラー訂正はエンコード構造に組み込まれています。 これらのフォーマットは、単純なチェックデジットに頼ることなく、部分的な損傷から復旧できます。 ConfidenceThresholdは、ML検出フェーズで適用され続け、デコードステップはシンボロジの内蔵冗長性から恩恵を受けます。
両方の手法が理解できたところで、それらを組み合わせて、本番環境ですぐに使える単一の検証パターンを作成します。
チェックサムとフォーマット制約をどのように組み合わせるか?
プロダクション対応パターンは、ExpectBarcodeTypes, RemoveFalsePositive, ConfidenceThreshold, Speedを一つのBarcodeReaderOptionsオブジェクトで設定します。 これらが組み合わさって多層的な検証を行います。フォーマット制約によって検索空間が絞り込まれ、チェックサム検証によってデータの整合性が確保され、信頼度閾値によって境界値のデコードがフィルタリングされ、誤検出の除去によって二次的な検証処理が追加されます。
入力
成功パスとして使用されるpos-scans/ディレクトリからの3つのPOSスキャンバーコード: 2つのEAN-13と1つのUPC-A。 Code128の倉庫棚ラベルは失敗パスとして使われます - EAN-13/UPC-Aの制約により拒否され、REJECT行がログされます。

pos-scan-1.png(成功)

pos-scan-2.png(成功)

pos-scan-3.png(成功)

warehouse-rack.png(失敗 - Code128拒否)
using IronBarCode;
using System;
using System.IO;
using System.Linq;
// Layered validation for retail POS: EAN-13, UPC-A, and UPC-E only.
// Each property adds a distinct filter to the read pipeline.
var options = new BarcodeReaderOptions
{
// Layer 1: format constraint, accept only retail symbologies
ExpectBarcodeTypes = BarcodeEncoding.EAN13 | BarcodeEncoding.UPCA | BarcodeEncoding.UPCE,
// Layer 2: confidence threshold, reject decodes below 80%
ConfidenceThreshold = 0.8,
// Layer 3: false-positive removal, runs a secondary verification pass
RemoveFalsePositive = true,
Speed = ReadingSpeed.Balanced,
ExpectMultipleBarcodes = false,
// Require 3 agreeing scan lines to reduce phantom reads from noisy images
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.
// ExpectBarcodeTypes already constrains the reader; this check documents
// intent and surfaces unexpected results during future changes.
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.Value}");
}using IronBarCode;
using System;
using System.IO;
using System.Linq;
// Layered validation for retail POS: EAN-13, UPC-A, and UPC-E only.
// Each property adds a distinct filter to the read pipeline.
var options = new BarcodeReaderOptions
{
// Layer 1: format constraint, accept only retail symbologies
ExpectBarcodeTypes = BarcodeEncoding.EAN13 | BarcodeEncoding.UPCA | BarcodeEncoding.UPCE,
// Layer 2: confidence threshold, reject decodes below 80%
ConfidenceThreshold = 0.8,
// Layer 3: false-positive removal, runs a secondary verification pass
RemoveFalsePositive = true,
Speed = ReadingSpeed.Balanced,
ExpectMultipleBarcodes = false,
// Require 3 agreeing scan lines to reduce phantom reads from noisy images
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.
// ExpectBarcodeTypes already constrains the reader; this check documents
// intent and surfaces unexpected results during future changes.
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.Value}");
}Imports IronBarCode
Imports System
Imports System.IO
Imports System.Linq
' Layered validation for retail POS: EAN-13, UPC-A, and UPC-E only.
' Each property adds a distinct filter to the read pipeline.
Dim options As New BarcodeReaderOptions With {
.ExpectBarcodeTypes = BarcodeEncoding.EAN13 Or BarcodeEncoding.UPCA Or BarcodeEncoding.UPCE,
.ConfidenceThreshold = 0.8,
.RemoveFalsePositive = True,
.Speed = ReadingSpeed.Balanced,
.ExpectMultipleBarcodes = False,
.MinScanLines = 3
}
Dim scanFiles As String() = Directory.GetFiles("pos-scans/", "*.png")
For Each file As String In scanFiles
Dim results As BarcodeResults = BarcodeReader.Read(file, options)
If results.Count = 0 Then
Console.Error.WriteLine($"REJECT {Path.GetFileName(file)}: no valid EAN-13/UPC barcode (checksum, confidence, or format mismatch)")
Continue For
End If
Dim primary As BarcodeResult = results.First()
If primary.BarcodeType <> BarcodeEncoding.EAN13 AndAlso primary.BarcodeType <> BarcodeEncoding.UPCA AndAlso primary.BarcodeType <> BarcodeEncoding.UPCE Then
Console.Error.WriteLine($"UNEXPECTED FORMAT {Path.GetFileName(file)}: got {primary.BarcodeType}, expected EAN-13/UPC")
Continue For
End If
Console.WriteLine($"OK {Path.GetFileName(file)}: [{primary.BarcodeType}] {primary.Value}")
NextImports IronBarCode
Imports System
Imports System.IO
Imports System.Linq
' Layered validation for retail POS: EAN-13, UPC-A, and UPC-E only.
' Each property adds a distinct filter to the read pipeline.
Dim options As New BarcodeReaderOptions With {
.ExpectBarcodeTypes = BarcodeEncoding.EAN13 Or BarcodeEncoding.UPCA Or BarcodeEncoding.UPCE,
.ConfidenceThreshold = 0.8,
.RemoveFalsePositive = True,
.Speed = ReadingSpeed.Balanced,
.ExpectMultipleBarcodes = False,
.MinScanLines = 3
}
Dim scanFiles As String() = Directory.GetFiles("pos-scans/", "*.png")
For Each file As String In scanFiles
Dim results As BarcodeResults = BarcodeReader.Read(file, options)
If results.Count = 0 Then
Console.Error.WriteLine($"REJECT {Path.GetFileName(file)}: no valid EAN-13/UPC barcode (checksum, confidence, or format mismatch)")
Continue For
End If
Dim primary As BarcodeResult = results.First()
If primary.BarcodeType <> BarcodeEncoding.EAN13 AndAlso primary.BarcodeType <> BarcodeEncoding.UPCA AndAlso primary.BarcodeType <> BarcodeEncoding.UPCE Then
Console.Error.WriteLine($"UNEXPECTED FORMAT {Path.GetFileName(file)}: got {primary.BarcodeType}, expected EAN-13/UPC")
Continue For
End If
Console.WriteLine($"OK {Path.GetFileName(file)}: [{primary.BarcodeType}] {primary.Value}")
Next出力
成功パス

3枚のPOSスキャン画像はすべて無事に通過しました。 リーダーは5901234123471, 4006381333931, 012345678905の値を返しました。 それぞれがEAN13 | UPCA | UPCEフィルターに一致し、有効なMod10チェックサムを持ち、信頼度が0.8を超えていました。
失敗パス

MinScanLinesを3に設定すると、1Dバーコードが有効であると見なすために必要な一致するスキャンラインの最小数が増えます; デフォルト値は2です。この値を上げると、ノイズの多いスキャンラインからの誤読み取りのリスクは低下しますが、細いバーコードや部分的に損傷したバーコードが読み取れなくなる可能性があります。 印刷ラベルがきれいな小売店のPOS環境では、値3は処理能力に影響を与えることなく検証を強化する保守的な選択肢です。
読み取り後のBarcodeTypeアサーションは多層防御の役割を果たします。ExpectBarcodeTypesはすでにフィルタリングしていますが、明示的なチェックは意図を文書化し、ランタイムコストなしで設定ドリフトを検出します。速度調整には、ReadingSpeed.Fasterが鮮明な機械印刷ラベルに適しています。 DetailedとExtremeDetailは、より長いスキャン時間の代償を払って、損傷したり、照明が不十分なバーコードを回復します。
次のステップは何ですか?
この記事では、IronBarcodeの暗黙のチェックサム検証モデル、フォーマット制約付き読み取りのためのBarcodeEncodingフラグ列挙、およびExpectBarcodeTypes, ConfidenceThreshold, RemoveFalsePositive, MinScanLinesを使用した統合検証パターンについて説明しました。
さらなる学習のために、以下のリソースを参照してください:
- IronBarcodeチュートリアル - バーコードの読み取り 全プロセスのウォークスルー。
- 誤検知防止
RemoveFalsePositiveメカニズムの詳細。 - 信頼度しきい値の例 MLベースの検出調整。
- 出力データフォーマット
BarcodeResultプロパティの参照。 - 画像補正の手引き デコード精度を向上させるフィルター。
- BarcodeReaderOptions API リファレンス 完全な構成ドキュメント。
- BarcodeEncoding APIリファレンス サポートされているシンボロジの完全なリスト。
無料トライアルライセンスを取得すると、実際の環境ですべての機能をテストできます。パイプラインを本番環境に導入する準備が整ったら、ライセンスオプションを表示するから確認してください。
よくある質問
バーコードチェックサムを検証する目的は何ですか?
バーコードチェックサムの検証は、バーコードスキャン中にデータが破損しないことを保証します。IronBarcode は、デコード時に各シンボロジーのチェックディジットアルゴリズムに対して自動的にチェックサム検証を行います。
IronBarcode はどのようにフォーマット対応の読み取りを処理しますか?
IronBarcode は BarcodeReaderOptions.ExpectBarcodeTypes プロパティを使用して特定のバーコードシンボロジーへの読み取りを制限します。これにより、フォーマット検出器を想定されるタイプのみに限定し、誤認識を減らしスキャンの効率を改善します。
IronBarcode は自動チェックサム検証を行えますか?
はい、IronBarcode はデコードプロセス中に自動チェックサム検証を行います。この検証に失敗したバーコードは、結果が返される前に破棄されます。
IronBarcode の `RemoveFalsePositive` 機能の使用は何ですか?
IronBarcode の RemoveFalsePositive 機能は、曖昧な読み取りを排除するための二次スキャン検証を追加し、バーコード検出の精度を向上させます。
IronBarcode で必須のチェックサム検証をサポートするシンボロジーはどれですか?
EAN-13、UPC-A、Code128 などのシンボロジーが必須のチェックサム検証をサポートします。IronBarcode はこれらのフォーマットに対して自動的にチェックサムチェックを強制します。
IronBarcode でバーコード読み取りフォーマットに制約を設定する方法は?
IronBarcode の ExpectBarcodeTypes プロパティを使用してバーコード読み取りフォーマットに制約を設定できます。これにより、IronBarcode がデコードすべきバーコードの種類を指定し制限できます。
バーコード読み取りでフォーマット制約を使用する利点は何ですか?
フォーマット制約は、不要なノイズを避け、スキャン速度を向上させるために、特定のシンボロジーに検索を限定することで、スキャンエラーを減らすのに役立ちます。
Code39 などのオプションのチェックサムフォーマットのバーコードを IronBarcode はどのように処理しますか?
Code39 などのオプションのチェックサムフォーマットに対して、IronBarcode は信頼度スコアリングと RemoveFalsePositive 機能の組み合わせを使用して低品質の読み取りをフィルタリングします。
IronBarcode の ConfidenceThreshold プロパティは何をしますか?
IronBarcode の ConfidenceThreshold プロパティは、デコードの品質ゲートを設定し、機械学習検出器の信頼度が指定されたパーセンテージを下回る場合にはバーコードを拒否します。
IronBarcode はどのようにチェックサム検証とフォーマット制約を組み合わせますか?
IronBarcode は BarcodeReaderOptions を使用してチェックサム検証とフォーマット制約を組み合わせます。ExpectBarcodeTypes、ConfidenceThreshold、および RemoveFalsePositive などのプロパティを設定することで、階層化された検証アプローチを作成します。
Curtis Chauは、カールトン大学でコンピュータサイエンスの学士号を取得し、Node.js、TypeScript、JavaScript、およびReactに精通したフロントエンド開発を専門としています。直感的で美しいユーザーインターフェースを作成することに情熱を持ち、Curtisは現代のフレームワークを用いた開発や、構造の良い視覚的に魅力的なマニュアルの作成を楽しんでいます。