在C#中設置錯誤更正的方法 | IronQR

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

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

C# BarCode 的錯誤校正功能是透過 IronBarcode 的 參數,在 方法中透過 參數設定,支援四種等級 (),可恢復 7% 至 30% 的損壞資料,且等級越高,生成的 QR 碼結構越複雜。

BARCODE中的 Error Correction 指的是即使出現視覺缺陷或編碼錯誤,仍能維持BARCODE可讀性的能力。 這些損壞可能源於列印瑕疵、污漬、刮痕或掃描條件的變化等因素。 錯誤校正是在決定哪種 BARCODE 編碼類型適合時的重要考量因素,特別是在 C# 中處理 QR 碼時。

一般而言,由於以下因素,二維BARCODE相較於一維BARCODE具有更高的缺陷容忍度:

  • 資料容量:2D BARCODE 比 1D BARCODE 儲存更多資料,能同時進行水平與垂直編碼。 進一步了解支援的BarCode格式
  • 冗餘性:二維BARCODE具有多層資料編碼,即使BARCODE部分受損,仍可從任何完好無損的區域提取資訊。
  • 緊湊性:由於其緊湊的形狀,2D BARCODE 非常適合空間有限的場合。
  • 靈活性:2D BARCODE 可從各種角度和方向進行掃描。

當處理不完整的BarCode,或在掃描條件不理想的影像修正情境下,錯誤修正便顯得尤為重要。

快速入門:在 QR 碼建立中使用錯誤修正等級

此簡短範例展示如何使用 IronBarcode 生成 QR 碼,並將錯誤修正等級設定為"中"。 開發人員可使用 `` 方法,並指定內容、大小及錯誤修正等級。

  1. using 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


如何調整 QR 碼的錯誤修正設定?

目前,IronBarcode 作為其全面 BarCode 生成功能的一部分,支援在 QR 碼Micro QR 碼及 rMQR 碼中設定錯誤校正功能。 它支援 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")
$vbLabelText   $csharpLabel

我應該選擇哪種錯誤修正等級?

錯誤修正等級的選擇取決於您的具體使用情境與環境。 對於可能面臨物理損壞、髒污或部分遮蔽的 QR 碼應用場景,建議採用較高的錯誤校正等級()。 這些等級雖能提供更好的容錯能力,但代價是 QR 碼的複雜度與大小會增加。

對於數位顯示器或高品質PRINT等需保持清晰、受控的環境,較低的錯誤校正等級(如 )可能已足夠。 這些設定會產生更簡單、密度較低的 QR 碼,即使尺寸較小也更容易掃描。 請考量以下因素:

  • 物理環境:戶外或工業環境中,較高的錯誤修正能力能帶來顯著效益
  • 列印品質:較低的列印品質需要更高的錯誤修正
  • 篇幅限制:受限的空間可能需要降低錯誤修正的嚴謹度,以確保可讀性
  • 掃描距離:掃描距離越長,對較簡單的 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")
$vbLabelText   $csharpLabel

哪些參數會影響錯誤修正?

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

為何錯誤校正會影響 QR 碼的複雜度?

錯誤校正機制是透過使用里德-所羅門(Reed–Solomon)錯誤校正演算法,在 QR 碼中加入冗餘資料來實現。 這種冗餘機制使 QR 碼讀取器能夠重建資料中遺失或受損的部分。 錯誤校正機制越強,編碼相同資訊所需的模組(黑白方塊)就越多,導致圖案變得更密集、更複雜。

此複雜性對BarCode讀取設定及掃描效能具有實際影響。 若採用較高的錯誤修正等級,可能需要調整閱讀器設定以獲得最佳效能。

錯誤修正等級有哪些?

以下是一組 QR 碼圖片範例,每張代表相同的值,但具備不同程度的錯誤修正能力。 如觀察所示,較高的錯誤修正等級會導致 QR 碼圖像更為複雜,從而提供更高的容錯能力。

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. 工業應用:當 QR 碼應用於暴露於惡劣環境的產品或設備時
  2. 戶外標誌:適用於暴露於戶外環境且可能受天候損壞的 QR 碼
  3. 長期儲存:需在數年內仍可掃描的文件或產品
  4. 行銷素材:當整合的標誌或設計部分遮擋 QR 碼時

