如何在 .NET 6 中使用 QRCoder 與 IronQR 比較生成 QR 碼
C# 中的二維碼生成
在 C# 應用程式中建立二維碼是開發人員的常見需求,特別是對於涉及產品識別、票務或輕鬆共享 URL 和其他資料的應用程式。 C# 中有多個可用於產生二維碼的函式庫。 兩個值得關注的選擇是QRCoder和IronQR 。 本文將探討這兩個函式庫,比較它們的功能、易用性、效能以及其他與使用 .NET 6 的開發人員相關的因素。本文將深入比較這些函式庫,考察它們的功能、易用性、許可等,並提供程式碼範例來說明它們的用法。
如何在 .NET 6 中使用 QRCoder 產生二維碼(與 IronQR 相比)
- 建立一個新的 Visual Studio 專案來產生二維碼
- 安裝 QRCoder 和 IronQR NuGet 套件以進行比較
- 使用 QRCoder 和 IronQR 建立二維碼
- 使用 QRCoder 和 IronQR 進行自訂選項
QR 圖碼
QRCoder是一個以 C# 編寫的開源程式庫,它提供了 QR 碼實現,讓您在任何 .NET 應用程式中產生符合 ISO/IEC 18004 定義的 QR 碼。 這是一個輕量級且易於使用的函式庫,不依賴其他函式庫或網路協定棧。
以下是QRCoder的主要特點和優點:
1. 易用性
C# QRCoder提供了一個簡單直覺的 API,讓開發人員能夠以最少的努力輕鬆產生 QR 碼/QR 碼文字。 它的簡易性使得各種技能等級的開發人員都能快速地將二維碼產生功能整合到他們的專案中。
2. 豐富的自訂選項
QRCoder 的一個突出特點是能夠根據特定要求自訂二維碼。 開發者可以調整糾錯等級、大小、顏色等參數,甚至可以在二維碼中嵌入徽標或圖像。
3. 多種編碼格式
QRCoder 支援多種編碼格式,使開發人員能夠將不同類型的資料編碼到 QR 碼中。 無論是純文字、URL、聯絡資訊或 Wi-Fi 憑證,QRCoder 都能處理各種資料格式。
4. 高品質輸出
C# QRCoder 產生的二維碼品質很高,可確保在不同裝置和掃描條件下具有良好的可讀性和可靠性。 這種可靠性對於以二維碼作為物理互動和數位互動之間橋樑的應用至關重要。
5. 開源和積極開發
C# QRCoder 是一個開源項目,允許開發人員為其開發做出貢獻,並確保持續改進和更新。 這種積極的社區參與促進了創新,並確保圖書館在不斷發展的技術環境中保持相關性。
IronQR
IronQR是一個功能強大的 C# 二維碼庫,由Iron Software開發和維護。 它允許 C# 軟體工程師在 .NET 應用程式和網站中檢測、讀取和建立二維碼。 以下是IronQR的一些主要特點:
二維碼生成
IronQR 可實現高度可自訂的二維碼產生。 您可以建立具有各種選項的二維碼,例如調整大小、邊距、邊框和重新著色。
using IronQr;
using IronSoftware.Drawing;
public class Program
{
public static void Main()
{
// Prepare a QR Code object
QrCode theQrGen = QrWriter.Write("Awesome IronQR");
// Save QR Code to memory
AnyBitmap myQrImage = theQrGen.Save();
// Save QR Code image to disk
myQrImage.SaveAs("awesome.png");
}
}
using IronQr;
using IronSoftware.Drawing;
public class Program
{
public static void Main()
{
// Prepare a QR Code object
QrCode theQrGen = QrWriter.Write("Awesome IronQR");
// Save QR Code to memory
AnyBitmap myQrImage = theQrGen.Save();
// Save QR Code image to disk
myQrImage.SaveAs("awesome.png");
}
}
Imports IronQr
Imports IronSoftware.Drawing
Public Class Program
Public Shared Sub Main()
' Prepare a QR Code object
Dim theQrGen As QrCode = QrWriter.Write("Awesome IronQR")
' Save QR Code to memory
Dim myQrImage As AnyBitmap = theQrGen.Save()
' Save QR Code image to disk
myQrImage.SaveAs("awesome.png")
End Sub
End Class
QR 圖碼讀取
IronQR採用先進的機器學習模型進行二維碼偵測。 此型號可確保準確快速地讀取二維碼。 IronQR 支援讀取各種影像格式的二維碼,包括 JPG、PNG、SVG、bmp 以及 gif 和 tiff 等多頁影像。
using IronQr;
using IronSoftware.Drawing;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
// Read QR code
var inputBmp = AnyBitmap.FromFile("awesome.png");
// Load the image into QrImageInput
QrImageInput imageInput = new QrImageInput(inputBmp);
// Create the QR Reader object
QrReader reader = new QrReader();
// Read the Input and get all embedded QR Codes
IEnumerable<QrResult> results = reader.Read(imageInput);
}
}
using IronQr;
using IronSoftware.Drawing;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
// Read QR code
var inputBmp = AnyBitmap.FromFile("awesome.png");
// Load the image into QrImageInput
QrImageInput imageInput = new QrImageInput(inputBmp);
// Create the QR Reader object
QrReader reader = new QrReader();
// Read the Input and get all embedded QR Codes
IEnumerable<QrResult> results = reader.Read(imageInput);
}
}
Imports IronQr
Imports IronSoftware.Drawing
Imports System.Collections.Generic
Public Class Program
Public Shared Sub Main()
' Read QR code
Dim inputBmp = AnyBitmap.FromFile("awesome.png")
' Load the image into QrImageInput
Dim imageInput As New QrImageInput(inputBmp)
' Create the QR Reader object
Dim reader As New QrReader()
' Read the Input and get all embedded QR Codes
Dim results As IEnumerable(Of QrResult) = reader.Read(imageInput)
End Sub
End Class
跨平台相容性
- 桌面:適用於 Windows 桌面應用程式(WPF 和 MAUI)。
- 行動端:相容於 Xamarin 和 MAUI。
- Web:支援 Blazor 和 WebForms。
- 控制台:應用程式和庫環境。
- 雲端運算:Docker、Azure 和 AWS。
資料編碼
您可以在二維碼中編碼各種類型的資料,包括文字、網址、位元組和數字。
錯誤糾正
IronQR提供詳細的錯誤訊息和自訂糾錯選項。
IronQR 因其可靠性和易用性而受到全球數百萬用戶的信賴,深受全球工程師的信賴。 要開始使用 IronQR,您可以透過NuGet安裝它。
步驟 1:建立一個新的 Visual Studio 專案來產生二維碼
要開始編寫程式碼,讓我們建立一個Visual Studio專案。 開啟 Microsoft Visual Studio 2022,然後選擇"建立新專案"選項。
如何在 .NET 6 中使用 QRCoder 產生二維碼(與 IronQR 相比):圖 1 - 開啟 Visual Studio 並點擊"建立新專案"選項
從模板清單中選擇控制台應用程式模板
如何在 .NET 6 中使用 QRCoder 產生二維碼(與 IronQR 相比):圖 2 - 選擇專案類型為"控制台應用程式"。
然後提供專案名稱和解決方案名稱。 選擇專案文件的儲存路徑
如何在 .NET 6 中使用 QRCoder 產生二維碼(與 IronQR 相比):圖 3 - 透過指定項目名稱、位置和解決方案名稱來配置項目,然後按一下"下一步"。
選擇所需的 .NET 版本。 在撰寫本文時,我將使用最新的 .NET 8 框架。
如何在 .NET 6 中使用 QRCoder 產生二維碼(與 IronQR 比較):圖 4 - 選擇最新的 .NET Framework 版本,然後按一下"建立"按鈕。
點擊創建按鈕後,專案即被創建並可以開始開發。
步驟 2:安裝 QRCoder 和 IronQR NuGet 套件
可以使用 Visual Studio NuGet 套件管理器安裝QRCoder NuGet 套件,如下圖所示。
或使用以下命令從 NuGet 套件管理器控制台安裝它
dotnet add package QRCoder --version 1.4.3
QRCoder NuGet 套件可在此處從 NuGet 網站取得。
如何在 .NET 6 中使用 QRCoder 產生二維碼(與 IronQR 相比):圖 6 - QRCoder NuGet 套件
IronQR也可以使用 Visual Studio NuGet 套件管理器進行類似安裝,如下所示。
此外,也可以從 NuGet 套件管理器控制台使用下列命令
dotnet add package IronQR --version 2024.4.1
如何在 .NET 6 中使用 QRCoder 產生二維碼(與 IronQR 相比):圖 8 - IronQR NuGet 套件
步驟 3:使用 QRCoder 和 IronQR 建立二維碼
這兩個函式庫都只需要簡單的程式碼就能創造二維碼,我們來看看吧。
QR 圖碼
以下程式碼展示如何使用 QRCoder 產生我們的第一個二維碼。
using QRCoder;
using System.Drawing;
using System.IO;
namespace QRCoderVsIronQR
{
public class Program
{
public static void Main()
{
// Initialize QRCodeGenerator
using (QRCodeGenerator qrGenerator = new QRCodeGenerator())
// Create QR code data
using (QRCodeData qrCodeData = qrGenerator.CreateQrCode("QRCoder Demo 1", QRCodeGenerator.ECCLevel.Q))
// Initialize the QR code with the data
using (BitmapByteQRCode qrCode = new BitmapByteQRCode(qrCodeData))
{
// Generate the QR code's graphic and store it in a byte array
byte[] qrCodeImage = qrCode.GetGraphic(20);
// Convert the byte array to an image format and save it to disk
using (var file = Image.FromStream(new MemoryStream(qrCodeImage)))
{
file.Save("QrCoderDemo1.png");
}
}
}
}
}
using QRCoder;
using System.Drawing;
using System.IO;
namespace QRCoderVsIronQR
{
public class Program
{
public static void Main()
{
// Initialize QRCodeGenerator
using (QRCodeGenerator qrGenerator = new QRCodeGenerator())
// Create QR code data
using (QRCodeData qrCodeData = qrGenerator.CreateQrCode("QRCoder Demo 1", QRCodeGenerator.ECCLevel.Q))
// Initialize the QR code with the data
using (BitmapByteQRCode qrCode = new BitmapByteQRCode(qrCodeData))
{
// Generate the QR code's graphic and store it in a byte array
byte[] qrCodeImage = qrCode.GetGraphic(20);
// Convert the byte array to an image format and save it to disk
using (var file = Image.FromStream(new MemoryStream(qrCodeImage)))
{
file.Save("QrCoderDemo1.png");
}
}
}
}
}
Imports QRCoder
Imports System.Drawing
Imports System.IO
Namespace QRCoderVsIronQR
Public Class Program
Public Shared Sub Main()
' Initialize QRCodeGenerator
Using qrGenerator As New QRCodeGenerator()
' Create QR code data
Using qrCodeData As QRCodeData = qrGenerator.CreateQrCode("QRCoder Demo 1", QRCodeGenerator.ECCLevel.Q)
' Initialize the QR code with the data
Using qrCode As New BitmapByteQRCode(qrCodeData)
' Generate the QR code's graphic and store it in a byte array
Dim qrCodeImage() As Byte = qrCode.GetGraphic(20)
' Convert the byte array to an image format and save it to disk
Using file = Image.FromStream(New MemoryStream(qrCodeImage))
file.Save("QrCoderDemo1.png")
End Using
End Using
End Using
End Using
End Sub
End Class
End Namespace
程式碼解釋
- 初始化
QRCodeGenerator並呼叫CreateQrCode方法產生二維碼資料。 - 使用
BitmapByteQRCode類,透過GetGraphic方法從 QR 碼資料產生位元組數組。 - 使用
Image.FromStream將位元組陣列轉換為影像。 - 將產生的影像儲存到磁碟。
輸出
如何在 .NET 6 中使用 QRCoder 產生二維碼(與 IronQR 比較):圖 9 - 輸出:使用 QRCoder 函式庫產生的二維碼影像。
IronQR
以下程式碼展示如何使用 IronQR 產生二維碼:
using IronQr;
using IronSoftware.Drawing;
namespace QrCodeWithIronQR
{
public class Program
{
public static void Main()
{
// Prepare a QR Code object
QrCode theQrGen = QrWriter.Write("Awesome IronQR");
// Save QR Code to memory
AnyBitmap myQrImage = theQrGen.Save();
// Save QR Code image to disk
myQrImage.SaveAs("awesome.png");
}
}
}
using IronQr;
using IronSoftware.Drawing;
namespace QrCodeWithIronQR
{
public class Program
{
public static void Main()
{
// Prepare a QR Code object
QrCode theQrGen = QrWriter.Write("Awesome IronQR");
// Save QR Code to memory
AnyBitmap myQrImage = theQrGen.Save();
// Save QR Code image to disk
myQrImage.SaveAs("awesome.png");
}
}
}
Imports IronQr
Imports IronSoftware.Drawing
Namespace QrCodeWithIronQR
Public Class Program
Public Shared Sub Main()
' Prepare a QR Code object
Dim theQrGen As QrCode = QrWriter.Write("Awesome IronQR")
' Save QR Code to memory
Dim myQrImage As AnyBitmap = theQrGen.Save()
' Save QR Code image to disk
myQrImage.SaveAs("awesome.png")
End Sub
End Class
End Namespace
正如你所看到的,程式碼量已經有所不同。 IronQR碼產生所需的程式碼更少。
程式碼解釋
- 使用
QrWriter.Write方法建立二維碼,並傳遞所需的內容。 - 使用
Save方法將二維碼儲存到記憶體中。 - 使用
SaveAs將二維碼影像儲存到磁碟。
輸出
由於我使用的是庫的試用版,因此輸出結果帶有 IronQR 浮水印。 購買正版軟體後,此功能將會移除。
如何在 .NET 6 中使用 QRCoder 產生二維碼(與 IronQR 相比):圖 10 - 輸出:使用 IronQR 產生的二維碼影像
步驟 4:使用 QRCoder 和 IronQR 進行自訂選項
這兩個庫都支援自訂選項。 讓我們來看看有哪些可行的方案。
我們可以在QRCoder中設定二維碼顏色,如下所示:
using QRCoder;
using System.Drawing;
using System.IO;
namespace QRCoderVsIronQR
{
public class Program
{
public static void Main()
{
// Initialize QRCodeGenerator
using (QRCodeGenerator qrGenerator = new QRCodeGenerator())
// Create QR code data
using (QRCodeData qrCodeData = qrGenerator.CreateQrCode("QRCoder Demo 1", QRCodeGenerator.ECCLevel.Q))
// Initialize the QR code with the data
using (BitmapByteQRCode qrCode = new BitmapByteQRCode(qrCodeData))
{
// Generate the QR code's graphic, specifying foreground and background colors
byte[] qrCodeImage = qrCode.GetGraphic(20, Color.Red, Color.Green);
// Convert the byte array to an image format and save it to disk
using (var file = Image.FromStream(new MemoryStream(qrCodeImage)))
{
file.Save("QrCoderDemo1.png");
}
}
}
}
}
using QRCoder;
using System.Drawing;
using System.IO;
namespace QRCoderVsIronQR
{
public class Program
{
public static void Main()
{
// Initialize QRCodeGenerator
using (QRCodeGenerator qrGenerator = new QRCodeGenerator())
// Create QR code data
using (QRCodeData qrCodeData = qrGenerator.CreateQrCode("QRCoder Demo 1", QRCodeGenerator.ECCLevel.Q))
// Initialize the QR code with the data
using (BitmapByteQRCode qrCode = new BitmapByteQRCode(qrCodeData))
{
// Generate the QR code's graphic, specifying foreground and background colors
byte[] qrCodeImage = qrCode.GetGraphic(20, Color.Red, Color.Green);
// Convert the byte array to an image format and save it to disk
using (var file = Image.FromStream(new MemoryStream(qrCodeImage)))
{
file.Save("QrCoderDemo1.png");
}
}
}
}
}
Imports QRCoder
Imports System.Drawing
Imports System.IO
Namespace QRCoderVsIronQR
Public Class Program
Public Shared Sub Main()
' Initialize QRCodeGenerator
Using qrGenerator As New QRCodeGenerator()
' Create QR code data
Using qrCodeData As QRCodeData = qrGenerator.CreateQrCode("QRCoder Demo 1", QRCodeGenerator.ECCLevel.Q)
' Initialize the QR code with the data
Using qrCode As New BitmapByteQRCode(qrCodeData)
' Generate the QR code's graphic, specifying foreground and background colors
Dim qrCodeImage() As Byte = qrCode.GetGraphic(20, Color.Red, Color.Green)
' Convert the byte array to an image format and save it to disk
Using file = Image.FromStream(New MemoryStream(qrCodeImage))
file.Save("QrCoderDemo1.png")
End Using
End Using
End Using
End Using
End Sub
End Class
End Namespace
程式碼解釋
- 初始化
QRCodeGenerator並呼叫CreateQrCode方法產生二維碼資料。 - 使用
BitmapByteQRCode類,透過GetGraphic方法從 QR 碼資料產生位元組數組,並指定前景色(紅色)和背景色(綠色)。 - 使用
Image.FromStream將位元組陣列轉換為影像。 - 將產生的影像儲存到磁碟。
輸出
如何在 .NET 6 中使用 QRCoder 產生二維碼(與 IronQR 相比):圖 11 - 輸出:使用 QRCoder 的自訂功能產生的彩色二維碼影像。
我們可以使用以下程式碼為IronQR二維碼產生設定顏色:
using IronQr;
using IronSoftware.Drawing;
using System.Drawing;
namespace QrCodeWithIronQR
{
public class Program
{
public static void Main()
{
// Set options
QrOptions options = new QrOptions(QrErrorCorrectionLevel.Medium, 20);
// Create QR
QrCode myQr = QrWriter.Write("IronQR Generation Demo 1", options);
// Style options
AnyBitmap logoBmp = new AnyBitmap("logo.png");
QrStyleOptions style = new QrStyleOptions
{
BackgroundColor = Color.Aqua,
Dimensions = 300, // px
Margins = 10, // px
Color = Color.Red,
Logo = new QrLogo
{
Bitmap = logoBmp,
Width = 100,
Height = 100,
CornerRadius = 2
}
};
// Save QR Code
AnyBitmap qrImage = myQr.Save(style);
// Save QR Code to local disk
qrImage.SaveAs("advancedQr.png");
}
}
}
using IronQr;
using IronSoftware.Drawing;
using System.Drawing;
namespace QrCodeWithIronQR
{
public class Program
{
public static void Main()
{
// Set options
QrOptions options = new QrOptions(QrErrorCorrectionLevel.Medium, 20);
// Create QR
QrCode myQr = QrWriter.Write("IronQR Generation Demo 1", options);
// Style options
AnyBitmap logoBmp = new AnyBitmap("logo.png");
QrStyleOptions style = new QrStyleOptions
{
BackgroundColor = Color.Aqua,
Dimensions = 300, // px
Margins = 10, // px
Color = Color.Red,
Logo = new QrLogo
{
Bitmap = logoBmp,
Width = 100,
Height = 100,
CornerRadius = 2
}
};
// Save QR Code
AnyBitmap qrImage = myQr.Save(style);
// Save QR Code to local disk
qrImage.SaveAs("advancedQr.png");
}
}
}
Imports IronQr
Imports IronSoftware.Drawing
Imports System.Drawing
Namespace QrCodeWithIronQR
Public Class Program
Public Shared Sub Main()
' Set options
Dim options As New QrOptions(QrErrorCorrectionLevel.Medium, 20)
' Create QR
Dim myQr As QrCode = QrWriter.Write("IronQR Generation Demo 1", options)
' Style options
Dim logoBmp As New AnyBitmap("logo.png")
Dim style As New QrStyleOptions With {
.BackgroundColor = Color.Aqua,
.Dimensions = 300,
.Margins = 10,
.Color = Color.Red,
.Logo = New QrLogo With {
.Bitmap = logoBmp,
.Width = 100,
.Height = 100,
.CornerRadius = 2
}
}
' Save QR Code
Dim qrImage As AnyBitmap = myQr.Save(style)
' Save QR Code to local disk
qrImage.SaveAs("advancedQr.png")
End Sub
End Class
End Namespace
IronQR提供了許多自訂選項。 在上面的程式中,我們嘗試自訂背景顏色、二維碼顏色和邊距。 我們還可以設定尺寸。
程式碼解釋
- 首先,建立
QrOptions物件來設定糾錯等級。 - 使用
QrWriter.Write方法準備二維碼。 - 使用
QrStyleOptions設定樣式選項。 - 設定二維碼的背景顏色、前景色、邊距、尺寸,甚至可以添加徽標。
- 將二維碼影像儲存到本機磁碟。
輸出
如何在 .NET 6 中使用 QRCoder 產生二維碼(與 IronQR 相比):圖 12 - 輸出:使用 IronQR 及其自訂功能產生的彩色二維碼影像。
授權
QRCoder是一個基於 MIT 許可證的軟體包,並在社區的幫助下開發。 這個軟體包適合小預算項目。 用戶需要等待社群解決問題,或者可以自行修復程式碼並將其推送到 Git 儲存庫,前提是拉取請求已獲得批准。
IronQR需要許可證。 它得到了Iron Software的支援。
可以取得IronQR試用許可證。 需要將取得到的金鑰放入 appSettings.json 檔案中的以下位置:
{
"IronQr.License.LicenseKey": "myKey"
}
結論
選擇 C# QRCoder還是IronQR最終取決於您專案的特定要求。 如果您需要具有大量自訂選項的免費開源二維碼生成庫,那麼 C# QRCoder 可能是您的首選,因為它簡單易用且具有廣泛的自訂選項。
如果您需要企業級庫,它不僅支援產生可自訂選項的二維碼,還支援讀取二維碼,那麼IronQR將是您的最佳選擇。借助 IronQR 程式庫及其諸多優勢,以及Iron Software提供的支援功能,開發人員可以輕鬆無憂地編寫企業級應用程式。
常見問題解答
如何在C#中生成QR碼?
您可以使用像QRCoder和IronQR等庫在C#中生成QR碼。QRCoder是開源的,提供了廣泛的自定義選項,而IronQR則提供了高級功能和跨平台支援,非常適合企業級應用。
在.NET 6項目中使用QRCoder有哪些好處?
QRCoder對於.NET 6項目有益,因為它易於使用,提供了廣泛的自定義選項,並支持多種編碼格式。它還在MIT許可證下免費提供,非常適合預算有限的項目。
如何使用IronQR自定義QR碼?
IronQR允許開發人員通過調整背景色、QR碼色、邊距、尺寸,甚至添加徽標來自定義QR碼的外觀。
IronQR對於跨平台應用有何優勢?
IronQR支持包括桌面(WPF & MAUI)、移動(Xamarin和MAUI)、網頁(Blazor和WebForms)、控制台應用程序以及如Docker、Azure和AWS之類的雲環境,使其非常適合多樣化的開發需求。
在Visual Studio中設置一個QR碼生成項目需要哪些步驟?
要在Visual Studio中設置QR碼生成項目,您需要安裝必要的NuGet包以支持QRCoder或IronQR(取決於您的選擇)。文章提供了分步指導和代碼範例來引導您完成過程。
IronQR如何增強QR碼錯誤校正?
IronQR通過提供詳細的選項來增強QR碼錯誤校正,以確保即使QR碼損壞或部分被遮擋也能準確讀取。這使得它在關鍵應用中非常可靠。
IronQR 提供哪些許可選項?
IronQR需要Iron Software支持的商業許可證,並可選擇獲取試用許可證以進行評估。這確保了企業級項目可以得到健全的支持和功能。
我應該為簡單的QR碼項目選擇哪個庫?
對於簡單的QR碼項目,建議使用QRCoder,因為它的簡單性、易用性以及同為MIT許可證下的開源庫。
IronQR能從多種圖片格式讀取QR碼嗎?
是的,IronQR能從多種圖片格式中讀取QR碼,利用先進的機器學習模型以確保在不同平台上快速和準確地讀取。




