如何使用 .NET MAUI 在 C# 中讀取和掃描條碼

.NET MAUI 條碼掃描器

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

介紹

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

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

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 中開啟此控制台,請導覽至Tools > NuGet Package Manager > Package Manager Console 。 然後,在控制台中輸入以下命令:

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 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)
{
    // Use FilePicker to allow the user to select an image file.
    var images = await FilePicker.Default.PickAsync(new PickOptions
    {
        PickerTitle = "Pick image",
        FileTypes = FilePickerFileType.Images
    });

    // Get the full path of the selected image file.
    var imageSource = images.FullPath.ToString();

    // Set the source of the Image view to the selected image's path.
    barcodeImage.Source = imageSource;

    // Use IronBarcode to read the barcode from the image file and get the first result.
    var result = BarcodeReader.Read(imageSource).First().Text;

    // Display the read result in the Editor.
    outputText.Text = result;
}
private async void SelectBarcode(object sender, EventArgs e)
{
    // Use FilePicker to allow the user to select an image file.
    var images = await FilePicker.Default.PickAsync(new PickOptions
    {
        PickerTitle = "Pick image",
        FileTypes = FilePickerFileType.Images
    });

    // Get the full path of the selected image file.
    var imageSource = images.FullPath.ToString();

    // Set the source of the Image view to the selected image's path.
    barcodeImage.Source = imageSource;

    // Use IronBarcode to read the barcode from the image file and get the first result.
    var result = BarcodeReader.Read(imageSource).First().Text;

    // Display the read result in the Editor.
    outputText.Text = result;
}
Private Async Sub SelectBarcode(ByVal sender As Object, ByVal e As EventArgs)
	' Use FilePicker to allow the user to select an image file.
	Dim images = Await FilePicker.Default.PickAsync(New PickOptions With {
		.PickerTitle = "Pick image",
		.FileTypes = FilePickerFileType.Images
	})

	' Get the full path of the selected image file.
	Dim imageSource = images.FullPath.ToString()

	' Set the source of the Image view to the selected image's path.
	barcodeImage.Source = imageSource

	' Use IronBarcode to read the barcode from the image file and get the first result.
	Dim result = BarcodeReader.Read(imageSource).First().Text

	' Display the read result in the Editor.
	outputText.Text = result
End Sub
$vbLabelText   $csharpLabel

下面的程式碼將用於複製文本編輯器中的文本,並向使用者顯示提示訊息,告知文本已被複製。

private async void CopyEditorText(object sender, EventArgs e)
{
    // Copy the text from the Editor to the clipboard.
    await Clipboard.SetTextAsync(outputText.Text);

    // Show a success message to the user.
    await DisplayAlert("Success", "Text is copied!", "OK");
}
private async void CopyEditorText(object sender, EventArgs e)
{
    // Copy the text from the Editor to the clipboard.
    await Clipboard.SetTextAsync(outputText.Text);

    // Show a success message to the user.
    await DisplayAlert("Success", "Text is copied!", "OK");
}
Private Async Sub CopyEditorText(ByVal sender As Object, ByVal e As EventArgs)
	' Copy the text from the Editor to the clipboard.
	Await Clipboard.SetTextAsync(outputText.Text)

	' Show a success message to the user.
	Await DisplayAlert("Success", "Text is copied!", "OK")
End Sub
$vbLabelText   $csharpLabel

You can find the project source code in this article on GitHub.

輸出

項目運行後,您將看到以下輸出。 圖片尚未顯示,因為它尚未被選取。

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

未選擇影像時的輸出

選取條碼後,它將顯示如下截圖所示,二維碼的輸出文字將顯示在編輯器中。

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

選擇影像後的輸出

點擊"複製"按鈕將觸發先前提到的警告視窗出現。

.NET MAUI Barcode Scanner Tutorial Using IronBarcode - Figure 3: 複製提醒

複製提醒

結論

在本文中,我們解釋瞭如何使用 IronBarcode 在 .NET MAUI 應用程式中讀取條碼。 作為一款二維碼閱讀器,IronBarcode 的表現非常出色——它能提供符合預期的準確輸出。 此外,它還能讀取難以辨識的條碼。 您也可以使用不同的字體來建立和自訂條碼。 點擊此連結以獲取更多關於IronBarcode的教學文章。

IronBarcode 必須獲得許可才能用於開發和商業用途。 您可以在這裡找到更多關於許可證方面的資訊。

常見問題解答

如何在.NET MAUI應用程序中掃描QR碼?

您可以通過使用 IronBarcode 庫在.NET MAUI應用程序中掃描QR碼。通過 Visual Studio 中的 NuGet 套件管理器安裝該庫,並使用 BarcodeReader.Read 方法從選定的圖像文件中提取文本。

在 .NET MAUI 項目中安裝 IronBarcode 的過程是什麼?

要在 .NET MAUI 項目中安裝 IronBarcode,請在 Visual Studio 中打開 NuGet 套件管理器控制台,運行命令 Install-Package Barcode 以下載並安裝該庫。

可以使用 IronBarcode 庫讀取哪些條碼格式?

IronBarcode 支持多種條碼格式,包括 QR 碼、Code 39、Code 128、PDF417 等,使您的應用程序具備多功能的條碼閱讀能力。

如何在 .NET MAUI 中設計條碼掃描應用程序的界面?

在 .NET MAUI 中,可以使用 XAML 設計條碼掃描應用程序的界面。通常,它包括一個布局,其中包含按鈕、文本區域和圖像框,這些可以在 MainPage.xaml 文件中定義。

如何在.NET MAUI應用程式中將掃描到的條碼文字複製到剪貼簿?

在 .NET MAUI 應用程序中使用 Clipboard.SetTextAsync 方法將掃描到的條碼文本複製到剪貼板。此方法可以通過按鈕單擊觸發,並顯示一個警報以確認該操作。

在 .NET MAUI 中使用 IronBarcode 可以自定義條碼外觀嗎?

可以,IronBarcode 提供自定義條碼外觀的選項,可以更改字體、顏色和樣式,支持創建視覺上量身定制的條碼。

需要許可才能在商業應用中使用 IronBarcode 嗎?

是的,無論是開發還是商業用途,都需要許可才能使用 IronBarcode。許可詳情和選項可在 IronBarcode 網站上獲得。

我可以在哪裡查找.NET MAUI條碼掃描教程的原始碼?

該.NET MAUI條碼掃描教程的源代碼可在 GitHub 上找到。文章中提供了一個鏈接以方便訪問倉儲庫。

IronBarcode 怎樣增強 .NET MAUI 應用中的條碼掃描功能?

IronBarcode 透過提供一個支持多種條碼格式的強大 API,並與 .NET MAUI 項目無縫集成,增強了.NET MAUI 應用中的條碼掃描,確保高效和準確的條碼閱讀。

Curtis Chau
技術撰稿人

Curtis Chau 擁有電腦科學學士學位(卡爾頓大學),專長於前端開發,精通 Node.js、TypeScript、JavaScript 和 React。Curtis 對製作直覺且美觀的使用者介面充滿熱情,他喜歡使用現代化的架構,並製作結構良好且視覺上吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 也有濃厚的興趣,他喜歡探索整合硬體與軟體的創新方式。在空閒時間,他喜歡玩遊戲和建立 Discord bots,將他對技術的熱愛與創意結合。

準備好開始了嗎?
Nuget 下載 2,070,733 | 版本: 2026.2 剛剛發布