如何验证条形码校验和并在C#中使用格式感知读取
条形码校验和有助于检测替换错误。 例如,EAN-13 标签中一个数字颠倒就可能导致包裹被送到错误的仓库。 格式感知读取通过将解码器限制为预期的符号体系,提供了额外的验证层。 这种方法通过跳过不必要的格式检测器来减少背景噪声造成的误报并缩短扫描时间。
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 等。
这种隐式模型不同于那些公开显式开关的库。 下表比较了这两种方法:
| 方面 | IronBarcode | Aspose.BarCode |
|---|---|---|
| 验证触发 | 自动运行;每次解码期间运行 | 显式: ChecksumValidation.On / Off / Default |
| 需要开发者操作 | 无;无效条形码已从结果中排除。 | 读取条形码前,请设置BarcodeSettings.ChecksumValidation |
| 校验和禁用 | 未公开;强制格式始终强制执行校验和。 | 是的; ChecksumValidation.Off会跳过验证 |
| 可选校验和格式(Code39) | 使用Confidence + RemoveFalsePositive来过滤低质量读取数据 | 显式启用EnableChecksum.Yes |
| 故障行为 | 结果中静默遗漏条码 | 条形码可能带有单独的校验和值,以便人工检查。 |
对于可选校验和的符号,如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是一个标志类型,允许使用按位或运算符组合多种格式。 设置ExpectBarcodeTypes将读取器限制为这些格式,并跳过其他格式的检测。
| 值 | 类别 | 说明 | 校验和 |
|---|---|---|---|
BarcodeEncoding.All | 元类别 | 所有支持的格式(默认行为) | 取决于格式 |
BarcodeEncoding.AllOneDimensional | 元类别 | 所有线性(一维)格式包括堆叠 | 取决于格式 |
BarcodeEncoding.AllTwoDimensional | 元类别 | 所有矩阵/网格(2D)格式 | 取决于格式 |
BarcodeEncoding.Code128 | 一维 | 高密度字母数字字符集(物流、运输) | 强制(加权Mod103) |
BarcodeEncoding.EAN13 | 一维 | 零售产品识别码,13 位数字 | 强制(Mod10) |
BarcodeEncoding.QRCode | 2D | 高容量矩阵(URL、结构化数据) | 里德-所罗门 ECC |
BarcodeEncoding.Code39 | 一维 | 字母数字(国防、汽车) | 可选(Mod43) |
BarcodeEncoding.UPCA | 一维 | 北美零售,12位数 | 强制(Mod10) |
BarcodeEncoding.DataMatrix | 2D | 紧凑型矩阵(电子、制药) | 里德-所罗门 ECC |
BarcodeEncoding.PDF417 | 2D | 堆叠(身份证、交通卡) | 里德-所罗门 ECC |
除了速度之外,限制格式集还起到验证门的作用:即使条形码实际存在于图像中,任何未列出的符号体系的条形码也会被排除在结果之外。
输入
Code128 运输标签(成功路径)和不符合 Code128 限制的二维码(失败路径)。

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 |
| QRCode / 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。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。