如何從圖像中讀取 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 還是任何其他受支援的環境。
Setting Up IronQR in Your C# Project
在開始掃描二維碼之前,讓我們先來了解如何在 .NET 應用程式中設定 IronQR。
透過 NuGet 安裝
您可以直接從 NuGet 套件管理器控制台安裝 IronQR:
Install-Package IronQR
Install-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 Class
輸出
二維碼掃描常見的應用場景
以下是一些從影像中讀取二維碼的實際應用場景:
*庫存和資產管理:*透過掃描包裝圖像上的二維碼自動識別物品。 雙重認證 (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 碼沒有被遮擋,並考慮必要時使用圖像預處理技術。




