如何設定錯誤更正C# | IronQR

如何在C#中設置錯誤更正

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

C#條碼的錯誤更正是通過IronBarcode的H),可恢復7-30%的受損資料,較高級別會生成更複雜的QR碼。

條碼中的Error Correction指的是在視覺缺陷或編碼錯誤下保持條碼可讀性的能力。 這些損壞可能是由於印刷缺陷、污漬、劃痕或掃描條件的變化引起的。 錯誤更正是決定哪種型別的條碼編碼合適的主要因素,特別是在處理C#中QR碼時。

一般而言,由於以下因素,2D條碼對缺陷的容忍度比1D條碼高:

  • 資料容量:2D條碼比1D條碼儲存更多資料,既橫向又縱向編碼。 了解更多關於支持的條碼格式
  • 冗餘性:2D條碼具有多層資料編碼,即使條碼的一部分損壞,仍能從任何尚完整的部分提取資訊。
  • 緊湊性:2D條碼因其緊湊形狀適用於空間有限的區域。
  • 靈活性:2D條碼可以從各種角度和方向掃描。

在處理不完美條碼和圖像校正情境中,錯誤更正變得尤為重要,因為掃描條件並不理想。

快速入門:使用錯誤更正級別建立QR碼

這個簡短範例展示了如何使用IronBarcode生成QR碼,並將錯誤更正級別設置為中等。 開發者可以使用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


如何調整QR碼中的錯誤更正?

目前,IronBarcode支持在其條碼生成功能中設置QR碼Micro QRsrMQR的錯誤更正。 它支持QR碼標準指定的所有四個預設錯誤更正級別。 錯誤更正級別通過QRCodeWriter.CreateQrCode方法中進行調整。 四種錯誤更正級別是:

  • 最高:級別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碼可能暴露於物理損壞、汙垢或部分遮擋的應用,建議使用較高的錯誤更正級別(H)。 這些級別在增加QR碼複雜性和尺寸的代價下提供更好的容錯性

對於數位顯示或高質量印刷等乾淨的受控環境,較低的錯誤更正級別(M)可能足夠。這些生成更簡單、密度較低的QR碼,方便小尺寸掃描。 請考慮以下因素:

  • 物理環境:戶外或工業環境受益於較高的錯誤更正
  • 印刷質量:較低質量的印刷需要較高的錯誤更正
  • 尺寸限制:有限的空間可能需要較低的錯誤更正以提高可讀性
  • 掃描距離:較長的掃描距離適合較簡單的QR碼

這是一個範例,展示如何生成具有不同錯誤更正級別的QR碼以供比較:

:path=/static-assets/barcode/content-code-examples/how-to/error-correction-3.cs
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")
$vbLabelText   $csharpLabel

哪些參數控制錯誤更正?

在IronBarcode中控制錯誤更正的主要參數是QrErrorCorrectionLevel枚舉。 此參數傳遞給CreateQrCode方法,並決定在QR碼中嵌入多少冗餘資料。 在建立自訂QR碼時,您可以將錯誤更正設置與其他樣式選項結合使用:

:path=/static-assets/barcode/content-code-examples/how-to/error-correction-4.cs
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")
$vbLabelText   $csharpLabel

為什麼錯誤更正會影響QR碼的複雜性?

錯誤更正通過使用Reed–Solomon錯誤更正算法向QR碼新增冗餘資料來工作。 這種冗餘允許QR碼讀取器重構丟失或損壞的資料部分。 增加的錯誤更正會導致需要更多模塊(黑白方塊)來編碼相同資訊,產生更密集、更複雜的圖案。

這種複雜性對條碼讀取設置和掃描性能有實際影響。 更高的錯誤更正級別可能需要調整讀取器設置以獲得最佳性能。

不同的錯誤更正級別是什麼?

下面是一組QR碼圖像樣本,每個樣本代表相同的值,但具有不同級別的錯誤更正。 如觀察所示,更高的錯誤更正級別導致更複雜的QR碼圖像,提供更好的容錯能力。

