
如何從圖像中讀取 QR 在 C#
QR codes(快速響應碼)無處不在——在產品包裝上、活動門票上、選單上,甚至在名片上。 作為一名.NET開發者,能夠快速可靠地從圖像中讀取QR碼可以為強大的自動化和使用者交互功能開啟大門。 在本指南中,我們將向您展示如何使用IronQR,一個專為.NET構建的高性能QR碼程式庫,以幾行C#程式碼從圖像中讀取QR碼。
無論您是在構建庫存管理軟體、整合雙重身份驗證,還是簡單地從截圖中解碼URLs,IronQR都讓其變得容易。
什麼是IronQR?
IronQR是一個強大的C# QR碼程式庫,旨在為.NET開發者建立一個強大的QR碼讀取和編寫工具。 IronQR被設計為既能生成又能掃描QR碼,並支持從多種圖像格式讀取,使其非常適合用於桌面、網頁或伺服器應用程式。 通過此程式庫,您可以建立準確的QR碼讀取工具來自動化QR碼識別和讀取的整個過程。
主要特色
- 輕鬆讀取和生成QR碼。
- 支持JPEG, PNG, BMP等圖像格式。
- 高速性能和準確的檢測,便於提取QR碼資料。
- 適用於.NET Framework, .NET Core, .NET Standard (2.0+), 和.NET 6/7+項目。
- 提供跨平台支持,無論是在Windows, Linux,還是任何其他受支持的環境,您都可以在自己偏好的應用環境和作業系統中工作。
與開源替代品不同,IronQR專注於企業級穩定性、商業授權和專業支持,使其成為業務關鍵應用程式的優秀選擇。
在您的C#專案中設置IronQR
在您開始掃描QR碼之前,讓我們來了解一下如何在您的.NET應用程式中設置IronQR。
通過NuGet安裝
您可以直接從NuGet Package Manager Console安裝IronQR:
或者,通過在Visual Studio的NuGet GUI中搜索IronQR,點擊"安裝":

新增命名空間和基礎設置
安裝後,請在您的C#文件中加入以下命名空間:
using IronSoftware.Drawing;
using IronQR;Imports IronSoftware.Drawing
Imports IronQR**注意:**IronSoftware.Drawing用於以跨平台的方式處理圖像格式。
從圖像中讀取QR碼
讓我們深入了解如何實際使用IronQR從文件中讀取QR碼。
支持的圖像格式
IronQR支持多種圖像格式,包括:
- PNG
- JPG/JPEG
- BMP
- GIF
- TIFF
這種靈活性允許您處理幾乎任何圖像來源,從相機快照到掃描文件。
基本程式碼範例
讓我們仔細看看如何使用此程式庫輕鬆解碼QR碼。 這是一個最小範例,它從圖像文件中讀取一個文字值為"Hello World!"的QR碼,使用Bitmap類和文件流載入圖像:
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控制台輸出

此程式碼載入QR碼圖像,讀取首個檢測到的QR碼,並列印解碼內容。 簡單而有效。在此基礎上,您可以保存QR碼的值以供進一步使用。
處理多個QR碼
如果您的圖像包含多個QR碼(例如,一張產品標籤紙),您可以提取所有這些QR碼。 對於此範例,我們將通過程式運行以下QR碼圖像:

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輸出

QR碼掃描的常見用例
以下是一些實際應用中從圖像中讀取QR碼變得有價值的場景:
- **庫存和資產管理:**通過掃描包裝圖像上的QR碼來自動化項識別。
- **雙重身份驗證(2FA):**解析2FA設置螢幕上的QR碼以協助安全配置。
- **移動應用整合:**通過掃描共享的QR截圖啟動移動URL或應用特定深層連結。
- **活動票務:**驗證通過電子郵件發送或顯示在螢幕上的票務QR碼。
- **支付網關:**提取嵌入在QR碼中的支付資料,適用於金融技術應用程式。
在所有這些用例中,快速和準確的識別是關鍵——這一點IronQR做得非常好。
故障排除提示
如果您在讀取QR碼時遇到問題,請考慮以下幾點:
圖像質量差
模糊或低解析度的圖像可能難以檢測到QR碼。 盡可能使用高質量的輸入。
QR碼未檢測到
確保圖像不太暗,具有強烈對比,並且QR碼未被遮擋。 嘗試裁剪圖像以聚焦於QR區域。
異常處理
始終將您的QR讀取邏輯包裹於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
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#從圖像中讀取QR碼並不困難。 使用IronQR,您可以僅用幾行程式碼將QR碼掃描整合到.NET應用程式中。 它的簡單性、跨平台相容性和優異的性能使其成為開發者在現代QR啟用系統中工作的首選工具。
如果您希望拓展此功能,請考慮探索:
- QR碼生成以建立自己的可掃碼碼。
- 與 IronBarcode 或 IronOCR 整合以獲得更廣泛的掃描能力。 準備好開始了嗎?
安裝IronQR免費試用版以立即開始,並了解此程式庫如何提升您的QR碼任務。

Curtis Chau擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。
相關文章


