.NET MAUI 条码扫描器

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 以扫描条形码或二维码。

IronBarcode:C# 条码库

为了在我们的应用程序中读取条形码,我们将使用我们的 IronBarcode for .NET 库。 它为读取条形码提供了一个强大而简单的应用程序接口,无需任何麻烦或条形码领域知识即可进行开发。 可以使用 NuGet 软件包管理器轻松安装。

IronBarcode 支持多种条码格式的读取,包括 Code 39Code 128PDF417 等。 您可以读取各种数据格式,如图像文件、内存流和 PDF。

在 .NET MAUI 应用程序中读取 BarCode 的步骤

请按照以下步骤在 .NET MAUI 应用程序中读取条形码。

先决条件

  1. 视觉工作室 2022

  2. 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>
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
VB   C#

下图所示代码将用于复制文本编辑器的文本,并向用户显示文本已复制的提示信息。

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
VB   C#

您可以在本文中找到项目源代码,网址是 GitHub.

输出

运行项目后,您会看到以下输出。 图像未显示是因为尚未选择。

使用 IronBarcode 的 .NET MAUI 条码扫描器教程 - 图 1:未选择图像时的输出结果

未选择图像时的输出

选择条形码后,就会显示如下截图,编辑器中也会显示 QR 码的输出文本。

.NET MAUI 条形码扫描仪教程使用 IronBarcode - 图 2:选择图像后的输出

选择图像后输出

单击 "复制 "按钮将触发前面提到的警报窗口。

.NET MAUI 条形码扫描器教程使用 IronBarcode - 图 3:复制警告

复制警报

结论

在本文中,我们介绍了如何使用 IronBarcode for .NET 在 .NET MAUI 应用程序中读取条形码。 作为 QR 码阅读器,IronBarcode 的表现非常出色,它能按照预期提供准确的输出。 此外,它还能读取难以读取的 BarCode。 您还可以使用不同的字体来创建和自定义条形码。 获取有关IronBarcode的更多教程文章链接.

IronBarcode 必须获得开发和商业使用许可。 您可以找到有关许可的更多信息这里.