IRONSOFTWAREHOME

如何在 C# 中验证二维码校验和并应用容错机制

Curtis Chau
Curtis Chau
Updated: 2026年3月6日

处理现实世界输入(包括打印标签、相机拍摄的图像或扫描的文档)的二维码管道会遇到损坏严重而无法解码的符号,以及通过校验和但未通过业务验证的结果。

里德-所罗门纠错技术能够自动解决解码过程中的物理损坏问题。 如果无法恢复某个符号,则结果集合为空而不是部分结果。 应用层验证是独立的,它涉及在进一步处理之前检查解码后的值是否非空、是否符合预期格式或是否包含有效的 URI。

本指南解释了如何用IronQR库验证QR码校验和应用容错检查。

快速入门:验证二维码校验和

读取二维码并检查解码是否成功:非空结果表示里德-所罗门校验和通过。

  1. 1Install IronQR with NuGet Package Manager

    PM > Install-Package IronQR

  2. 2复制并运行这段代码。

    using IronQr;
    using IronSoftware.Drawing;
    
    var reader = new QrReader();
    IEnumerable<QrResult> results = reader.Read(new QrImageInput("label.png"));
    
    if (!results.Any())
    {
        Console.WriteLine("No QR code detected or decoding failed.");
        return;
    }
    
    Console.WriteLine(results.First().Value);
    C#
  3. 3部署到您的生产环境中进行测试

    通过免费试用立即在您的项目中开始使用IronQR
    arrow pointer

验证二维码校验和

二维码采用里德-所罗门纠错码来检测并修复编码数据的损坏。 校正水平(低为 7%,中为 15%,四分位数为 25%,高为 30%)决定了可以丢失但仍能恢复的码字百分比。

在读取方面,校验和验证在解码过程中内部运行。 类QrResult不公开信心属性; 如果集合中存在结果,则校验和通过。 如果解码失败,则集合为空。

输入

一个QR码产品标签编码https://ironsoftware.com/,生成时采用中等错误校正,代表可能在运输过程中被处理或轻微划伤的标签。

二维码编码 https://ironsoftware.com 用作校验和验证的输入
using IronQr;
using IronSoftware.Drawing;

var reader = new QrReader();
IEnumerable<QrResult> results = reader.Read(new QrImageInput(AnyBitmap.FromFile("damaged-label.png")));

// Reed-Solomon decoding is pass/fail — presence in results means valid checksum
if (!results.Any())
{
    // Decoding failed entirely — damage exceeded the error correction capacity
    Console.WriteLine("QR code could not be decoded. Consider re-scanning or using a higher error correction level at generation time.");
    return;
}

foreach (QrResult result in results)
{
    // Decoded successfully — validate the content matches expected format
    if (string.IsNullOrWhiteSpace(result.Value))
    {
        Console.WriteLine("QR decoded but produced an empty value.");
        continue;
    }

    Console.WriteLine($"Valid QR: {result.Value}");
}
C#

输出

控制台显示解码后的值https://ironsoftware.com/,确认里德-所罗门解码成功并且有效负载被完整恢复。

终端输出显示已解码的二维码:https://ironsoftware.com

为了提高抗物理损坏能力,请生成具有更高纠错级别的二维码。 高级版本可以恢复高达 30% 的数据丢失,但代价是符号更大。


处理二维码读取中的格式感知

IronQR支持三种 QR 编码格式:标准 QR、Micro QR 和矩形 Micro QR。 扫描仪在读取过程中会自动检测格式。 扫描后,QrResult.QrType字段提供检测到的格式作为枚举值。

对于PDF,使用QrImageInput。 扫描模式决定了速度与准确性的平衡:OnlyBasicScan完全跳过机器学习以处理高质量的预处理图像。

输入

PNG 产品标签(左)和 JPEG 相机拍摄图像(右),演示了对两种常见输入类型进行格式感知读取的功能。

product-label.png 二维码,用作格式感知读取的输入

产品标签(PNG格式)

camera-capture.jpg JPEG 二维码,模拟相机拍摄

