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

.NET MAUI BarCode 掃描器

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 應用程式中整合 IronBarcode,以掃描條碼或 QR 碼。

IronBarcode:C# BarCode程式庫

為了在我們的應用程式中讀取條碼,我們將使用 IronBarcode .NET 程式庫。 它提供了一個強大且簡單的 API 來讀取 BARCODE,讓開發者無需任何麻煩或 BARCODE 領域的專業知識即可進行開發。 可透過 NuGet 套件管理員輕鬆安裝。

IronBarcode 支援讀取多種 BARCODE 格式,包括 Code 39Code 128PDF417 等眾多格式。 您可以讀取多種資料格式,例如圖像檔案、記憶體流和 PDF 檔案。

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

請依照以下步驟,在 .NET MAUI 應用程式中讀取 BARCODE。

先決條件

  1. Visual Studio 2022
  2. Visual Studio 中的 .NET MAUI 專案

安裝 IronBarcode 程式庫

我們可以透過 NuGet 安裝 IronBarcode 程式庫 Package Manager Console。 要在 Visual Studio 中開啟此控制台,請導航至 Tools > NuGet Package Manager > Package Manager Console。 接著,在控制台輸入以下指令:

Install-Package BarCode

此控制台指令將下載 MAUI 專案中 IronBarcode程式庫的最新版本。 此外,您也可以在 NuGet 網站上搜尋 NuGet 套件的最新版本。

前端

第一步將是建立前端設計。 為此,我們將建立一個由兩個按鈕、一個文字輸入區及一個圖片框所組成的版面配置。 將使用一個按鈕來選取BarCode,另一個按鈕則用於複製BarCode的文字內容。 "圖片"方塊將顯示所選的圖片。

請將 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: 未選取圖片時的輸出內容

未選取圖片時的輸出內容

選取 BarCode 後,畫面將如下方截圖所示,且 QR 碼的輸出文字會顯示在編輯器中。

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

選取圖片後的輸出內容

點擊"複製"按鈕將觸發前述的警示視窗出現。

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

複製提示

結論

在本文中,我們說明了如何在 .NET MAUI 應用程式中使用 IronBarcode 讀取條碼。 作為 QR 碼讀取工具,IronBarcode 的表現非常出色——它能提供完全符合預期的輸出結果。 此外,它還能讀取難以辨識的BARCODE。 您也可以使用不同的字型來建立和自訂BarCode。 請透過此連結查看更多關於 IronBarcode 的教學文章。

IronBarcode 必須取得授權,方可進行開發及商業用途。 您可在此處查閱更多授權相關資訊。

常見問題

我怎麼能在.NET MAUI應用程式中掃描QR碼?

您可以使用IronBarcode程式庫在.NET MAUI應用程式中掃描QR碼。透過Visual Studio中的NuGet Package Manager安裝程式庫,並使用BarcodeReader.Read方法從選擇的圖片檔案中提取文字。

在.NET MAUI專案中安裝IronBarcode的過程是什麼?

在.NET MAUI專案中安裝IronBarcode,請在Visual Studio中打開NuGet Package Manager Console並執行Install-Package Barcode命令,以下載並安裝程式庫。

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

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

我怎樣設計.NET MAUI中的條碼掃描器應用程式介面?

在.NET MAUI中,條碼掃描器應用程式的介面可以使用XAML進行設計。通常,它包含具有按鈕、文字區域和圖片框的佈局,這可以在MainPage.xaml檔案中定義。

我怎樣才能在.NET MAUI應用程式中將掃描的條碼文字複製到剪貼板?

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

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

是的,IronBarcode允許通過提供更改字型、顏色和樣式的選項來自訂條碼外觀,使得可以創建視覺上量身定製的條碼。

在商業應用程式中使用IronBarcode需要授權嗎?

是的,開發和商業用途皆需要授權才能使用IronBarcode。授權詳情和選項可以在IronBarcode網站上找到。

我在哪裡可以獲得.NET MAUI條碼掃描器教程的源代碼?

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

IronBarcode如何提升.NET MAUI應用程式中的條碼掃描?

IronBarcode通過提供支援多種條碼格式的強大API來提升.NET MAUI應用程式中的條碼掃描,並且與.NET MAUI專案無縫整合,確保高效且準確的條碼讀取。

IronBarcode支援批次處理條碼嗎?

是的,IronBarcode支援批次處理,允許開發者在一次操作中生成或讀取多個條碼,提升大規模應用的效率。

Curtis Chau
技術撰稿人

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

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

準備好開始了嗎?
Nuget 下載 2,240,258 | 版本: 2026.5 just released
Still Scrolling Icon

還在捲動嗎?

想要快速證明? PM > Install-Package BarCode
執行範例 看您的字串變成 BarCode。