如何從圖像中讀取 QR 在 C#
二維碼(快速回應碼)無所不在-產品包裝、活動門票、菜單,甚至名片上都有二維碼。 身為 .NET 開發人員,能夠快速可靠地從影像中讀取二維碼,可以開啟強大的自動化和使用者互動功能。 在本指南中,我們將引導您了解如何使用IronQR (一個專為 .NET 構建的高效能二維碼庫)透過幾行 C# 程式碼從映像中讀取二維碼。
無論您是建立庫存管理軟體、整合雙重認證,還是簡單地從螢幕截圖中解碼 URL,IronQR 都能讓這一切變得輕鬆。
IronQR是什麼?
IronQR是一個功能強大的 C# 二維碼庫,旨在為 .NET 開發人員創建功能強大的二維碼讀取器和寫入器。 IronQR 既可用於產生二維碼,也可用於掃描二維碼,並且支援讀取多種影像格式,因此非常適合在桌面、Web 或伺服器應用程式中使用。 透過這個函式庫,您可以建立精確的二維碼閱讀器工具,實現二維碼辨識和讀取的整個流程自動化。
主要功能
- 輕鬆讀取和產生二維碼。
- 支援 JPEG、PNG、BMP 等影像格式。
- 高速效能和精準偵測,方便擷取二維碼資料。
- 適用於 .NET Framework、.NET Core、.NET Standard (2.0+) 和 .NET 6/7+ 專案。
- 提供跨平台支持,因此您可以在您喜歡的應用程式環境和作業系統中工作,無論是 Windows、Linux 還是任何其他受支援的環境。
在 C# 專案中設定 IronQR
在開始掃描二維碼之前,讓我們先來了解如何在 .NET 應用程式中設定 IronQR。
透過 NuGet 安裝
您可以直接從 NuGet 套件管理器控制台安裝 IronQR:
Install-Package IronQRInstall-Package IronQR'INSTANT VB TODO TASK: The following line uses invalid syntax:
'Install-Package IronQR或者,您也可以在 Visual Studio 中使用 NuGet GUI,搜尋IronQR ,然後按一下"安裝":
新增命名空間和基本設置
安裝完成後,請在 C# 檔案中包含以下命名空間:
using IronSoftware.Drawing;
using IronQR;using IronSoftware.Drawing;
using IronQR;Imports IronSoftware.Drawing
Imports IronQR注意: IronSoftware.Drawing 用於以跨平台的方式處理影像格式。
從影像中讀取二維碼
讓我們深入了解如何使用 IronQR 從檔案中讀取二維碼。
支援的圖片格式
IronQR 支援多種影像格式,包括:
- PNG
- JPG/JPEG
- BMP
- GIF
- 多倫多國際電影節
這種靈活性使您可以處理幾乎任何影像來源,從相機快照到掃描文件。
基本程式碼範例
讓我們仔細看看如何使用這個函式庫輕鬆解碼二維碼。 以下是一個簡單的範例,它使用 Bitmap 類別和檔案流從圖像檔案中讀取文字值為"Hello World!"的單一二維碼:
using IronQr;
using IronSoftware.Drawing;
using System;
class Program
{
static void Main()
{
// Load the image using a file stream
using var stream = File.OpenRead("sample-qr.png");
var bitmapImage = AnyBitmap.FromStream(stream);
QrImageInput qrImageInput = new QrImageInput(bitmapImage );
// Read the QR code
QrReader qrReader = new QrReader();
try
{
// Use the QR read method to read the values within your QR code(s)
IEnumerable<QrResult> results = qrReader.Read(qrImageInput);
// Output the decoded value
foreach (var result in results)
{
Console.WriteLine("QR Code Value: " + result.Value);
}
}
catch (Exception ex)
{
Console.WriteLine("Error reading QR code: " + ex.Message);
}
}
}using IronQr;
using IronSoftware.Drawing;
using System;
class Program
{
static void Main()
{
// Load the image using a file stream
using var stream = File.OpenRead("sample-qr.png");
var bitmapImage = AnyBitmap.FromStream(stream);
QrImageInput qrImageInput = new QrImageInput(bitmapImage );
// Read the QR code
QrReader qrReader = new QrReader();
try
{
// Use the QR read method to read the values within your QR code(s)
IEnumerable<QrResult> results = qrReader.Read(qrImageInput);
// Output the decoded value
foreach (var result in results)
{
Console.WriteLine("QR Code Value: " + result.Value);
}
}
catch (Exception ex)
{
Console.WriteLine("Error reading QR code: " + ex.Message);
}
}
}Imports IronQr
Imports IronSoftware.Drawing
Imports System
Friend Class Program
Shared Sub Main()
' Load the image using a file stream
Dim stream = File.OpenRead("sample-qr.png")
Dim bitmapImage = AnyBitmap.FromStream(stream)
Dim qrImageInput As New QrImageInput(bitmapImage)
' Read the QR code
Dim qrReader As New QrReader()
Try
' Use the QR read method to read the values within your QR code(s)
Dim results As IEnumerable(Of QrResult) = qrReader.Read(qrImageInput)
' Output the decoded value
For Each result In results
Console.WriteLine("QR Code Value: " & result.Value)
Next result
Catch ex As Exception
Console.WriteLine("Error reading QR code: " & ex.Message)
End Try
End Sub
End Class控制台輸出
如何在 C# 中讀取影像中的二維碼:圖 3 - 二維碼值控制台輸出
這段程式碼載入二維碼圖像,讀取偵測到的第一個二維碼,並列印解碼後的內容。 簡單有效。您可以在這裡保存二維碼的值,以備後續使用。
處理多個二維碼
如果您的圖像包含多個二維碼(例如,一張產品標籤),您可以提取所有二維碼。 在這個例子中,我們將透過程式運行以下二維碼圖像:
如何在 C# 中讀取影像中的二維碼:圖 4 - 包含多個二維碼的影像
using IronQr;
using IronSoftware.Drawing;
using System;
class Program
{
static void Main()
{
// Load the image from file
var inputImage = AnyBitmap.FromFile("SampleCodes.png");
QrImageInput qrImageInput = new QrImageInput(inputImage);
// Read the QR code
QrReader qrReader = new QrReader();
IEnumerable<QrResult> results = qrReader.Read(qrImageInput);
// Output the decoded value
foreach (var result in results)
{
Console.WriteLine("QR Code Value: " + result.Value);
}
}
}using IronQr;
using IronSoftware.Drawing;
using System;
class Program
{
static void Main()
{
// Load the image from file
var inputImage = AnyBitmap.FromFile("SampleCodes.png");
QrImageInput qrImageInput = new QrImageInput(inputImage);
// Read the QR code
QrReader qrReader = new QrReader();
IEnumerable<QrResult> results = qrReader.Read(qrImageInput);
// Output the decoded value
foreach (var result in results)
{
Console.WriteLine("QR Code Value: " + result.Value);
}
}
}Imports IronQr
Imports IronSoftware.Drawing
Imports System
Friend Class Program
Shared Sub Main()
' Load the image from file
Dim inputImage = AnyBitmap.FromFile("SampleCodes.png")
Dim qrImageInput As New QrImageInput(inputImage)
' Read the QR code
Dim qrReader As New QrReader()
Dim results As IEnumerable(Of QrResult) = qrReader.Read(qrImageInput)
' Output the decoded value
For Each result In results
Console.WriteLine("QR Code Value: " & result.Value)
Next result
End Sub
End ClassOutput
二維碼掃描常見的應用場景
以下是一些從影像中讀取二維碼的實際應用場景:
*庫存和資產管理*:透過掃描包裝圖像上的二維碼自動識別物品。 雙重認證 (2FA) :解析 2FA 設定畫面上的二維碼,以協助進行安全性設定。 行動應用整合:透過掃描共享的二維碼截圖啟動行動網址或應用專屬深度連結。 活動門票**:驗證透過電子郵件發送或顯示在螢幕上的門票二維碼。 *支付網關:擷取嵌入在二維碼中的支付數據,用於金融科技應用。
在所有這些應用場景中,快速且準確的識別至關重要——IronQR 可以輕鬆應對這種情況。
故障排除技巧
如果您在讀取二維碼時遇到問題,請考慮以下幾點:
影像品質差
模糊或低解析度的影像會使二維碼難以偵測。 盡可能使用高品質的原料。
未偵測到二維碼
確保影像不要太暗,對比度要強,且二維碼沒有被遮蔽。 嘗試裁切影像,使二維碼區域突出顯示。
例外處理
務必將讀取二維碼的邏輯放在 try-catch 區塊中,以便優雅地處理損壞的檔案或意外的格式:
try
{
IEnumerable<QrResult> results = qrReader.Read(qrImageInput);
// Output the decoded value
foreach (var result in results)
{
Console.WriteLine("QR Code Value: " + result.Value);
}
}
catch (Exception ex)
{
Console.WriteLine("Error reading QR code: " + ex.Message);
}try
{
IEnumerable<QrResult> results = qrReader.Read(qrImageInput);
// Output the decoded value
foreach (var result in results)
{
Console.WriteLine("QR Code Value: " + result.Value);
}
}
catch (Exception ex)
{
Console.WriteLine("Error reading QR code: " + ex.Message);
}Try
Dim results As IEnumerable(Of QrResult) = qrReader.Read(qrImageInput)
' Output the decoded value
For Each result In results
Console.WriteLine("QR Code Value: " & result.Value)
Next result
Catch ex As Exception
Console.WriteLine("Error reading QR code: " & ex.Message)
End Try最後的想法
在 C# 中讀取影像中的二維碼並不難。 借助IronQR ,您只需幾行程式碼即可將二維碼掃描功能整合到您的 .NET 應用程式中。 它的簡潔性、跨平台相容性和卓越的性能使其成為開發現代二維碼系統的開發人員的首選工具。
如果您希望擴展此功能,請考慮以下方面:
*產生二維碼,建立您自己的可掃描二維碼。 *與IronBarcode或IronOCR集成,可實現更廣泛的掃描功能。 準備好開始了嗎?
立即安裝 IronQR 免費試用版,開始使用,並了解該程式庫如何提升您的二維碼任務效率。
常見問題解答
如何在 C# 中從影像讀取 QR 代碼?
您可以使用 IronQR 在 C# 中從影像讀取 QR 碼。IronQR 提供了從各種圖像格式(如 PNG、JPG 和 TIFF)中掃描和提取 QR 碼資料的方法,只需最少的代碼。
在 .NET 專案中設定 IronQR 需要哪些步驟?
若要在 .NET 專案中設定 IronQR,請使用 NuGet 套件管理員控制台,執行指令 Install-Package IronQR,或在 Visual Studio 內的 NuGet 套件管理員 GUI 中找到 IronQR,然後按一下「安裝」。
IronQR 如何協助在低品質影像中讀取 QR 碼?
IronQR 的設計可處理不同品質影像的 QR 碼讀取。不過,為了達到最佳效果,請確保圖像有良好的對比度,並將焦點放在 QR 代碼區域。如果偵測失敗,請嘗試改善影像品質或裁切影像。
IronQR 可以從單一影像讀取多個 QR 碼嗎?
是的,IronQR 可以從單一影像偵測並讀取多個 QR 碼。這對於處理具有多個 QR 代碼的文件或標籤非常有用。
在 .NET 應用程式中讀取 QR 碼的常見用例有哪些?
常見的使用案例包括庫存管理、雙因素驗證、行動應用程式整合、活動票務以及付款閘道。IronQR 透過提供快速可靠的 QR 碼讀取功能,為這些應用程式提供便利。
IronQR 如何確保跨平台相容性?
IronQR 支援 .NET Framework、.NET Core、.NET Standard 及 .NET 6/7+,提供 Windows、Linux 及其他環境的跨平台相容性。
如果 IronQR 讀取 QR 代碼失敗,該怎麼辦?
如果 IronQR 無法讀取 QR 碼,請檢查影像品質的清晰度和對比度。確保 QR 碼上沒有障礙物,並考慮裁切以集中在 QR 碼區域。如果問題仍然存在,請確認是否支援檔案格式。
使用 IronQR 讀取 QR 碼時,如何處理異常?
若要處理異常,請將您的 IronQR 程式碼包覆在 try-catch 區塊中。此方法有助於從容處理損毀的檔案或不支援的格式等問題。
使用 IronQR 處理 QR 碼有什麼好處?
IronQR 提供高速效能、支援多種影像格式,並提供企業級的穩定性。它很容易整合到各種 .NET 應用程式中,是尋求高效 QR 碼處理解決方案的開發人員的理想選擇。
如何使用 IronQR 提高 QR 碼檢測準確度?
使用具有良好對比度和焦點的高品質影像,以提高偵測準確度。確保 QR 代碼不被遮蔽,必要時可考慮使用影像預處理技術。








