如何在WPF中建立QR Code掃描器
Windows Presentation Foundation (WPF) 是一個.NET框架,用於建立具有XAML定義UI的Windows桌面應用程式。 IronQR直接整合到WPF中,只需幾行C#程式碼即可從使用者選擇的圖像文件中掃描QR Code。
在本指南中,我們將建立一個WPF應用程式,打開文件對話框,載入選擇的圖像,並使用IronQR解碼任何嵌入的QR Code。 該方法支援PNG、JPEG、BMP、GIF、TIFF及其他常見圖像格式。
如何在WPF中掃描QR Code
- 透過NuGet安裝IronQR C#程式庫
- 在XAML中向WPF窗口新增
Button和TextBlock - 使用
OpenFileDialog打開文件對話框,從磁碟中選擇圖像 - 使用
AnyBitmap.FromFile載入圖像並將其包裝在QrImageInput中 - 呼叫
Read,並在TextBlock中顯示解碼後的值
先決條件
- 安裝.NET桌面開發工作負載的Visual Studio 2022
- 目標為.NET 8或更高版本的WPF專案
安裝IronQR
使用Visual Studio中的NuGet套件管理器控制台安裝IronQR程式庫。 導航至工具 > NuGet Package Manager > Package Manager Console並運行:
Install-Package IronQR
或者,在NuGet上搜尋IronQR並安裝最新版本。
WPF窗口佈局
掃描器UI使用TextBlock顯示解碼結果。 向MainWindow.xaml新增以下標記:
<StackPanel Margin="28" VerticalAlignment="Center">
<TextBlock Text="WPF QR Code Scanner" FontSize="20" FontWeight="Bold" Margin="0,0,0,16"/>
<Button x:Name="ScanButton"
Content="Select Image and Scan QR Code"
Click="OnScanButtonClicked"
Padding="12,8"
Margin="0,0,0,16"
HorizontalAlignment="Left"/>
<TextBlock x:Name="ResultLabel"
Text="Select an image to scan."
TextWrapping="Wrap"
FontSize="14"/>
</StackPanel>
<StackPanel Margin="28" VerticalAlignment="Center">
<TextBlock Text="WPF QR Code Scanner" FontSize="20" FontWeight="Bold" Margin="0,0,0,16"/>
<Button x:Name="ScanButton"
Content="Select Image and Scan QR Code"
Click="OnScanButtonClicked"
Padding="12,8"
Margin="0,0,0,16"
HorizontalAlignment="Left"/>
<TextBlock x:Name="ResultLabel"
Text="Select an image to scan."
TextWrapping="Wrap"
FontSize="14"/>
</StackPanel>
範例輸入
使用以下QR Code作為測試圖像。 將其保存到您的裝置中,透過文件對話框選擇並點擊選擇圖像並掃描QR Code。 解碼值應顯示為https://ironsoftware.com。
範例QR Code — 編碼https://ironsoftware.com
使用IronQR掃描QR Code
當點擊按鈕時,OnScanButtonClicked打開文件對話框以選擇圖像。 選擇的文件被載入至ResultLabel。
向OnScanButtonClicked方法:
:path=/static-assets/qr/content-code-examples/get-started/wpf-qr-code-scanner.cs
using IronQr;
using IronSoftware.Drawing;
using Microsoft.Win32;
using System.Windows;
private void OnScanButtonClicked(object sender, RoutedEventArgs e)
{
// Open a file dialog to select a QR code image
var dialog = new OpenFileDialog
{
Title = "Select a QR code image",
Filter = "Image Files|*.png;*.jpg;*.jpeg;*.bmp;*.gif;*.tiff"
};
if (dialog.ShowDialog() != true) return;
var imageSource = dialog.FileName;
// Load the image into IronQR
var inputBmp = AnyBitmap.FromFile(imageSource);
// Load the asset into QrImageInput
QrImageInput imageInput = new QrImageInput(inputBmp);
// Create a QR Reader object
QrReader reader = new QrReader();
// Read the input and get all embedded QR Codes
IEnumerable<QrResult> results = reader.Read(imageInput);
// Display the first result
var firstResult = results.FirstOrDefault();
ResultLabel.Text = firstResult != null
? "Scanned Text: " + firstResult.Value
: "No QR code found in the selected image.";
}
Imports IronQr
Imports IronSoftware.Drawing
Imports Microsoft.Win32
Imports System.Windows
Private Sub OnScanButtonClicked(sender As Object, e As RoutedEventArgs)
' Open a file dialog to select a QR code image
Dim dialog As New OpenFileDialog With {
.Title = "Select a QR code image",
.Filter = "Image Files|*.png;*.jpg;*.jpeg;*.bmp;*.gif;*.tiff"
}
If dialog.ShowDialog() <> True Then Return
Dim imageSource As String = dialog.FileName
' Load the image into IronQR
Dim inputBmp As AnyBitmap = AnyBitmap.FromFile(imageSource)
' Load the asset into QrImageInput
Dim imageInput As New QrImageInput(inputBmp)
' Create a QR Reader object
Dim reader As New QrReader()
' Read the input and get all embedded QR Codes
Dim results As IEnumerable(Of QrResult) = reader.Read(imageInput)
' Display the first result
Dim firstResult As QrResult = results.FirstOrDefault()
ResultLabel.Text = If(firstResult IsNot Nothing, "Scanned Text: " & firstResult.Value, "No QR code found in the selected image.")
End Sub
OpenFileDialog提供原生的Windows文件選擇,過濾至常見圖像格式。 QrReader.Read返回每個檢測到的QR Code包含一個條目的IEnumerable<QrResult>。 當找不到QR Code時,null,避免在沒有有效程式碼的圖像上產生例外。
輸出
在選擇了QR Code圖像並按下掃描按鈕後,解碼值會出現在按鈕下方的TextBlock中。
在WPF窗口中呈現的解碼後QR Code值
下載專案
結論
IronQR以最少的設定整合到WPF應用程式中——單次QrReader.Read呼叫即可處理桌面上的整個解碼管線。若要從單一圖像掃描多個QR Code,請遍歷完整的結果集合,而不是呼叫FirstOrDefault。
相同模式可擴展至通過迴圈遍歷圖像文件目錄來進行批量處理,或通過使用WPF媒體元素從網路攝影機鏡頭流中捕捉幀來進行即時掃描。
要了解更多有關讀取QR Code和可用掃描模式的資訊,請參閱從圖像讀取QR Code和使用掃描模式讀取QR Code指南。
常見問題
WPF QR Code Scanner教程的內容是什麼?
WPF QR Code Scanner教程提供了一步步使用IronQR建置QR碼掃描器的指南。它解釋了如何使用OpenFileDialog進行圖像選擇並使用QrReader.Read在Windows桌面應用程式中解碼QR碼。
IronQR如何促進在WPF應用程式中的QR碼掃描?
IronQR透過提供如QrReader.Read等工具簡化了在WPF應用程式中的QR碼掃描,該工具能有效解碼透過OpenFileDialog選擇的圖像中的QR碼。
建置WPF QR碼掃描器所需的主要元件是什麼?
使用IronQR建置WPF QR碼掃描器的主要元件包括用於選擇影像的OpenFileDialog和用於QR碼解碼的QrReader.Read方法。
IronQR能否解碼不同圖像格式的QR碼?
是的,IronQR能解碼各種圖像格式中的QR碼,使其在WPF應用程式中具有多樣性,因為圖像可能以不同的文件型別呈現。
IronQR能否與現有WPF應用程式整合?
絕對可以,IronQR能與現有WPF應用程式整合,使開發者能在不需全面改造其現有系統的情況下新增QR碼掃描功能。
IronQR為何適合WPF開發?
IronQR適合WPF開發,因其使用便捷、強大的QR碼解碼能力及能無縫整合至Windows桌面應用程式中。
OpenFileDialog如何在WPF QR碼掃描器中提升使用者體驗?
OpenFileDialog提供了一個簡單的使用介面來選擇圖像檔案,之後IronQR可以處理這些檔案並解碼QR碼,從而提升使用者體驗。
QrReader.Read在QR碼掃描過程中的作用是什麼?
QrReader.Read在QR碼掃描過程中至關重要,因為它利用IronQR的先進解碼算法來從所選影像中解碼QR碼。
在WPF應用程式中使用IronQR有什麼先決條件嗎?
要在WPF應用程式中使用IronQR,開發者需對WPF開發有基本的理解,並具備C#的知識。IronQR的直觀API使得各個技術水平的開發者都能輕鬆使用。
IronQR如何確保QR碼解碼的準確性?
IronQR透過其設計用於高效處理各種QR碼複雜性和圖像質量的先進算法來確保QR碼解碼的準確性。

