如何在 C# 中驗證 條碼校驗碼並使用格式感知讀取功能
條碼校驗碼有助於偵測替換錯誤。 例如,EAN-13 標籤中一個數字顛倒就可能導致包裹被送到錯誤的倉庫。 格式感知讀取透過將解碼器限制為預期的條碼格式,提供了額外的驗證層。 這種方法透過跳過不必要的格式偵測器來減少背景雜訊造成的誤報並縮短掃描時間。
IronBarcode在解碼過程中會自動執行校驗碼驗證。 每種條碼編碼的檢查碼演算法預設運行,失敗的條碼會在傳回結果之前被丟棄。 BarcodeReaderOptions.ExpectBarcodeTypes 屬性限制讀取特定格式,而 RemoveFalsePositive 增加二次掃描以解決模糊讀取。
本指南說明如何驗證條碼校驗碼,將讀取限制為預期格式,並使用BarcodeReaderOptions將這兩種技術結合到分層品質關卡中。
使用 ExpectBarcodeTypes 和 RemoveFalsePositive 設定 BarcodeReaderOptions,以限制讀取至預期符號並進行自動校驗碼驗證。
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 等。
此隱式模型有別於提供明確切換開關的函式庫。 下表比較了這兩種方法:
| 範疇 | IronBarcode | Aspose.BarCode |
|---|---|---|
| 驗證觸發條件 | 自動運行;每次解碼期間運行 | 顯式: ChecksumValidation.On / Off / Default |
| 開發者需採取的行動 | 無;無效條碼已從結果中排除。 | 讀取條碼前,請設定BarcodeSettings.ChecksumValidation |
| 校驗碼停用 | 未公開;強制格式始終強制執行校驗碼。 | 是的; ChecksumValidation.Off會跳過驗證 |
| 可選校驗碼格式 (Code39) | 使用Confidence + RemoveFalsePositive過濾低品質讀取資料 | 明確啟用EnableChecksum.Yes |
| 失敗行為 | BarCode已從結果中隱去 | 條碼可能帶有單獨的校驗碼值,以便人工檢查。 |
對於具有可選校驗碼的條碼格式,例如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 | 堆疊(身分證、交通卡) | 里德-所羅門 ECC |
除了速度之外,限制格式集還起到驗證門的作用:即使條碼實際存在於影像中,任何未列出的條碼格式的條碼也會被排除在結果之外。
輸入
Code128 運輸標籤(成功路徑)和不符合 Code128 限制的QR Code(失敗路徑)。

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 |
| Codabar | Mod16 | (可選) | 與 Code39 相同;使用信心度作為品質關卡 |
| ITF | Mod10 | (可選) | 針對交錯格式啟用 RemoveFalsePositive |
| QR Code/DataMatrix | 里德-所羅門 ECC | 總是 | 結構性錯誤更正;無需額外設定 |
| PDF417 | 里德-所羅門 ECC | 總是 | 與 QR/DataMatrix 相同;糾錯功能是固有的。 |
對於 QR、DataMatrix 和 PDF417 等二維條碼格式,糾錯功能已整合到編碼結構中。 這些格式無需依賴簡單的檢查碼即可從部分損壞中復原。 在ML檢測階段,ConfidenceThreshold仍然適用,而解碼步驟受益於條碼格式的內建冗餘。
既然我們已經了解了這兩種技術,讓我們將它們合併成一個可用於生產的單一驗證模式。
如何將校驗碼與格式限制結合使用?
可用於正式環境的模式在單一的 BarcodeReaderOptions 物件中設定 ExpectBarcodeTypes, RemoveFalsePositive, ConfidenceThreshold 和 Speed。 它們共同構成了分層驗證機制:格式約束縮小了搜尋空間,校驗碼驗證確保了資料完整性,信心度閾值過濾了可靠性不足的解碼結果,而誤報消除則提供第二次驗證。
輸入
來自pos-scans/目錄的三個POS掃描條碼用作成功路徑:兩個EAN-13和一個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輸出
成功路徑

三張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應該解碼的條碼類型。
使用格式限制進行條碼讀取的好處是什麼?
格式限制有助於通過限制搜尋至特定符號學來減少掃描錯誤,從而避免不必要的干擾,也提高了掃描速度。
IronBarcode如何處理像Code39這樣具有可選校驗和格式的條碼?
對於像Code39這樣的可選校驗和格式,IronBarcode使用信心評分和RemoveFalsePositive功能的組合來篩除低質量的讀取。
IronBarcode中的ConfidenceThreshold屬性有什麼作用?
IronBarcode中的ConfidenceThreshold屬性為解碼設置質量門檻,拒絕機器學習檢測器的置信度低於指定位數的條碼。
IronBarcode如何將校驗和驗證與格式限制結合起來?
IronBarcode使用BarcodeReaderOptions結合校驗和驗證與格式限制。通過設置如ExpectBarcodeTypes、ConfidenceThreshold和RemoveFalsePositive等屬性,形成了一個多層次的驗證方法。
Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。