相机拍摄(JPEG)

using IronQr;
using IronSoftware.Drawing;
using IronQr.Enum;

// Read from an image file with ML + classic scan (default)
var reader = new QrReader();
IEnumerable<QrResult> results = reader.Read(new QrImageInput(AnyBitmap.FromFile("product-label.png")));

foreach (QrResult result in results)
{
    // Inspect the detected QR format
    Console.WriteLine($"Format: {result.QrType}");   // QRCode, MicroQRCode, or RMQRCode
    Console.WriteLine($"Value:  {result.Value}");

    // Url is non-null only if Value is a valid URI
    if (result.Url != null)
    {
        Console.WriteLine($"URI:    {result.Url.AbsoluteUri}");
    }

    // Corner coordinates for positional context
    Console.WriteLine($"Corners: {result.Points.Length} points detected");
}

// Read from a bitmap with ML-only mode for faster throughput
var bitmap = AnyBitmap.FromFile("camera-capture.jpg");
var fastResults = reader.Read(new QrImageInput(bitmap, QrScanMode.OnlyDetectionModel));
C#

输出

控制台显示产品标签的检测到的格式、解码值、解析的 URI 和角数,然后显示相机捕获的快速扫描结果计数。

终端输出显示:格式:QRCode,值:https://ironsoftware.com/product,URI,以及检测到的角点:4 个。

当应用程序需要特定格式时,QrType字段很有帮助。 例如,一个只生成标准二维码的仓库系统可以过滤掉意外的微型二维码或矩形微型二维码检测结果,这些结果可能表示噪声或无关标签。 每种格式都有其独特的容量特性:标准 QR 码最多支持 7,089 个数字字符,Micro QR 码最多支持 35 个字符,而矩形 Micro QR 码则采用矩形外形,适用于标签空间有限的情况。


对二维码结果应用空值检查

QrReader.Read如果没有找到QR码则返回空集合; 它从不返回null。 但是,各个结果属性仍需验证。 例如,Url则返回null。

一个完善的验证模式会在将数据传递给另一个系统之前检查三个方面:集合计数、值完整性和类型或 URI 有效性。

输入

一张没有二维码的空白白色图像,代表一批混合文档中的一页,其中一些页面没有机器可读标签。

一张空白的白色图像(不含二维码)用作空值检查演示的输入
using IronQr;
using IronQr.Enum;
using IronSoftware.Drawing;
using System.Collections.Generic;
using System.Linq;

public static class QrValidator
{
    public static List<QrResult> GetValidResults(
        string imagePath,
        QrEncoding? expectedFormat = null)
    {
        var reader = new QrReader();
        IEnumerable<QrResult> results = reader.Read(new QrImageInput(AnyBitmap.FromFile(imagePath)));

        // Guard: no QR codes detected
        if (!results.Any())
            return new List<QrResult>();

        return results
            .Where(r => !string.IsNullOrWhiteSpace(r.Value))
            .Where(r => expectedFormat == null || r.QrType == expectedFormat)
            .ToList();
    }
}

// Usage — only accept standard QR codes with non-empty values
var validated = QrValidator.GetValidResults(
    "shipping-manifest.png",
    expectedFormat: QrEncoding.QRCode);

if (validated.Count == 0)
{
    Console.WriteLine("No valid QR codes found for processing.");
    return;
}

foreach (var qr in validated)
{
    // Safe for downstream: value is non-empty, format is verified
    SendToInventoryApi(qr.Value, qr.Url?.AbsoluteUri);
}
C#

输出

控制台显示验证器的空结果响应:未检测到二维码,因此集合为空,没有数据继续进行下游处理。

终端输出显示:未找到可处理的有效二维码。

验证器返回空列表(从不为null),消除了在调用点进行null检查的需要。可选的expectedFormat参数作为格式门,使调用代码仅接收与预期格式类型匹配的结果。 Url属性使用空条件运算符以安全地处理URI和非URI负载。

对于异步工作流,应用相同的验证模式到ReadAsync:等待调用并对结果集合使用相同的检查。


