IRONSOFTWAREHOME

.NET MAUI 条形码扫描器

Curtis Chau
Curtis Chau
Updated: 2026年6月29日

简介

.NET MAUI (.NET 多平台应用 UI) 是一个跨平台框架,可以在单个代码库中无缝地创建跨平台应用程序。 例如,您可以在一个项目中轻松创建 Microsoft Windows、iOS 和 Android 应用程序。 与其他平台、框架和库的区别在于它允许开发者社区在其项目中使用本地控件,并为他们提供额外的组件。 因此,开发人员可以使用这些预制组件和服务来更快速地构建应用程序,而无需从头编写代码的每个方面。

在本文中,我们将解释如何在 .NET MAUI Windows 应用中集成 IronBarcode 以扫描条形码或二维码。

IronBarcode: C#条码库

为了在我们的应用程序中读取条形码,我们将使用 IronBarcode .NET 库。 它提供了一个强大而简单的 API 用于读取条形码,允许开发时不费事也无需条形码领域知识。 它可以通过 NuGet 包管理器轻松安装。

IronBarcode支持多种条码格式的读取,包括_Code 39_、Code 128、_PDF417_等。 您可以从图像文件、内存流和 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。 然后,在控制台中输入以下命令:

PM > 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值。 最后,我们将在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 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");
}

您可以在本文中的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: 复制提醒

复制提醒

结论

在本文中,我们解释了如何在 .NET MAUI 应用程序中使用 IronBarcode 读取条形码。 作为一个二维码读取器,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 来下载并安装库。

using 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 条形码扫描器教程的源代码?

GitHub 上提供了 .NET MAUI 条形码扫描器教程的源代码。文章中提供了指向存储库的链接,以方便访问。

IronBarcode 如何增强 .NET MAUI 应用程序中的条形码扫描功能?

IronBarcode 通过提供支持多种条形码格式的强大 API 和与 .NET MAUI 项目的无缝集成,增强了 .NET MAUI 应用程序中的条形码扫描功能,确保条形码读取的效率和准确性。

Do I need a license to use IronBarcode in a commercial application?

Yes, IronBarcode requires a license for both development and commercial use. More information on licensing can be found on the IronBarcode licensing page.

Curtis Chau
技术作家

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

...
阅读更多

准备开始了吗?

Nuget Downloads 2,422,100版本:2026.9刚刚发布

免费获取

30天试用密钥 即刻获取。

bullet_checked无需信用卡或创建账户
bullet_test在生产环境中测试
且无水印
bullet_calendar30天完全
功能性产品
bullet_support试用期间提供
24/5技术支持
立即获取您的免费30 天试用密钥
无需信用卡或创建账户
C# 用于 PDF 的 NuGet 库
通过 NuGet 安装

版本: 2026.9

PM > Install-Package BarCode
nuget.org/packages/BarCode/
  1. 在解决方案资源管理器中,右键点击引用,管理 NuGet 包
  2. 选择浏览并搜索 "IronBarcode"
  3. 选择包并安装
C# PDF DLL
下载 DLL

版本: 2026.9

  1. 下载并将 IronBarCode 解压到解决方案目录中的 ~/Libs 等位置
  2. 在 Visual Studio 解决方案资源管理器中,右键单击引用。选择浏览,"IronBarcode.dll"

许可 $999

Key in blue circle

立即获取免费的 30 天试用版密钥

Your trial license will be sent to your email address

无任何限制。100% 解锁。无需信用卡。

bullet_checked无需信用卡或创建账户无任何限制。100% 解锁。无需信用卡。
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried IronPDF
预约您的免费现场演示
Booking Badge

深受全球数百万工程师信赖

Iron Software 的客户徽标
获取您的无义务咨询
填写下面的表格或通过sales@ironsoftware.com
您的资料将始终保密。
深受全球数百万工程师信赖
Iron Software 的客户徽标
立即获取您的免费30 天试用密钥
无需信用卡或创建账户