如何使用 NET MAUI 在 C# 中讀取和掃描 BarCode

.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 library 在 .NET MAUI 應用程式中掃描 QR 碼。透過 Visual Studio 中的 NuGet Package Manager 安裝該函式庫,並使用 BarcodeReader.Read 方法從選取的影像檔案中擷取文字。

在 .NET MAUI 專案中安裝 IronBarcode 的流程為何?

要在 .NET MAUI 專案中安裝 IronBarcode,請在 Visual Studio 中開啟 NuGet 套件管理員控制台,並執行指令 Install-Package Barcode 下載並安裝該函式庫。

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

IronBarcode 支援多種條碼格式,包括 QR 碼、Code 39、Code 128、PDF417 等,讓您的應用程式擁有多樣化的條碼讀取功能。

如何在 .NET MAUI 中為條碼掃描器應用程式設計介面?

在 .NET MAUI 中,可以使用 XAML 設計條碼掃描器應用程式的介面。通常,它包括一個具有按鈕、文字區域和影像方塊的佈局,這些都可以在 MainPage.xaml 檔案中定義。

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

在您的 .NET MAUI 應用程式中使用 Clipboard.SetTextAsync 方法,將掃描的 BarCode 文字複製到剪貼簿。此方法可由按鈕點擊觸發,顯示警示以確認動作。

是否可以在 .NET MAUI 中使用 IronBarcode 自訂條碼外觀?

是的,IronBarcode 允許透過提供改變字型、顏色和樣式的選項來自訂條碼的外觀,從而創建視覺度身訂造的條碼。

在商業應用中使用 IronBarcode 是否需要授權?

是的,使用 IronBarcode 作開發和商業用途都需要授權。授權細節和選項可在 IronBarcode 網站上找到。

在哪裡可以取得 .NET MAUI 條碼掃描器教學的原始碼?

.NET MAUI 條碼掃描器教程的原始碼可在 GitHub 上取得。文章中提供了該資源庫的鏈接,以便於訪問。

IronBarcode 如何增強 .NET MAUI 應用程式中的條碼掃描功能?

IronBarcode 可增強 .NET MAUI 應用程式中的條碼掃描功能,其提供的強大 API 可支援多種條碼格式,並可與 .NET MAUI 專案無縫整合,確保條碼讀取的效率與精確度。

Curtis Chau
技術作家

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。

準備好開始了嗎?
Nuget 下載 1,979,979 | Version: 2025.11 剛發表