如何在 C# 中设置纠错
C# 条形码的纠错级别是通过 IronBarcode 的 参数在方法中通过 参数进行设置,支持四种级别(),可恢复 7% 至 30% 的受损数据,级别越高生成的QR码结构越复杂。
BARCODE中的Error Correction指即使存在视觉缺陷或编码错误,BARCODE仍能保持可读性的能力。 这些损害可能是由于打印缺陷、污迹、划痕或扫描条件差异等因素造成的。 纠错是决定适合哪种类型条形码编码的主要因素,尤其是在使用 C# 中的 QR 代码时。
一般来说,由于以下因素,2D条形码比1D条形码对缺陷具有更高的容忍度:
- 数据容量:二维条形码比一维条形码存储更多数据,可在水平和垂直方向进行编码。 了解有关支持的条形码格式的更多信息。
- 冗余:2D条形码有多层数据编码,即使条形码的一部分受损,也能从剩余完好部分提取信息。
- 紧凑性:由于其紧凑的形状,2D条形码适用于有限的空间。
- 灵活性:2D条形码可以从各种角度和方向进行扫描。
在处理不完美条形码和图像校正等扫描条件不理想的情况时,纠错变得尤为重要。
快速入门:在 QR 码创建中使用错误校正级别
这个简短的示例展示了如何使用 IronBarcode 生成二维码,并将纠错级别设置为中等。 开发者可以使用 `` 方法,并传入内容、尺寸和纠错级别参数。
最小工作流程(5 个步骤)
- 下载 C# 库以调整条形码的纠错功能。
- 使用QRCodeWriter类生成二维码
- 修改QrErrorCorrection参数以调整纠错级别
- 可视化比较在四个不同错误纠正级别下生成的二维码
- 检查输出的二维码
如何调整二维码中的纠错功能?
目前,IronBarcode 支持在 QR码、Micro QRs和 rMQRs中设置纠错,作为其综合条码生成功能的一部分。 它支持QR码标准指定的所有四个预设错误校正级别。 错误更正级别可通过 QrErrorCorrectionLevel 方法中的 `` 参数进行调整。 四个错误校正级别是:
- 最高:等级H。 可以恢复多达30%的数据。
- 高:等级Q。 可以恢复多达25%的数据。
- 中等:等级M。 可以恢复多达15%的数据。
- 低:等级L。 可以恢复多达7%的数据。
更高的错误校正级别会导致更加复杂的QR码图像,在生成QR码时需要在视觉清晰度和错误校正之间取得平衡。 下面的代码示例演示了如何设置错误校正:
:path=/static-assets/barcode/content-code-examples/how-to/set-error-correction.cs
// 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")
我应该选择哪个纠错级别?
纠错级别的选择取决于您的具体使用情况和环境。 对于可能面临物理损坏、污损或部分遮挡的QR码应用场景,建议采用更高的纠错级别(如 或)。 这些级别提供了更好的容错性,但代价是增加了 QR 代码的复杂性和大小。
对于数字显示屏或高质量PRINT等要求清晰、可控的环境,较低的纠错级别(如 或)可能已足够。 这些设置会生成更简单、密度更低的QR码,在较小尺寸下更容易扫描。 考虑这些因素:
- 物理环境:户外或工业环境受益于更高的纠错能力
- 打印质量:较低质量的打印需要较高的纠错能力
- 尺寸限制:篇幅有限,可能需要降低纠错率以提高可读性
- 扫描距离:较长的扫描距离对较简单的二维码效果更好
下面的示例展示了如何生成具有不同纠错级别的 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 = <code>QRCodeWriter.CreateQrCode(content, size, QRCodeWriter.QrErrorCorrectionLevel.Low)</code>;
var mediumCorrection = <code>QRCodeWriter.CreateQrCode(content, size, QRCodeWriter.QrErrorCorrectionLevel.Medium)</code>;
var highCorrection = <code>QRCodeWriter.CreateQrCode(content, size, QRCodeWriter.QrErrorCorrectionLevel.High)</code>;
var highestCorrection = <code>QRCodeWriter.CreateQrCode(content, size, QRCodeWriter.QrErrorCorrectionLevel.Highest)</code>;
// Save each with descriptive filenames
<code>lowCorrection.SaveAsPng("qr_low_correction.png")</code>;
<code>mediumCorrection.SaveAsPng("qr_medium_correction.png")</code>;
<code>highCorrection.SaveAsPng("qr_high_correction.png")</code>;
<code>highestCorrection.SaveAsPng("qr_highest_correction.png")</code>;
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 = <code>QRCodeWriter.CreateQrCode(content, size, QRCodeWriter.QrErrorCorrectionLevel.Low)</code>;
var mediumCorrection = <code>QRCodeWriter.CreateQrCode(content, size, QRCodeWriter.QrErrorCorrectionLevel.Medium)</code>;
var highCorrection = <code>QRCodeWriter.CreateQrCode(content, size, QRCodeWriter.QrErrorCorrectionLevel.High)</code>;
var highestCorrection = <code>QRCodeWriter.CreateQrCode(content, size, QRCodeWriter.QrErrorCorrectionLevel.Highest)</code>;
// Save each with descriptive filenames
<code>lowCorrection.SaveAsPng("qr_low_correction.png")</code>;
<code>mediumCorrection.SaveAsPng("qr_medium_correction.png")</code>;
<code>highCorrection.SaveAsPng("qr_high_correction.png")</code>;
<code>highestCorrection.SaveAsPng("qr_highest_correction.png")</code>;
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 中控制错误校正的主要参数是 枚举。 此参数传递给 方法,用于确定 QR 码中嵌入的冗余数据量。 当创建自定义 QR 代码时,您可以将纠错设置与其他样式选项结合起来:
using IronBarCode;
// Create a styled QR code with high error correction
var styledQr = <code>QRCodeWriter.CreateQrCode("Important Data", 500, QRCodeWriter.QrErrorCorrectionLevel.High)</code>;
// 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");
using IronBarCode;
// Create a styled QR code with high error correction
var styledQr = <code>QRCodeWriter.CreateQrCode("Important Data", 500, QRCodeWriter.QrErrorCorrectionLevel.High)</code>;
// 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
' Create a styled QR code with high error correction
Dim 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")
为什么纠错会影响二维码的复杂性?
纠错的工作原理是使用里德-所罗门纠错算法在 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 码的复杂性。
IronBarcode是否提供支持自定义条形码外观的功能?
是的,IronBarcode为条形码外观提供广泛的自定义选项,包括颜色、大小和文本注释,允许运您将条形码设计成符合您特定的设计要求。
IronBarcode如何帮助提高业务流程的效率?
IronBarcode通过快速准确的条码生成和读取提高了业务流程效率,减少了手动数据输入错误,并改善了库存和资产跟踪。
在项目中实现IronBarcode需要哪些编程技能?
了解C#编程的基础知识就足以在项目中实现IronBarcode,因为它提供了简单的方法和全面的文档来指导开发人员。
IronBarcode适合小项目和大型企业应用吗?
IronBarcode被设计为可扩展且多功能,适合需要强大条码解决方案的小项目和大型企业应用。

