如何在 C# 中設置錯誤修正 | IronQR

如何在 C# 中設定錯誤修正

This article was translated from English: Does it need improvement?
Translated
View the article in English

C# 條碼中的糾錯是透過 IronBarcode 的 QRCodeWriter.CreateQrCode 方法中的 QrErrorCorrectionLevel 參數設定的,支援四個等級(QCO的損壞數據,等級越高,產生的二維碼越複雜。

條碼糾錯是指即使有視覺缺陷或編碼錯誤,也能保持條碼可讀性的能力。 這些損壞可能是由於印刷瑕疵、污跡、刮痕或掃描條件的變化等因素造成的。 糾錯是決定哪種條碼編碼合適的主要因素,尤其是在 C# 中使用 QR 碼時。

一般來說,由於以下因素,二維條碼比一維條碼具有更高的缺陷容忍度:

-資料容量:二維條碼比一維條碼儲存更多的數據,可以同時進行水平和垂直編碼。 了解更多關於支援的條碼格式的資訊。 -冗餘性:二維條碼具有多層資料編碼,即使條碼的一部分損壞,也可以從任何剩餘的完整部分提取資訊。 -緊湊性:二維條碼由於其緊湊的形狀,適用於空間有限的場所。 -靈活性:二維條碼可以從各種角度和方向掃描。

當處理不完美的條碼和影像校正場景時,如果掃描條件不太理想,糾錯就顯得特別重要。

快速入門:在二維碼建立中使用糾錯等級

這個簡短的範例展示如何使用IronBarcode來產生二維碼,並將糾錯等級設為"中"。 開發者可以使用 CreateQrCode 方法,並指定內容、大小和糾正等級。

  1. 使用NuGet套件管理器安裝https://www.nuget.org/packages/BarCode

    PM > Install-Package BarCode
  2. 複製並運行這段程式碼。

    var qr = IronBarCode.QRCodeWriter.CreateQrCode("https://ironsoftware.com", 500, IronBarCode.QRCodeWriter.QrErrorCorrectionLevel.Medium).SaveAsPng("qr.png");
  3. 部署到您的生產環境進行測試

    今天就在您的專案中開始使用免費試用IronBarcode

    arrow pointer


如何調整二維碼的糾錯設定?

目前, IronBarcode在其全面的條碼產生功能中支援設定QR 碼Micro QR 碼rMQR 碼的糾錯功能。 它支援二維碼標準規定的所有四個預設糾錯等級。 The error correction level is adjusted via the QrErrorCorrectionLevel parameter in the QRCodeWriter.CreateQrCode method. 糾錯分為四個層次:

-最高等級H級。 最多可恢復 30% 的資料。 -Q級。 最多可恢復 25% 的資料。 -M級。 最多可恢復 15% 的資料。 -L級。 最多可恢復 7% 的資料。

更高的糾錯等級會導致更複雜的二維碼影像,因此在生成二維碼時需要在視覺清晰度和糾錯之間取得平衡。 以下程式碼範例示範如何設定糾錯:

: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");
$vbLabelText   $csharpLabel

我該選擇哪一個糾錯等級?

糾錯等級的選擇取決於您的特定使用場景和環境。 對於二維碼可能受到物理損壞、污垢或部分遮蔽的應用,建議使用更高的糾錯等級(QH)。 這些等級提供了更好的容錯能力,但代價是增加了二維碼的複雜性和大小。

對於潔淨、可控制的環境,例如數位顯示器或高品質印刷,較低的糾錯等級(例如 LM)可能就足夠了。這些等級的二維碼更簡單、密度更低,尺寸更小也更容易掃描。 請考慮以下因素:

-物理環境:戶外或工業環境有利於更高的誤差修正率 -列印品質:列印品質越低,糾錯率越高。 -篇幅限制:空間有限可能需要降低糾錯率以提高可讀性。 掃描距離:較遠的掃描距離較適合簡單的二維碼。

以下範例展示如何產生具有不同糾錯等級的二維碼,以便進行比較:

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>;
$vbLabelText   $csharpLabel

哪些參數控制誤差校正?

The primary parameter controlling error correction in IronBarcode is the QrErrorCorrectionLevel enumeration. This parameter is passed to the CreateQrCode method and determines how much redundant data is embedded in the QR code. 建立自訂二維碼時,您可以將糾錯設定與其他樣式選項結合使用:

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");
$vbLabelText   $csharpLabel

為什麼糾錯會影響二維碼的複雜度?

糾錯功能透過使用里德-所羅門糾錯演算法向二維碼添加冗餘資料來實現。 這種冗餘設計使得二維碼閱讀器能夠重建缺少或損壞的資料部分。 增加的糾錯越多,編碼相同訊息所需的模組(黑白方塊)就越多,從而產生更密集、更複雜的圖案。

