.NET MAUI 條碼掃描器

查克尼思·賓
查克尼思·賓
2023年2月1日
已更新 2024年12月17日
分享:
This article was translated from English: Does it need improvement?
Translated
View the article in English

介紹

.NET MAUI (.NET 多平台 App UI) 是一個跨平台框架,用於在單一代碼庫中無縫創建跨平台應用程式。 例如,您可以在一個專案中輕鬆創建 Microsoft Windows、iOS 和 Android 應用程式。 它與其他平台、框架和庫的不同之處在於它讓開發者社區在其項目中使用原生控件並為他們提供額外的組件。 因此,開發人員可以使用這些預製的組件和服務來更快地構建應用程序,而無需從頭開始編寫代碼的每一個方面。

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

IronBarcode:C# 條碼庫

要在我們的應用程式中讀取條碼,我們將使用我們的 IronBarcode .NET 程式庫。 它提供了強大而簡單的 API 用於讀取條碼,使得開發時不需要任何麻煩或條碼領域知識。 可以使用 NuGet 套件管理器輕鬆安裝。

IronBarcode 支援多種條碼格式,包括 Code 39Code 128PDF417 等眾多格式。 您可以從多種數據格式讀取,例如圖像文件、記憶體流和PDF。

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

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

先決條件

  1. Visual Studio 2022

  2. Visual Studio 中的 .NET MAUI 專案

安裝 IronBarcode 程式庫

我們可以使用 NuGet Package Manager Console 安裝 IronBarcode 庫。 在 Visual Studio 中開啟此控制台,請導航至工具 > NuGet 套件管理員 > 套件管理員控制台。 然後,在控制台中輸入以下命令:

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

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

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

您可以在這篇文章中於[GitHub](https://github.com/tayyab-create/Read-and-Scan-Barcode-in-MAUI" target="_blank" rel="nofollow noopener noreferrer)找到項目源代碼。

輸出

在執行專案後,您將看到以下輸出。 影像未顯示是因為尚未選擇。

.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: 複製警報

複製警報

結論

在本文中,我們解釋了如何在 .NET MAUI 應用中使用 IronBarcode 讀取條碼。 作為二維碼讀取器,IronBarcode 表現非常出色——它提供了預期的準確輸出。 此外,它可以读取难以识别的条形码。 您也可以使用不同的字體來創建和自定義條碼。 從這個連結獲取更多有關IronBarcode的教程文章。

IronBarcode 必須獲得開發和商業使用的許可。 您可以在此處找到有關授權的更多資訊。

查克尼思·賓
軟體工程師
Chaknith 致力於 IronXL 和 IronBarcode。他在 C# 和 .NET 方面擁有豐富的專業知識,協助改進軟體並支持客戶。他從用戶互動中獲得的洞察力有助於提高產品、文檔和整體體驗。