跳至頁尾內容
USING IRONQR

如何從圖像中讀取 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:

Install-Package IronQR

或者,通過在Visual Studio的NuGet GUI中搜索IronQR,點擊"安裝":

如何用C#從圖像中讀取QR碼:圖2 - IronQR NuGet Package Manager截圖

新增命名空間和基礎設置

安裝後,請在您的C#文件中加入以下命名空間:

using IronSoftware.Drawing;
using IronQR;
using IronSoftware.Drawing;
using IronQR;
Imports IronSoftware.Drawing
Imports IronQR
$vbLabelText   $csharpLabel

注意: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);
        }
    }
}
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
$vbLabelText   $csharpLabel

控制台輸出

如何用C#從圖像中讀取QR碼:圖3 - QR碼值控制台輸出

此程式碼載入QR碼圖像,讀取首個檢測到的QR碼,並列印解碼內容。 簡單而有效。在此基礎上,您可以保存QR碼的值以供進一步使用。

處理多個QR碼

如果您的圖像包含多個QR碼(例如,一張產品標籤紙),您可以提取所有這些QR碼。 對於此範例,我們將通過程式運行以下QR碼圖像:

如何用C#從圖像中讀取QR碼:圖4 - 含有多個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);
        }
    }
}
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
$vbLabelText   $csharpLabel

輸出

如何用C#從圖像中讀取QR碼:圖5 - QR碼值輸出

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
{
    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
$vbLabelText   $csharpLabel

最後的想法

用C#從圖像中讀取QR碼並不困難。 使用IronQR,您可以僅用幾行程式碼將QR碼掃描整合到.NET應用程式中。 它的簡單性、跨平台相容性和優異的性能使其成為開發者在現代QR啟用系統中工作的首選工具。

如果您希望拓展此功能,請考慮探索:

安裝IronQR免費試用版以立即開始,並了解此程式庫如何提升您的QR碼任務。

常見問題

如何在C#中從圖像讀取QR碼?

您可以使用IronQR在C#中從圖像讀取QR碼。IronQR提供方法以最少的程式碼從各種圖像格式如PNG、JPG和TIFF掃描和提取QR碼資料。

設置IronQR在.NET專案中所需的步驟是什麼?

要在.NET專案中設置IronQR,請使用NuGet Package Manager Console中的命令Install-Package IronQR,或在Visual Studio內的NuGet Package Manager 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碼未被遮擋,並在必要時考慮使用圖像預處理技術。

Curtis Chau
技術作家

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

除了開發,Curtis對物聯網(IoT)有濃厚的興趣,探索創新的方法來整合硬體和軟體。在空閒時間,他喜歡玩遊戲和建立Discord機器人,結合他對技術的熱愛與創造力。

Iron 支援團隊

我們線上24小時,每週5天。
聊天
電子郵件
給我打電話