對於涉及讀取多個BarCode或處理受損影像的進階情境,更強大的錯誤修正功能能提供額外的可靠性。

錯誤修正如何影響 QR 碼的大小?

錯誤修正會直接影響 QR 碼的物理尺寸與資料容量。 較高的錯誤修正等級需要更多模組來編碼相同量的資料,這可能導致:

  • 相同資料內容的較大 QR 碼
  • 在特定 QR 碼尺寸下,最大資料容量已降低
  • 更複雜的圖案,在小尺寸下可能較難辨識
  • 生成與掃描的處理時間均有所增加

不同校對等級在視覺上有哪些差異?

當比較編碼相同資料的 QR 碼時,不同錯誤校正等級之間的視覺差異便顯而易見。 較低的修正等級會產生模組較少、結構較簡單的圖案,而較高的等級則會生成更密集、更複雜的圖案。 這些差異不僅影響外觀,也涉及列印與顯示的實際考量。

若需更進階的BarCode生成技術,請參閱我們的完整文件,並瞭解如何將錯誤修正功能與其他BarCode功能整合,以在您的 .NET 應用程式中獲得最佳效果。

常見問題

什麼是條碼技術中的錯誤更正?

條碼中的錯誤更正是指即使存在視覺缺陷或編碼錯誤仍能保持條碼可讀性的能力。IronBarcode透過QrErrorCorrectionLevel參數實現錯誤更正,根據選定的級別,允許恢復7-30%的損壞資料。

如何在C#中創建QR Codes時設置錯誤更正級別?

您可以使用IronBarcode的QRCodeWriter.CreateQrCode方法設置錯誤更正級別,需指定QrErrorCorrectionLevel參數。此方法接受內容、大小以及四個錯誤更正級別之一(L、M、Q、H)。

QR Codes可用的四個錯誤更正級別是什麼?

IronBarcode支持四個預設的QR Code錯誤更正級別:L(低-7%恢復)、M(中-15%恢復)、Q(四分位-25%恢復)、H(高-30%恢復)。更高級別會創建更複雜的QR Codes,但提供更好的損壞抵抗力。

哪些條碼類型支持錯誤更正設置?

目前,IronBarcode支持為QR Codes、Micro QRs和rMQRs設置錯誤更正級別。這些2D條碼格式相比傳統的1D條碼提供更好的錯誤更正能力。

為何2D條碼的錯誤更正能力優於1D條碼?

IronBarcode支持的2D條碼由於具有更高的資料容量(橫向和縱向編碼)、內建冗餘允許從完整的部分提取資料、適合有限空間的緊湊形狀、以及在各種角度掃描的靈活性,因此具有更好的錯誤更正能力。

我應該什麼時候使用較高的錯誤更正級別?

當面對不完美的打印條件、潛在的物理損壞(刮痕、污漬)、困難的掃描環境或需要長期耐用性時,請使用IronBarcode較高的錯誤更正級別。較高級別提供的掃描更可靠,但增加了QR Code的複雜性。

IronBarcode是否提供自定義條碼外觀的支持?

是的,IronBarcode提供了廣泛的條碼外觀自定義選項,包括顏色、大小和文字註釋,讓您可以根據具體設計需求定制條碼。

IronBarcode如何幫助改善業務流程效率?

IronBarcode通過使條碼生成和讀取快速且準確來提高業務流程效率,減少手動數據輸入錯誤,並改善庫存和資產追蹤。

將IronBarcode實現於專案中需要什麼程式設計技能?

基本的C#程式設計知識足以將IronBarcode實現於專案中,因為它提供了簡單的方法和全面的文檔來指導開發者。

IronBarcode適合於小型專案和大型企業應用嗎?

IronBarcode設計為可擴展且多功能,使其適合小型專案和需要強大條碼解決方案的大型企業應用。

Hairil Hasyimi Bin Omar
軟體工程師
如同所有傑出的工程師,Hairil 是一位熱衷學習的人。他正不斷精進自己在 C#、Python 和 Java 方面的知識,並運用這些知識為 Iron Software 的團隊成員創造價值。Hairil 從馬來西亞馬拉科技大學(Universiti Teknologi MARA)加入 Iron Software 團隊,他在該校取得化學與製程工程學士學位。
準備好開始了嗎?
Nuget 下載 2,240,258 | 版本: 2026.5 just released
Still Scrolling Icon

還在捲動嗎?

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