如何為鏈接創建QR碼(C#教程)
1.0 引言
快速響應碼(QR碼)起源於日本汽車產業。 它也被稱為 QR 符號、QR 圖形或 QR 程式碼圖形。 原正弘於 1994 年發明了它,其最初的應用是作為一種快速簡便的工具,用於監控車輛在工廠組裝過程中的進度。 自那時起,二維碼作為條碼替代品的應用在汽車製造業以外的領域也得到了顯著增長。 造成這種情況的原因在於其更快的讀取速度和更大的儲存容量。 除了追蹤和識別產品外,二維碼還可以用於交換和儲存訊息,以用於行銷目的以及社交媒體網站和帳戶。 可以使用相機應用程式掃描應用程式產生的靜態二維碼。
2.0 IronBarcode 功能
借助 IronBarcode 的二維碼庫,建立動態二維碼變得非常簡單。 只要幾行程式碼,這個簡單易用的函式庫就能快速產生二維碼。 IronBarcode 可以產生高品質的二維碼,並且使二維碼掃描器能夠輕鬆讀取自訂程式碼。
- IronBarcode 可以讀取和寫入大多數條碼格式和二維碼標準,包括 UPC A/E、EAN 8/13、Code 39/93/128、ITF、MSI、RSS 14/Expanded、Databar 和 CodaB。
- IronBarcode 可以讀取掃描影像和即時視訊幀,校正旋轉、雜訊、失真和傾斜。 IronBarcode 在建立二維碼時會自動預處理條碼影像,以提高讀取效率和準確性。 它們允許內容編輯,動態二維碼尤其受歡迎。
- IronBarcode 可以在多個核心和執行緒上運行(這對批次伺服器尤其有用)。
- IronBarcode 可以自動定位單頁和多頁文件中的一個或多個條碼。
- IronBarcode 可與 .NET Framework 和 .NET Core 實作搭配使用,並支援 32 位元和 64 位元架構。
- IronBarcode 在 PC 和行動平台上支援控制台、桌面、雲端和 Web 應用程式。
- PDF、JPG、TIFF、GIF、BMP、PNG 和 HTML 只是 IronBarcode 可以產生二維碼圖片的幾種檔案和串流格式。
3.0 建立二維碼影像
3.1 從 Windows/控制台應用程式使用 IronBarcode
本文的後續部分提供了一個示例,以演示我們的庫如何輕鬆生成QR碼。
第一步:建立一個用於產生二維碼的新項目
開啟 Visual Studio,然後按一下"檔案"功能表選項中的"新專案" 。
在隨後出現的對話方塊中選擇控制台應用程式範本後,按一下"下一步"。
在"項目名稱"文字區域中輸入您選擇的任何項目名稱(例如,二維碼產生器),然後在"位置"欄位中輸入新項目的位置。 點擊"下一步"按鈕繼續。
從框架下拉選項中選擇一個 .NET Framework(這裡我們使用的是 .NET 6.0(長期支援)),然後按一下"建立"。
步驟 2. 安裝 IronBarcode 庫
透過在軟體包管理器控制台中輸入以下程式碼來下載所需的 IronBarcode 庫:
Install-Package BarCode
或者,我們可以使用 NuGet 套件管理進行搜尋並下載"IronBarcode"套件,這將列出所有結果,並允許我們選擇需要下載的套件。
步驟 3:產生二維碼影像
IronBarcode 讓我們可以用幾行程式碼建立二維碼。 使用 QRCodeWriter.CreateQrCode 方法建立新的二維碼,如下面的程式碼所示:
using IronBarCode;
// Create a QR code with a specified URL and dimensions,
// and save it as a PNG file.
QRCodeWriter.CreateQrCode("www.google.com", 500, QRCodeWriter.QrErrorCorrectionLevel.Medium, 0).SaveAsPng("NewQR.png");
using IronBarCode;
// Create a QR code with a specified URL and dimensions,
// and save it as a PNG file.
QRCodeWriter.CreateQrCode("www.google.com", 500, QRCodeWriter.QrErrorCorrectionLevel.Medium, 0).SaveAsPng("NewQR.png");
Imports IronBarCode
' Create a QR code with a specified URL and dimensions,
' and save it as a PNG file.
QRCodeWriter.CreateQrCode("www.google.com", 500, QRCodeWriter.QrErrorCorrectionLevel.Medium, 0).SaveAsPng("NewQR.png")
要編碼到程式碼影像中的資料(可能是 URL 或流)是 CreateQrCode 方法的唯一必要參數。 此外,該方法還接受三個可選參數:
- 圖形的預設尺寸為寬 500 像素,高 500 像素。
- 糾錯級別。 IronBarcode 提供低、中、高、最高四個等級的糾錯能力。
CreateQrCode方法預設使用最高等級的糾錯(QRCodeWriter.QrErrorCorrectionLevel.Highest)。 - 二維碼的版本號。 如需查看可接受的變體列表,請造訪此頁面。 如果值為 0(預設值),則方法會根據要編碼的資料使用正確的版本號。
上面的範例使用中等程度的糾錯功能產生 500 x 500 像素的圖形,從而建立自訂二維碼。 接下來,我們可以透過呼叫 SaveAsPng 方法,將產生的二維碼儲存為 PNG 檔案到指定的檔案位置。
接下來,我們將看一個用例範例,其中使用者或企業希望將公司徽標添加到生成的二維碼中。 為了實現這一點,下面的範例程式碼中使用了 QrCodeWriter.CreateQrCodeWithLogo 方法。
using System.Drawing; // Necessary for Color type
// Create a QR code with a logo included.
var QRWithLogo = QRCodeWriter.CreateQrCodeWithLogo("www.google.com", "qrWithlogo.png", 500);
// Customize the color of the QR code using the ChangeBarCodeColor method.
QRWithLogo.ChangeBarCodeColor(Color.DarkRed);
// Save the customized QR code as a new PNG file.
QRWithLogo.SaveAsPng("NewQR_Code.png");
using System.Drawing; // Necessary for Color type
// Create a QR code with a logo included.
var QRWithLogo = QRCodeWriter.CreateQrCodeWithLogo("www.google.com", "qrWithlogo.png", 500);
// Customize the color of the QR code using the ChangeBarCodeColor method.
QRWithLogo.ChangeBarCodeColor(Color.DarkRed);
// Save the customized QR code as a new PNG file.
QRWithLogo.SaveAsPng("NewQR_Code.png");
Imports System.Drawing ' Necessary for Color type
' Create a QR code with a logo included.
Private QRWithLogo = QRCodeWriter.CreateQrCodeWithLogo("www.google.com", "qrWithlogo.png", 500)
' Customize the color of the QR code using the ChangeBarCodeColor method.
QRWithLogo.ChangeBarCodeColor(Color.DarkRed)
' Save the customized QR code as a new PNG file.
QRWithLogo.SaveAsPng("NewQR_Code.png")
在上面的範例中,我們將 URL"www.google.com"的字串值編碼成一個新的二維碼,該二維碼將該網站上的圖像嵌入到指定的檔案位置。
上面的二維碼中包含圖片。 徽標會自動定位到該尺寸,並調整大小以匹配二維碼的方形網格,以便仍可讀取純程式碼。 我們也可以使用 ChangeBarCodeColor 方法自訂 QR 位址條碼的顏色,該方法提供了一系列可在 QR 碼上使用的程式碼顏色。 一種方法使用顏色類別類型,另一種方法則使用 HTML 十六進位顏色表示法,如下所示:
QRWithLogo.ChangeBarCodeColor(System.Drawing.ColorTranslator.FromHtml("#8B0000"));
QRWithLogo.ChangeBarCodeColor(System.Drawing.ColorTranslator.FromHtml("#8B0000"));
QRWithLogo.ChangeBarCodeColor(System.Drawing.ColorTranslator.FromHtml("#8B0000"))
上述程式碼行指定了條碼的深紅色。 然後,按照代碼指示,我們將其儲存為 PNG 檔案。我們也可以將二維碼儲存為其他檔案格式,例如 HTML:
QRWithLogo.SaveAsHtmlFile("test.html");
QRWithLogo.SaveAsHtmlFile("test.html");
QRWithLogo.SaveAsHtmlFile("test.html")
上述原始碼的運行結果如下圖所示。
二維碼可以儲存為其他檔案格式,例如 HTML:
QRWithLogo.SaveAsHtmlFile("test.html");
QRWithLogo.SaveAsHtmlFile("test.html");
QRWithLogo.SaveAsHtmlFile("test.html")
使用 IronBarcode 二維碼產生器網站
IronBarcode 也可以用於 Web 應用程式。 下面提供了 MVC Dot Net core 6.0 範例程式碼。
public IActionResult Index()
{
// Generate a QR code for www.google.com
var barcode = QRCodeWriter.CreateQrCode("www.google.com", 500, QRCodeWriter.QrErrorCorrectionLevel.Medium, 0);
// Save the QR code as a PNG file
barcode.SaveAsPng("Barcode.png");
// Get the file path to the saved QR code
var filePath = Path.Combine(Directory.GetCurrentDirectory(), "Barcode.png");
// Return the file as a physical file result to be downloaded
return PhysicalFile(filePath, "image/png", "Barcode.png");
}
public IActionResult Index()
{
// Generate a QR code for www.google.com
var barcode = QRCodeWriter.CreateQrCode("www.google.com", 500, QRCodeWriter.QrErrorCorrectionLevel.Medium, 0);
// Save the QR code as a PNG file
barcode.SaveAsPng("Barcode.png");
// Get the file path to the saved QR code
var filePath = Path.Combine(Directory.GetCurrentDirectory(), "Barcode.png");
// Return the file as a physical file result to be downloaded
return PhysicalFile(filePath, "image/png", "Barcode.png");
}
Public Function Index() As IActionResult
' Generate a QR code for www.google.com
Dim barcode = QRCodeWriter.CreateQrCode("www.google.com", 500, QRCodeWriter.QrErrorCorrectionLevel.Medium, 0)
' Save the QR code as a PNG file
barcode.SaveAsPng("Barcode.png")
' Get the file path to the saved QR code
Dim filePath = Path.Combine(Directory.GetCurrentDirectory(), "Barcode.png")
' Return the file as a physical file result to be downloaded
Return PhysicalFile(filePath, "image/png", "Barcode.png")
End Function
我們用於 Windows/控制台程式的程式碼與上面提供的程式碼相同。 上述程式碼首先產生二維碼文件,然後再返回該文件。 然後用戶可以將二維碼下載為PNG檔案。我們還可以使其可在行動裝置和線上表單上使用。
想了解更多關於IronBarcode的程式碼教學,請點擊這裡。
4.0 結論
IronBarcode 是建立和識別條碼最有效的程式庫之一。 此外,它也是創建和讀取條碼速度最快的庫之一。 該庫與不同的作業系統相容。
它開發簡單,並支援多種條碼格式。 我們可以更改顏色、線條粗細、高度、條碼文字等等。
您可以在這裡找到許可資訊。 IronBarcode 提供免費的開發者授權版二維碼產生器庫和進階版。 包含一年的免費支援和更新。
用戶還可以從Iron Suite中受益,Iron Suite 是一套包含 5 個專業 ASP.NET core 庫的套件,其中包括IronBarcode 、 IronXL 、 IronPDF等。
常見問題解答
QR 碼在現代應用程序中的意義是什麼?
QR 碼提供了一種快速有效的方式來存儲和交換信息,這使得它們在許多現代應用程序中必不可少,如營銷、產品跟蹤和支付系統。IronBarcode 使得為這些應用程序生成和自定義 QR 碼變得簡單。
如何在 C# 中為一個網路鏈接創建 QR 碼?
您可以使用 IronBarcode 通過調用 QRCodeWriter.CreateQrCode 方法並將 URL 作為參數來為一個網路鏈接創建 QR 碼。自定義 QR 碼外觀並將其保存在 PNG 或 PDF 等格式中。
使用 IronBarcode 生成 QR 碼的優勢是什麼?
IronBarcode 提供了高質量的 QR 碼生成,並具有可自定義的功能,如顏色更改和徽標嵌入。它支持多種條碼格式和文件輸出,使其在不同應用中具有多功能性。
如何將 QR 碼生成集成到 .NET Web 應用中?
要將 QR 碼生成集成到 .NET Web 應用中,可以使用帶有 MVC Dot Net core 6.0 的 IronBarcode。該庫允許您在 Web 應用中動態生成 QR 碼,並支持多種下載格式。
我可以使用該庫自定義 QR 碼的尺寸和錯誤校正級別嗎?
可以,IronBarcode 允許您自定義 QR 碼的尺寸和錯誤校正級別。使用 CreateQrCode 方法並使用特定參數來根據您的需求調整這些功能。
如何在 C# 中添加徽標到 QR 碼中?
要在 C# 中向 QR 碼中添加徽標,請使用 IronBarcode 的 QRCodeWriter.CreateQrCodeWithLogo 方法。此功能允許您在 QR 碼中嵌入徽標,增加品牌知名度的同時保持碼的可讀性。
有哪些 QR 碼生成問題的故障排除提示?
如果您在 QR 碼生成中遇到問題,請確保通過 NuGet 正確安裝了庫,並按照文檔使用所有方法。檢查是否有與您的項目設置或 .NET 版本的兼容性問題。
用於保存通過該庫生成的 QR 碼的文件格式有哪些?
IronBarcode 支持將 QR 碼保存為多種文件格式,包括 PDF、JPG、TIFF、GIF、BMP、PNG 和 HTML,為不同的應用場景提供靈活性。
是否有免費版本的 QR 碼生成庫?
有,IronBarcode 為開發者提供其 QR 碼生成庫的免費版本。此外還有一個包含額外功能、支持和更新的高級版本。

