.NET MAUI 條碼掃描器
介紹
.NET MAUI (.NET 多平台應用程式介面)是一個跨平台框架,可以在單一代碼庫中無縫創建跨平台應用程式。 例如,您可以在一個專案中輕鬆創建 Microsoft Windows、iOS 和 Android 應用程式。 它與其他平台、框架和庫的不同之處在於它讓開發者社區在其項目中使用原生控件並為他們提供額外的組件。 因此,開發人員可以使用這些預製的組件和服務來更快地構建應用程序,而無需從頭開始編寫代碼的每一個方面。
在本文中,我們將解釋如何在 .NET MAUI Windows 應用程式中整合 IronBarcode 以掃描條碼或 QR 碼。
如何在.NET MAUI中讀取和掃描條碼
IronBarcode:C# 條碼庫
要在我們的應用程式中讀取條碼,我們將使用我們的 IronBarcode .NET 程式庫。 它提供了強大而簡單的 API 用於讀取條碼,使得開發時不需要任何麻煩或條碼領域知識。 可以使用 NuGet 套件管理器輕鬆安裝。
IronBarcode 支援多種條碼格式,包括 Code 39、Code 128、PDF417 等眾多格式。 您可以從多種數據格式讀取,例如圖像文件、記憶體流和PDF。
在 .NET MAUI 應用程式中讀取條碼的步驟
按照以下步驟在 .NET MAUI 應用程式中讀取條碼。
先決條件
-
Visual Studio 2022
- Visual Studio 中的 .NET MAUI 專案
安裝 IronBarcode 程式庫
我們可以使用 NuGet 套件管理器主控台
安裝 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>
所有元素都在垂直堆疊中,並處於中心位置。 您可以根據您的喜好進行更改。
使用 IronBarcode 進行條碼掃描
此部分將描述使用 IronBarcode 程式庫掃描條碼的代碼。 首先,我們將使用 FilePicker
來選擇文件並指定圖像的文件類型。 之後,我們將使用 FullPath
屬性來檢索圖像文件的路徑,然後將圖像框的源設置為 FullPath
值。 最後,我們將在 BarcodeReader
的 Read
函數中使用 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
以下代碼將用於複製文字編輯器的文本,並向用戶顯示文本已被複製的警告訊息。
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
您可以在本文中找到專案的源代碼 GitHub.
輸出
在執行專案後,您將看到以下輸出。 影像未顯示是因為尚未選擇。

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

選擇圖片後的輸出
點擊複製按鈕將觸發前面提到的警示窗口出現。

複製警報
結論
在本文中,我們解釋了如何在 .NET MAUI 應用中使用 IronBarcode 讀取條碼。 作為二維碼讀取器,IronBarcode 表現非常出色——它提供了預期的準確輸出。 此外,它可以读取难以识别的条形码。 您也可以使用不同的字體來創建和自定義條碼。 獲取更多有關 IronBarcode 的教程文章,從這里开始。連結.
IronBarcode 必須獲得開發和商業使用的許可。 您可以找到有關授權的更多資訊這裡.