如何在 C# 中设置纠错
在C#条码中,错误修正器是通过IronBarcode的H),可以恢复7-30%的损坏数据,高级别会创建更复杂的二维码。
条码中的Error Correction指的是即使存在视觉缺陷或编码错误,仍能保持条码可读性的能力。 这些损害可能是由于打印缺陷、污迹、划痕或扫描条件差异等因素造成的。 纠错是决定适合哪种类型条形码编码的主要因素,尤其是在使用 C# 中的 QR 代码时。
一般来说,由于以下因素,2D条形码比1D条形码对缺陷具有更高的容忍度:
- 数据容量:二维条形码比一维条形码存储更多数据,可在水平和垂直方向进行编码。 了解有关支持的条形码格式的更多信息。
- 冗余:2D条形码有多层数据编码,即使条形码的一部分受损,也能从剩余完好部分提取信息。
- 紧凑性:由于其紧凑的形状,2D条形码适用于有限的空间。
- 灵活性:2D条形码可以从各种角度和方向进行扫描。
在处理不完美条形码和图像校正等扫描条件不理想的情况时,纠错变得尤为重要。
快速入门:在 QR 码创建中使用错误校正级别这个简短的示例展示了如何使用 IronBarcode 生成二维码,并将纠错级别设置为中等。 开发者可以使用CreateQrCode方法设定内容、大小和错误修正级别。
-
1Install IronBarcode with NuGet Package Manager
-
2复制并运行这段代码。
var qr = IronBarCode.QRCodeWriter.CreateQrCode("https://ironsoftware.com", 500, IronBarCode.QRCodeWriter.QrErrorCorrectionLevel.Medium).SaveAsPng("qr.png");C# -
3部署到您的生产环境中进行测试
通过免费试用立即在您的项目中开始使用IronBarcode
最小工作流程(5 个步骤)
- 下载 C# 库以调整条形码的纠错功能。
- 使用QRCodeWriter类生成二维码
- 修改QrErrorCorrection参数以调整纠错级别
- 可视化比较在四个不同错误纠正级别下生成的二维码
- 检查输出的二维码
如何调整二维码中的纠错功能?
目前,IronBarcode 支持在 QR码、Micro QRs和 rMQRs中设置纠错,作为其综合条码生成功能的一部分。 它支持QR码标准指定的所有四个预设错误校正级别。 通过QrErrorCorrectionLevel参数来调整错误修正级别。 四个错误校正级别是:
- 最高:等级H。 可以恢复多达30%的数据。
- 高:等级Q。 可以恢复多达25%的数据。
- 中等:等级M。 可以恢复多达15%的数据。
- 低:等级L。 可以恢复多达7%的数据。
更高的错误校正级别会导致更加复杂的QR码图像,在生成QR码时需要在视觉清晰度和错误校正之间取得平衡。 下面的代码示例演示了如何设置错误校正:
// Import the necessary namespace for barcode generation
using IronBarCode;
// Create a QR code with the specified URL, size, and error correction level
GeneratedBarcode mediumCorrection = QRCodeWriter.CreateQrCode(
"https://ironsoftware.com/csharp/barcode/", // URL to be encoded in the QR code
500, // Size of the QR code (500x500 pixels)
QRCodeWriter.QrErrorCorrectionLevel.Medium // Error correction level to handle distortions
);
// Save the generated QR code image as a PNG file with the specified filename
mediumCorrection.SaveAsPng("mediumCorrection.png");' Import the necessary namespace for barcode generation
Imports IronBarCode
' Create a QR code with the specified URL, size, and error correction level
Private mediumCorrection As GeneratedBarcode = QRCodeWriter.CreateQrCode("https://ironsoftware.com/csharp/barcode/", 500, QRCodeWriter.QrErrorCorrectionLevel.Medium)
' Save the generated QR code image as a PNG file with the specified filename
mediumCorrection.SaveAsPng("mediumCorrection.png")我应该选择哪个纠错级别?
纠错级别的选择取决于您的具体使用情况和环境。 对于可能暴露于物理损伤、污垢或部分遮挡的二维码应用,建议使用较高的错误修正级别(H)。 这些级别提供了更好的容错性,但代价是增加了 QR 代码的复杂性和大小。
对于像数字显示器或高质量打印这样的干净、受控环境,较低的错误修正级别(M)可能就足够了。这些生成较简单、较稀疏的二维码,可以在较小的尺寸下更容易扫描。 考虑这些因素:
- 物理环境:户外或工业环境受益于更高的纠错能力
- 打印质量:较低质量的打印需要较高的纠错能力
- 尺寸限制:篇幅有限,可能需要降低纠错率以提高可读性
- 扫描距离:较长的扫描距离对较简单的二维码效果更好
下面的示例展示了如何生成具有不同纠错级别的 QR 代码,以供比较:
using IronBarCode;
using System.Drawing;
// Generate QR codes with all four error correction levels
var content = "https://ironsoftware.com/csharp/barcode/";
int size = 500;
// Create QR codes with different error correction levels
var lowCorrection = QRCodeWriter.CreateQrCode(content, size, QRCodeWriter.QrErrorCorrectionLevel.Low);
var mediumCorrection = QRCodeWriter.CreateQrCode(content, size, QRCodeWriter.QrErrorCorrectionLevel.Medium);
var highCorrection = QRCodeWriter.CreateQrCode(content, size, QRCodeWriter.QrErrorCorrectionLevel.High);
var highestCorrection = QRCodeWriter.CreateQrCode(content, size, QRCodeWriter.QrErrorCorrectionLevel.Highest);
// Save each with descriptive filenames
lowCorrection.SaveAsPng("qr_low_correction.png");
mediumCorrection.SaveAsPng("qr_medium_correction.png");
highCorrection.SaveAsPng("qr_high_correction.png");
highestCorrection.SaveAsPng("qr_highest_correction.png");Imports IronBarCode
Imports System.Drawing
' Generate QR codes with all four error correction levels
Dim content As String = "https://ironsoftware.com/csharp/barcode/"
Dim size As Integer = 500
' Create QR codes with different error correction levels
Dim lowCorrection = QRCodeWriter.CreateQrCode(content, size, QRCodeWriter.QrErrorCorrectionLevel.Low)
Dim mediumCorrection = QRCodeWriter.CreateQrCode(content, size, QRCodeWriter.QrErrorCorrectionLevel.Medium)
Dim highCorrection = QRCodeWriter.CreateQrCode(content, size, QRCodeWriter.QrErrorCorrectionLevel.High)
Dim highestCorrection = QRCodeWriter.CreateQrCode(content, size, QRCodeWriter.QrErrorCorrectionLevel.Highest)
' Save each with descriptive filenames
lowCorrection.SaveAsPng("qr_low_correction.png")
mediumCorrection.SaveAsPng("qr_medium_correction.png")
highCorrection.SaveAsPng("qr_high_correction.png")
highestCorrection.SaveAsPng("qr_highest_correction.png")哪些参数控制纠错?
IronBarcode中控制错误修正的主要参数是QrErrorCorrectionLevel枚举。 此参数传递到CreateQrCode方法中,并决定嵌入在二维码中的冗余数据量。 当创建自定义 QR 代码时,您可以将纠错设置与其他样式选项结合起来:
using IronBarCode;
// Create a styled QR code with high error correction
var styledQr = QRCodeWriter.CreateQrCode("Important Data", 500, QRCodeWriter.QrErrorCorrectionLevel.High);
// Apply custom styling
styledQr.ChangeBarCodeColor(System.Drawing.Color.DarkBlue)
.SetMargins(10)
.AddAnnotationTextAboveBarcode("Scan for Details");
// Export with various options
styledQr.SaveAsPng("styled_high_correction.png");
styledQr.SaveAsJpeg("styled_high_correction.jpg");
styledQr.SaveAsPdf("styled_high_correction.pdf");Imports IronBarCode
Imports System.Drawing
' Create a styled QR code with high error correction
Dim styledQr = QRCodeWriter.CreateQrCode("Important Data", 500, QRCodeWriter.QrErrorCorrectionLevel.High)
' Apply custom styling
styledQr.ChangeBarCodeColor(Color.DarkBlue) _
.SetMargins(10) _
.AddAnnotationTextAboveBarcode("Scan for Details")
' Export with various options
styledQr.SaveAsPng("styled_high_correction.png")
styledQr.SaveAsJpeg("styled_high_correction.jpg")
styledQr.SaveAsPdf("styled_high_correction.pdf")为什么纠错会影响二维码的复杂性?
纠错的工作原理是使用里德-所罗门纠错算法在 QR 代码中添加冗余数据。 这种冗余可以让 QR 码阅读器重建数据中缺失或损坏的部分。 添加的纠错功能越多,编码相同信息所需的模块(黑白方格)就越多,从而形成更密集、更复杂的图案。
这种复杂性对条码读取设置和扫描性能有实际影响。 较高的纠错级别可能需要调整阅读器设置以获得最佳性能。
有哪些不同的纠错级别?
以下是一组QR码图像示例,每个图像代表相同的值,但具有不同的错误校正级别。 如所观察到的,较高的错误校正级别导致更复杂的QR码图像,提供更大的容错能力。