這種複雜性對條碼讀取設定和掃描效能有實際影響。 更高的糾錯等級可能需要調整讀卡機設定以獲得最佳效能。

糾錯級別有哪些?

下面這組二維碼圖像範例,每個圖像都代表相同的值,但糾錯等級各不相同。 如觀察所見,更高的糾錯等級會導致更複雜的二維碼影像,從而提供更大的容錯能力。

QR code with highest error correction level showing dense pattern of black and white squares
QR code demonstrating high error correction level with dense data patterns and robust finder patterns
QR code with medium error correction level showing clear data pattern and finder squares
QR code with low error correction level showing standard black and white matrix pattern

何時應該使用更高的糾錯等級?

在以下幾種情況下建議採用更高的糾錯等級:

1.工業應用:當二維碼用於暴露於惡劣環境下的產品或設備時
2.戶外標示牌:適用於戶外展示的二維碼,容易受天氣損壞。
3.長期儲存:需要多年保持可掃描狀態的文件或產品
4.行銷資料:當使用會部分遮蔽二維碼的標誌或設計時

對於涉及讀取多個條碼或處理損壞影像的高階場景,更高的糾錯能力可提供更高的可靠性。

糾錯機制如何影響二維碼大小?

糾錯功能直接影響二維碼的實體尺寸和資料容量。 更高的糾錯等級需要更多的模組來編碼相同數量的數據,這可能導致:

  • 相同資料內容,更大的二維碼
  • 在給定二維碼尺寸下,最大資料容量降低
  • 更複雜的圖案,在小尺寸下可能更難掃描。
  • 產生和掃描的處理時間均增加

不同矯正等級之間在視覺上有哪些差異?

當比較編碼相同資料的二維碼時,不同糾錯程度的視覺差異就會變得很明顯。 較低的校正水準會產生模組較少的簡單圖案,而較高的校正水準會產生更密集、更複雜的圖案。 這些差異不僅影響外觀,也影響印刷和展示等實際問題。

要了解更高級的條碼生成技術,請瀏覽我們的綜合文檔,了解如何將糾錯功能與其他條碼功能集成,以便在您的.NET應用程式中獲得最佳效果。

常見問題解答

什麼是 BarCode 技術中的錯誤修正?

BarCode 中的錯誤修正是指在視覺缺陷或編碼錯誤的情況下仍能保持條碼可讀性的能力。IronBarcode 透過 QrErrorCorrectionLevel 參數實現錯誤修正,根據所選擇的等級,可恢復 7-30% 的損壞資料。

在 C# 中建立 QR 碼時,如何設定錯誤修正等級?

您可以使用 IronBarcode 的 QRCodeWriter.CreateQrCode 方法,透過指定 QrErrorCorrectionLevel 參數來設定錯誤修正等級。該方法接受內容、大小以及四種錯誤修正等級(L、M、Q、H)中的一種。

QR 碼有哪四種錯誤修正等級?

IronBarcode 支援四種預設的 QR 代碼錯誤修正等級:L (Low - 7% 復原率)、M (Medium - 15% 復原率)、Q (Quartile - 25% 復原率) 和 H (High - 30% 復原率)。較高的等級可產生更複雜的 QR 碼,但具有較佳的抗損壞性。

哪些 BarCode 類型支援錯誤修正設定?

目前,IronBarcode 支援為 QR Code、Micro QRs 和 rMQRs 設定錯誤修正等級。與傳統的一維條碼相比,這些二維條碼格式具有更優異的錯誤修正能力。

為什麼 2D 條碼比 1D 條碼有更好的錯誤修正效果?

IronBarcode 所支援的二維條碼因具有較高的資料容量(水平和垂直編碼)而具有較佳的錯誤更正能力,內建的備援功能允許從完整的部分擷取資料,緊湊的形狀適合有限的空間,以及可從不同角度掃描的彈性。

何時應該使用較高的錯誤修正等級?

在處理不完美的印刷條件、潛在的實體損害(刮痕、污點)、具挑戰性的掃描環境,或需要長期耐用性時,請使用 IronBarcode 較高的錯誤修正等級。較高的等級可提供更可靠的掃描,但會增加 QR 代碼的複雜性。

Hairil Hasyimi Bin Omar
軟體工程師
和所有优秀的工程师一样,Hairil 是個努力学习者。他正在细化自己的 C# 、Python 和 Java 知识,将这些知识應用于 Iron Software 各個团队成员以增加价值。Hairil 自马来西亚 Universiti Teknologi MARA 加入 Iron Software 团队,并以化学与工艺工程学士学位毕业。
準備好開始了嗎?
Nuget 下載 2,108,094 | 版本: 2026.3 剛剛發布
Still Scrolling Icon

還在捲動嗎?

想要快速證明? PM > Install-Package BarCode
執行範例 看您的字串變成 BarCode。