.NET MAUI 條碼掃描器

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

簡介

.NET MAUI (.NET 多平台應用程式介面) 是一個跨平台框架,可以更輕鬆地在單一代碼基礎上創建跨平台應用程式。例如,您可以在一個專案中輕鬆地創建 Microsoft Windows、IOS 和 Android 應用程式。它與其他平台、框架和庫的不同之處在於,它允許開發者社群在項目中使用原生控件,並為他們提供額外的組件。因此,開發者可以使用這些預製組件和服務更快地構建應用程式,而不必從頭開始編寫每一部分的代碼。

在本文中,我將解釋如何在 .NET MAUI Windows 應用程式中掃描 QR 碼或條形碼。

IronBarcode: C# 條碼庫

IronBarcode 是 .NET 程式庫,可以在程式中輕鬆地建立條碼。它提供了一套強大且簡單的預設 API。IronBarcode 程式庫提供從物件層級生成和讀取 QR 碼或其他條碼的功能,您可以通過調用其方法逐一開發,而不用擔心建立條碼物件的細節或先前的知識。此外,它不需要載入任何外部依賴項即可工作,並且可以使用 NuGet 套件管理器輕鬆下載。

IronBarcode 支援多種 QR 和現代條碼格式,如 Code 39、Code 128、PDF417 等許多格式。您還可以將此 .NET 程式庫用作 QR 碼讀取器以及生成器。它將輸入數據解碼成人們可以理解的文本。您可以從圖像、流、GIF 等獲取輸入數據。本文還將解釋如何在 .NET MAUI 應用程式中使用 IronBarcode 程式庫來讀取和掃描 QR 碼。

在 .NET MAUI 應用程式中讀取和掃描條碼的步驟

請按照以下步驟在 .NET MAUI 應用程式中讀取 QR 碼。

先決條件

  1. Visual Studio 2022

  2. 在 Visual Studio 中運行的 .NET MAUI 專案

  3. 穩定的網路用於安裝 IronBarcode 庫以進行條碼掃描

假設您已具備上述先決條件,您可以進行下一步。

安裝 IronBarcode 函式庫

我們可以使用 NuGet 套件主控台安裝 IronBarcode 函式庫。在控制台中輸入以下命令:

Install-Package BarCode

這個控制台命令將在MAUI專案中下載最新版的IronBarcode庫。當然,你可以隨時在NuGet套件庫中檢查並搜尋最新版本。NuGet網站.

前端

作為第一步,我們必須創建前端設計。

我們將創建一個由兩個按鈕、一個文本區和一個圖像框組成的佈局。一個按鈕用於選擇條形碼,另一個則用於複製條形碼的文本。圖像框將顯示所選圖像。

MainPage.xaml 文件中的內容替換為以下內容:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MAUI_Barcode.MainPage">

    <ScrollView>
        <VerticalStackLayout
            Spacing="25"
            Padding="30,0"
            VerticalOptions="Center">

            <Button
                x:Name="ImageSelect"
                Text="Select Barcode"
                SemanticProperties.Hint="Select Image"
                Clicked="SelectBarcode"
                HorizontalOptions="Center" />
            <Image
                x:Name="barcodeImage"
                SemanticProperties.Description="Selected Barcode"
                HeightRequest="200"
                HorizontalOptions="Center" />
            <Editor x:Name="outputText"
                Placeholder="Output text"
                HeightRequest="100"
                WidthRequest="500" />
            <Button
                x:Name="copyText"
                Text="Copy"
                SemanticProperties.Hint="Copy Text"
                WidthRequest="150"
                Clicked="CopyEditorText"
                HorizontalOptions="Center" />

        </VerticalStackLayout>
    </ScrollView>

</ContentPage>
XML

所有元素均垂直堆疊在中心位置。您可以根據自己的喜好進行更改。

使用 IronBarcode 掃描條碼

本節將描述使用 IronBarcode 庫進行條碼掃描的代碼。首先,我們將使用 FilePicker 選擇文件並指定圖像的文件類型。之後,我們將使用 FullPath 屬性檢索圖像文件的路徑,然後將圖像框的源設置為 FullPath 值。最後,我們將在 BarcodeReaderRead 函數中使用 path 值來檢索文本。

private async void SelectBarcode(object sender, EventArgs e)
{
    var images = await FilePicker.Default.PickAsync(new PickOptions
    {
    PickerTitle = "Pick image",
    FileTypes = FilePickerFileType.Images
    });
    var imageSource = images.FullPath.ToString();
    barcodeImage.Source = imageSource;
    var result = BarcodeReader.Read(imageSource).First().Text;
    outputText.Text = result;
}
private async void SelectBarcode(object sender, EventArgs e)
{
    var images = await FilePicker.Default.PickAsync(new PickOptions
    {
    PickerTitle = "Pick image",
    FileTypes = FilePickerFileType.Images
    });
    var imageSource = images.FullPath.ToString();
    barcodeImage.Source = imageSource;
    var result = BarcodeReader.Read(imageSource).First().Text;
    outputText.Text = result;
}
Private Async Sub SelectBarcode(ByVal sender As Object, ByVal e As EventArgs)
	Dim images = Await FilePicker.Default.PickAsync(New PickOptions With {
		.PickerTitle = "Pick image",
		.FileTypes = FilePickerFileType.Images
	})
	Dim imageSource = images.FullPath.ToString()
	barcodeImage.Source = imageSource
	Dim result = BarcodeReader.Read(imageSource).First().Text
	outputText.Text = result
End Sub
VB   C#

以下代碼將用於複製文字編輯器的文本,並向用戶顯示文本已被複製的警告訊息。

private async void CopyEditorText (object sender, EventArgs e)
{
    await Clipboard.SetTextAsync(outputText.Text);
    await DisplayAlert("Success", "Text is copied!", "OK");
}
private async void CopyEditorText (object sender, EventArgs e)
{
    await Clipboard.SetTextAsync(outputText.Text);
    await DisplayAlert("Success", "Text is copied!", "OK");
}
Private Async Sub CopyEditorText(ByVal sender As Object, ByVal e As EventArgs)
	Await Clipboard.SetTextAsync(outputText.Text)
	Await DisplayAlert("Success", "Text is copied!", "OK")
End Sub
VB   C#

您可以獲取此專案來閱讀和掃描代碼在 GitHub().

輸出

執行項目後,您將看到以下輸出。由於尚未選擇,圖像尚未顯示。

.NET MAUI Barcode Scanner Tutorial Using IronBarcode - Figure 1: 未選擇圖片時的輸出

未選擇圖片時的輸出

選擇條碼時,它將顯示如下截圖,並且 QR Code 的輸出文本將顯示在編輯器中。

.NET MAUI Barcode Scanner Tutorial Using IronBarcode - Figure 2: 選擇圖片後的輸出

選擇圖片後的輸出

點擊複製按鈕將觸發前面提到的警示窗口出現。

.NET MAUI Barcode Scanner Tutorial Using IronBarcode - Figure 3: 複製警報

複製警報

結論

在本文中,說明了如何使用 IronBarcode 在 .NET MAUI 應用程式中讀取條碼。IronBarcode 是一個完整的產品,配備了所有必要的工具,可執行條碼操作。作為 QR code 閱讀器,IronBarcode 表現非常出色,能提供預期的精確輸出。此外,它還能讀取複雜的條碼。您還可以使用不同的字體來創建和自訂條碼。請從此處獲取更多關於 IronBarcode 的教程文章。 連結.

IronBarcode 是免費的開發工具。但對於商業用途,你需要購買許可證。你可以通過以下方式獲取有關許可證的信息連結.