最高纠错率

高纠错能力

中等误差校正

低纠错率
何时应该使用高级纠错?
建议在几种情况下采用更高的纠错级别:
- 工业应用:当QR码用于暴露于恶劣环境的产品或设备上
- 户外标识:对于户外显示的QR码,受天气损害
- 长期存储:需要保存多年可扫描的文件或产品
- 市场营销材料:当嵌入部分遮挡QR码的标志或设计时
对于涉及读取多个条形码或处理损坏图像的高级场景,更高的纠错能力可提供额外的可靠性。
纠错如何影响二维码大小?
纠错直接影响二维码的物理尺寸和数据容量。 较高的纠错级别需要更多的模块来对相同数量的数据进行编码,这可能导致以下结果
- 相同数据内容的较大QR码
- 在给定QR码大小下减少最大数据容量
- 更复杂的图案可能在小尺寸扫描时更难
- 生成和扫描的处理时间增加
校正级别之间的视觉差异是什么?
在比较编码相同数据的 QR 代码时,纠错级别之间的视觉差异会很明显。 较低的修正级别会产生模块较少的简单模式,而较高的修正级别则会产生更密集、更复杂的模式。 这些差异不仅会影响外观,还会影响打印和显示时的实际考虑因素。
如需了解更多高级条形码生成技术,请浏览我们的 综合文档,并了解如何将纠错与其他条形码功能集成,以便在您的 .NET 应用程序中获得最佳效果。
常见问题解答
什么是 BarCode 技术中的纠错?
条形码中的纠错是指尽管存在视觉缺陷或编码错误,仍能保持条形码可读性的能力。IronBarcode 通过 QrErrorCorrectionLevel 参数实现纠错,根据所选级别,可恢复 7-30% 的损坏数据。
在 C# 中创建二维码时,如何设置纠错级别?
您可以使用 IronBarcode 的 QRCodeWriter.CreateQrCode 方法,通过指定 QrErrorCorrectionLevel 参数来设置纠错级别。该方法接受内容、大小和四种纠错级别(L、M、Q、H)中的一种。
二维码有哪四种纠错级别?
IronBarcode 支持 QR 码的四种预设纠错级别:L(低--7% 恢复)、M(中--15% 恢复)、Q(四分位--25% 恢复)和 H(高--30% 恢复)。级别越高,QR 码越复杂,但抗损能力越强。
哪些条形码类型支持纠错设置?
目前,IronBarcode 支持为 QR 码、Micro QR 和 rMQR 设置纠错级别。与传统的一维条形码相比,这些二维条形码格式具有更出色的纠错能力。
为什么二维条形码的纠错效果比一维条形码好?
IronBarcode 支持的二维条形码具有更高的数据容量(水平和垂直编码),因此具有更好的纠错能力,内置冗余允许从完整的部分提取数据,形状紧凑适合有限的空间,以及从不同角度扫描的灵活性。
何时应使用较高的纠错级别?
在处理不完美的印刷条件、潜在的物理损坏(划痕、污点)、具有挑战性的扫描环境或需要长期耐用性时,使用 IronBarcode 时应使用较高的纠错级别。较高的纠错级别可提供更可靠的扫描,但会增加 QR 码的复杂性。
Does IronBarcode support custom styling with error correction settings?
Yes, IronBarcode allows you to create styled QR codes, combining custom design elements with error correction settings for more functional and appealing barcodes.
Can I compare QR codes with different error correction levels using IronBarcode?
Absolutely, IronBarcode allows you to generate and visually compare QR codes with different error correction levels, enabling developers to balance between size, complexity, and scannability.
What impact does error correction have on QR code scanning performance?
QR code error correction influences scanning performance, as higher levels may necessitate adjustments to scanner settings due to their increased complexity and density of information, a feature managed efficiently by IronBarcode.
How does IronBarcode enhance QR code generation for .NET developers?
IronBarcode offers .NET developers a powerful tool to generate QR codes with adjustable error correction levels, optimal reader settings, and custom styling options, all critical for ensuring both aesthetic appeal and functional reliability.