具有最高錯誤更正等級的 QR 碼,顯示密集的黑白方塊圖樣
展示高錯誤更正等級的 QR 碼,具有密集的資料圖樣與穩固的定位圖樣
具有中錯誤更正等級的 QR 碼,顯示清晰的資料圖樣與定位方塊
具有低錯誤更正等級的 QR 碼,顯示標準的黑白矩陣圖樣

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

在幾種場景中建議使用較高的錯誤更正級別:

  1. 工業應用:當QR碼用於暴露於惡劣環境中的產品或裝置時
  2. 戶外標識:在戶外顯示、受天氣損害的QR碼
  3. 長期儲存:需要多年保持可掃描文件或產品
  4. 營銷材料:當包含部分遮擋QR碼的標誌或設計時

對於涉及讀取多個條碼或處理受損圖像的高級情境,高錯誤更正提供額外的可靠性。

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

錯誤更正直接影響QR碼的物理大小和資料容量。 更高的錯誤更正級別需要更多模塊來編碼相同數量的資料,這可能導致:

  • 相同資料內容的QR碼更大
  • 在給定QR碼大小下,最大資料容量減少
  • 更複雜的圖案,可能在小尺寸下較難掃描
  • 生成和掃描的處理時間增加

更正級別之間的視覺差異是什麼?

當比較QR碼編碼相同資料時,錯誤更正級別之間的視覺差異變得明顯。 較低的更正級別生成少模塊的簡單圖案,而較高級別生成密集且更複雜的圖案。 這些差異不僅影響外觀,還影響印刷和顯示的實際考量。

欲了解更多高級條碼生成技術,請探索我們的詳盡文件,了解如何將錯誤更正與其他條碼功能整合,實現在.NET應用中的最佳效果。

常見問題

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

條碼中的錯誤更正是指即使有視覺缺陷或編碼錯誤也能保持條碼可讀性的能力。IronBarcode通過QrErrorCorrectionLevel參數來實現錯誤更正,根據選擇的等級可以恢復7-30%的受損資料。

如何在C#中建立QR Codes時設定錯誤更正等級?

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

QR Codes有哪些四個錯誤更正等級?

IronBarcode支援QR Codes的四個預設錯誤更正等級:L(低 - 7%恢復)、M(中 - 15%恢復)、Q(四分之一 - 25%恢復),以及H(高 - 30%恢復)。更高的等級會生成更複雜的QR Codes,但提供更好的損壞抵抗力。

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

目前,IronBarcode支援QR Codes、Micro QR、和rMQR的錯誤更正等級設置。這些二維條碼格式相較於傳統一維條碼提供更優越的錯誤更正能力。

為什麼二維條碼的錯誤更正能力比一維條碼更好?

IronBarcode支持的二維條碼因為具有更高的資料容量(可橫向和縱向編碼),而且內建的冗餘性允許從完好部分提取資料,且其緊湊的形狀適合有限空間,故能夠從各個角度進行掃描,具備優良的錯誤更正能力。

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

當面對不完美的印刷條件、可能的物理損壞(如刮痕、污漬)、具有挑戰的掃描環境或需要長期耐用性時,請使用IronBarcode的更高錯誤更正等級。更高等級提供更可靠的掃描,但會增加QR Code的複雜性。

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

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

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

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

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

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

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

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

Hairil Hasyimi Bin Omar
軟體工程師
和所有偉大的工程師一樣,Hairil是一位充滿熱情的學習者。他正在完善自己對C#、Python和Java的知識,並利用這些知識為Iron Software的團隊成員創造價值。Hairil從馬來西亞的瑪拉工藝大學畢業,擁有化學與流程工程學士學位,後加入Iron Software團隊。
準備好開始了嗎?
Nuget 下載 2,317,217 | 版本: 2026.7 剛剛發布
Still Scrolling Icon

還在滾動嗎?

想快速驗證嗎? PM > Install-Package BarCode
運行範例觀看您的字串成為條碼。