如何在 WPF 中建立 QR 碼掃描器

This article was translated from English: Does it need improvement?
Translated
View the article in English

Windows Presentation Foundation (WPF) 是一個 .NET Framework,用於建立採用 XAML 定義使用者介面的 Windows 桌面應用程式。 IronQR 可直接整合至 WPF,僅需幾行 C# 程式碼,即可從使用者選定的影像檔案中掃描 QR 碼。

在本指南中,我們將建立一個 WPF 應用程式,該程式會開啟檔案對話方塊、載入選定的圖片,並使用 IronQR 解碼任何嵌入的 QR 碼。 此方法支援 PNG、JPEG、BMP、GIF、TIFF 及其他常見的圖像格式。

先決條件

  1. 已安裝 .NET 桌面開發工作負載的 Visual Studio 2022
  2. 針對 .NET 8 或更高版本的 WPF 專案

安裝 IronQR

請透過 Visual Studio 中的 NuGet 套件管理員控制台安裝 IronQR程式庫。 請前往"工具">"NuGet 套件管理員">"套件管理員控制台",並執行:

Install-Package IronQR

或者,您也可以在 NuGet 上搜尋 IronQR 並安裝最新版本。

WPF 視窗佈局

掃描器使用者介面使用 Button 來觸發檔案對話方塊,並使用 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>
XML

範例輸入

請使用下方的 QR 碼作為測試圖片。 將其儲存至您的裝置,透過檔案對話方塊選取該檔案,然後點擊"選取圖片並掃描 QR 碼"。 解碼後的值應顯示為 https://ironsoftware.com。**

Sample QR code encoding https://ironsoftware.com for testing the WPF QR scanner

QR 碼範例 — 編碼網址:https://ironsoftware.com

使用 IronQR 進行 QR 碼掃描

點擊按鈕後,OnScanButtonClicked 會開啟檔案對話方塊以選擇圖片。 選取的檔案會載入至 AnyBitmap,傳遞至 QrReader.Read,並將第一個解碼後的值寫入 ResultLabel

請將以下 OnScanButtonClicked 方法新增至 MainWindow.xaml.cs

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

OpenFileDialog 提供原生 Windows 檔案選取功能,並篩選常見的圖像格式。 AnyBitmap.FromFile 負責解碼 PNG、JPEG、BMP、GIF 和 TIFF 格式的輸入,而 QrReader.Read 則會傳回一個 IEnumerable<QrResult>,其中包含每個偵測到的 QR 碼對應的一筆記錄。 FirstOrDefault 在未找到 QR 碼時會安全地返回 null,避免在圖片中沒有有效 QR 碼時觸發例外狀況。

輸出

選取 QR 碼圖片並點擊掃描按鈕後,解碼後的值會顯示在按鈕下方的 TextBlock 中。

WPF QR Code Scanner using IronQR — decoded result displayed in the window

在 WPF 視窗中渲染的解碼 QR 碼值

下載專案

點擊此處下載完整的 WpfQrScanner 專案。

結論

IronQR 只需極少設定即可整合至 WPF 應用程式中 —— 單一 QrReader.Read 呼叫即可處理桌面端的整個解碼流程。若要從單一影像掃描多個 QR 碼,請遍歷完整的結果集合,而非呼叫 FirstOrDefault

此模式同樣適用於批次處理(透過迴圈遍歷圖像檔案目錄),或即時掃描(透過 WPF 媒體元件擷取網路攝影機串流的畫面)。

有關讀取 QR 碼及可用掃描模式的更多資訊,請參閱《從圖片讀取 QR 碼》和《使用掃描模式讀取 QR 碼》指南。

常見問題

WPF QR 碼掃描器教學內容為何?

《WPF QR 碼掃描器教學》提供使用 IronQR 建立 QR 碼掃描器的逐步指南。其中說明如何在 Windows 桌面應用程式中,運用 OpenFileDialog 進行圖片選取,並透過 QrReader.Read 來解碼 QR 碼。

IronQR 如何協助在 WPF 應用程式中掃描 QR 碼?

IronQR 透過提供如 QrReader.Read 等工具,簡化了 WPF 應用程式中的 QR 碼掃描流程,該工具能高效解碼透過 OpenFileDialog 選取的圖片中的 QR 碼。

在 WPF 中建置 QR 碼掃描器需要哪些主要元件?

在 WPF 中使用 IronQR 建立 QR 碼掃描器的主要元件,包含用於選取影像的 OpenFileDialog,以及用於 QR 碼解碼的 QrReader.Read 方法。

IronQR 能否解碼來自不同圖像格式的 QR 碼?

是的,IronQR 能夠解碼來自各種圖像格式的 QR 碼,使其在 WPF 應用程式中具有廣泛的適用性,因為這些應用程式中的圖像可能採用不同的檔案類型。

是否可以將 IronQR 整合到現有的 WPF 應用程式中?

沒問題,IronQR 可以與現有的 WPF 應用程式整合,讓開發人員無需大幅修改現有系統,即可為其應用程式新增 QR 碼掃描功能。

IronQR 為何適合用於 WPF 開發?

IronQR 因其易用性、強大的 QR 碼解碼能力,以及能無縫整合至 Windows 桌面應用程式,非常適合用於 WPF 開發。

OpenFileDialog 如何提升 WPF QR 碼掃描器的使用者體驗?

OpenFileDialog 透過提供直觀的介面來選取圖像檔案,進而由 IronQR 進行處理以解碼 QR 碼,從而提升使用者體驗。

在 QR 碼掃描過程中,QrReader.Read 扮演什麼角色?

QrReader.Read 在 QR 碼掃描過程中至關重要,它利用 IronQR 的先進解碼演算法,從選定的圖片中解碼 QR 碼。

在 WPF 應用程式中使用 IronQR 是否有任何先決條件?

若要在 WPF 應用程式中使用 IronQR,開發人員應具備 WPF 開發的基本知識,並熟悉 C#。IronQR 直觀的 API 設計,使其能讓不同技術層級的開發人員輕鬆上手。

IronQR 如何確保 QR 碼解碼的準確性?

IronQR 透過其先進的演算法確保 QR 碼解碼的準確性,這些演算法專為高效處理各種 QR 碼複雜度與影像品質而設計。

Curtis Chau
技術撰稿人

Curtis Chau 擁有卡爾頓大學(Carleton University)的電腦科學學士學位,專精於前端開發,並精通 Node.js、TypeScript、JavaScript 及 React。他熱衷於打造直觀且美觀的用戶介面,喜歡運用現代框架,並創建結構完善、視覺上吸引人的手冊。

除了開發工作之外,Curtis 對物聯網(IoT)抱有濃厚興趣,致力於探索整合硬體與軟體的創新方法。閒暇時,他喜歡玩遊戲和開發 Discord 機器人,將對科技的熱愛與創意相結合。

準備開始了嗎?
Nuget 下載 67,270 | 版本: 2026.5 just released
Still Scrolling Icon

還在往下捲動嗎?

想要快速確認成果嗎? PM > Install-Package IronQR
執行範例 觀看您的 URL 轉為 QR 碼。