进一步阅读

-错误纠正级别:写入时恢复能力和纠正级别配置。 -如何读取二维码:输入格式选项和读取模式。

准备生产时,请查看许可选项

点击这里下载完整的 ChecksumFaultToleranceTest 控制台应用程序项目。

常见问题解答

在C#中验证QR码校验和的目的是什么?

在C#中验证QR码校验和确保数据完整性,通过验证QR码中编码的数据在传输或扫描过程中没有被损坏。IronQR可以通过提供可靠的校验和验证来协助这一过程。

IronQR如何处理QR码格式检测?

IronQR提供了强大的能力来检测各种QR码格式,确保与范围广泛的QR码标准兼容。此功能帮助开发人员在其C#应用程序中高效地处理QR码。

Reed-Solomon错误校正在QR码验证中起什么作用?

Reed-Solomon错误校正是一种用于QR码的错误修正方法,用以纠正扫描过程中发生的错误。IronQR利用这种技术来增强容错性,允许它准确读取并验证即便是有一定失真或损伤的QR码。

IronQR能否在处理QR码时应用空安全模式?

是的,IronQR可以应用空安全模式,这有助于在处理QR码数据时防止空引用错误。这确保了您的应用程序能更可靠和高效地处理QR码。

在QR码处理过程中,容错性为何重要?

容错性至关重要,因为它允许QR码处理系统在处理错误或差异时不会失败。IronQR的容错功能确保即便部分损坏或遮挡的QR码也能被准确读取。

在C#中使用IronQR进行QR码验证的好处是什么?

IronQR为QR码验证提供了多种优势,包括高精确度、支持多种QR码格式以及内置的错误校正机制,使其成为使用C#处理QR码的开发人员的理想选择。

IronQR如何增强QR码读取的准确性?

IronQR通过使用高级算法来检测和解码QR码,并应用像Reed-Solomon这样的错误校正方法来处理扫描期间的数据损坏,从而提高读取准确性。

What is the role of the `QrResult` class in IronQR?

The `QrResult` class in IronQR represents decoded results from QR code reading. It provides properties like the decoded value, detected QR type, and optionally, a URI if the value is a valid URL.

How does IronQR handle physical damage to QR codes?

IronQR uses Reed-Solomon error correction to automatically correct minor damages in QR codes during decoding, making it more resilient to physical damage and ensuring data integrity.

Can IronQR validate QR codes asynchronously?

Yes, IronQR supports asynchronous workflows. You can use `ReadAsync` to perform non-blocking QR code reading while applying similar validation patterns on the results.

Curtis Chau
技术作家

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

...
阅读更多

准备开始了吗?

Nuget Downloads 74,386版本:2026.9刚刚发布

立即获取您的免费30 天试用密钥
无需信用卡或创建账户
C# 用于 PDF 的 NuGet 库
通过 NuGet 安装

版本: 2026.9

PM > Install-Package IronQR
nuget.org/packages/IronQR/
  1. 在解决方案资源管理器中,右键点击引用,管理 NuGet 包
  2. 选择浏览并搜索“IronQR”
  3. 选择包并安装
C# PDF DLL
下载 DLL

版本: 2026.9

  1. 下载并解压IronQR到您的解决方案目录中的~/Libs等位置
  2. 在Visual Studio解决方案资源管理器中,右键单击引用。选择浏览,“IronQR.dll”

许可证从$999开始

Key in blue circle

立即获取免费的 30 天试用版密钥

Your trial license will be sent to your email address

无任何限制。100% 解锁。无需信用卡。

bullet_checked无需信用卡或创建账户无任何限制。100% 解锁。无需信用卡。
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried IronPDF
预约您的免费现场演示
Booking Badge

深受全球数百万工程师信赖

Iron Software 的客户徽标
获取您的无义务咨询
填写下面的表格或通过sales@ironsoftware.com
您的资料将始终保密。
深受全球数百万工程师信赖
Iron Software 的客户徽标
立即获取您的免费30 天试用密钥
无需信用卡或创建账户