IronOCR 开始 Android 设置指南 如何在 .NET MAUI 中对 Android 执行 OCR Curtis Chau 已更新:七月 22, 2025 下载 IronOCR NuGet 下载 DLL 下载 Windows 安装程序 免费试用 法学硕士副本 法学硕士副本 将页面复制为 Markdown 格式,用于 LLMs 在 ChatGPT 中打开 向 ChatGPT 咨询此页面 在双子座打开 向 Gemini 询问此页面 在双子座打开 向 Gemini 询问此页面 打开困惑 向 Perplexity 询问有关此页面的信息 分享 在 Facebook 上分享 分享到 X(Twitter) 在 LinkedIn 上分享 复制链接 电子邮件文章 This article was translated from English: Does it need improvement? Translated View the article in English .NET MAUI(多平台应用程序 UI)是 Xamarin.Forms 框架的演进版本,旨在利用 .NET 创建适用于 Android、iOS、macOS 和 Windows 的跨平台应用程序。 .NET MAUI 旨在简化构建可在多个平台上运行的原生用户界面的过程。 IronOcr.Android软件包为Android带来了OCR支持! 如何在 .NET MAUI 中使用 Android 上的 IronOCR 下载 C# 库以在 Android 设备上执行 OCR 操作 创建一个.NET MAUI App项目 编辑 XAML 文件以显示激活按钮和输出文本 编辑相应的 C# 文件以执行 OCR 下载样本项目以快速开始 IronOCR 安卓软件包 IronOcr.Android 包通过 .NET 跨平台项目在 Android 设备上启用 OCR 功能。 不需要原版 IronOCR 软件包。 安装 IronOcr.Android 软件包 通过 NuGet 安装 安装 IronOcr.Android 软件包 nuget.org/packages/IronOcr.Android/ 创建一个 .NET MAUI 项目 打开Visual Studio并点击"创建一个新项目"。 搜索MAUI,选择.NET MAUI App,然后点击"下一步"。 创建 .NET MAUI 应用程序项目 包含 IronOCR.Android 库 这个库可以通过多种方式添加。 最简单的方法可能是使用NuGet。 在Visual Studio中,右键点击"Dependencies"并选择"管理NuGet Packages ..."。 选择"浏览"选项卡,搜索"IronOcr.Android"。 选择"IronOcr.Android"软件包,然后点击"安装"。 下载 IronOCR.Android 包 为了防止在其他平台上出现问题,修改csproj文件以仅在针对Android平台时包含该包。 为此: 右键单击项目,然后选择"编辑项目文件"。 创建一个新的ItemGroup元素,如下所示: <ItemGroup Condition="$(TargetFramework.Contains('android')) == true"> </ItemGroup> <ItemGroup Condition="$(TargetFramework.Contains('android')) == true"> </ItemGroup> XML 将"IronOcr.Android"PackageReference 移到我们刚刚创建的 ItemGroup 中。 上述步骤将阻止"IronOCR.Android"软件包在例如 iOS 平台上使用(为此,请安装IronOCR.iOS )。 编辑"MainActivity.cs" 通过导航到平台 -> Android 打开"MainActivity.cs"文件。 添加MainActivity方法并调用Initialize方法。 using Android.App; using Android.Content.PM; using Android.Runtime; using Android.OS; using IronOcr; namespace MAUIIronOCRAndroidSample { [Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)] public class MainActivity : MauiAppCompatActivity { public MainActivity() { // Initialize IronTesseract for OCR purposes IronTesseract.Initialize(this); } } } using Android.App; using Android.Content.PM; using Android.Runtime; using Android.OS; using IronOcr; namespace MAUIIronOCRAndroidSample { [Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)] public class MainActivity : MauiAppCompatActivity { public MainActivity() { // Initialize IronTesseract for OCR purposes IronTesseract.Initialize(this); } } } Imports Android.App Imports Android.Content.PM Imports Android.Runtime Imports Android.OS Imports IronOcr Namespace MAUIIronOCRAndroidSample <Activity(Theme := "@style/Maui.SplashTheme", MainLauncher := True, ConfigurationChanges := ConfigChanges.ScreenSize Or ConfigChanges.Orientation Or ConfigChanges.UiMode Or ConfigChanges.ScreenLayout Or ConfigChanges.SmallestScreenSize Or ConfigChanges.Density)> Public Class MainActivity Inherits MauiAppCompatActivity Public Sub New() ' Initialize IronTesseract for OCR purposes IronTesseract.Initialize(Me) End Sub End Class End Namespace $vbLabelText $csharpLabel 编辑"MainPage.xaml" 编辑 XAML 文件,使其显示一个按钮和一个标签,用于显示 OCR 结果。 例如: <?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="MAUIIronOCRAndroidSample.MainPage"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <Button Text="Import File" Clicked="ReadFileOnImport" Grid.Row="0" HorizontalOptions="Center" Margin="20, 20, 20, 10"/> <ScrollView Grid.Row="1" BackgroundColor="LightGray" Padding="10" Margin="10, 10, 10, 30"> <Label x:Name="OutputText"/> </ScrollView> </Grid> </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="MAUIIronOCRAndroidSample.MainPage"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <Button Text="Import File" Clicked="ReadFileOnImport" Grid.Row="0" HorizontalOptions="Center" Margin="20, 20, 20, 10"/> <ScrollView Grid.Row="1" BackgroundColor="LightGray" Padding="10" Margin="10, 10, 10, 30"> <Label x:Name="OutputText"/> </ScrollView> </Grid> </ContentPage> XML 编辑"MainPage.xaml.cs" 首先,创建一个IronTesseract对象实例。 确保IronTesseract在类中初始化一次,如下面的代码所示。 在方法内部实例化它可能无效,并可能导致意想不到的错误。 接下来,使用FilePicker.PickAsync方法选择一个文件,然后从FileResult打开一个读取流。 创建一个新的OcrInput对象,并将图像加载到该对象中。 使用 Tesseract 实例对图像执行 OCR 并检索文本。 最后,将结果文本显示在标签中。 using IronOcr; using Microsoft.Maui.Controls; using System; using System.Diagnostics; using System.IO; using System.Threading.Tasks; namespace MAUIIronOCRAndroidSample { public partial class MainPage : ContentPage { // Initialize IronTesseract once in a class private readonly IronTesseract ocrTesseract = new IronTesseract(); public MainPage() { InitializeComponent(); // Apply License key if required IronOcr.License.LicenseKey = "IRONOCR.MYLICENSE.KEY.1EF01"; } private async void ReadFileOnImport(object sender, EventArgs e) { try { // Configure the file picker var options = new PickOptions { PickerTitle = "Please select a file" }; // Await user's file selection var result = await FilePicker.PickAsync(options); if (result != null) { using var stream = await result.OpenReadAsync(); // Instantiate OcrInput using var ocrInput = new OcrInput(); // Load image stream for OCR processing ocrInput.AddImage(stream); // Perform OCR var ocrResult = ocrTesseract.Read(ocrInput); // Display extracted text OutputText.Text = ocrResult.Text; } } catch (Exception ex) { // Log and handle exceptions Debug.WriteLine(ex); } } } } using IronOcr; using Microsoft.Maui.Controls; using System; using System.Diagnostics; using System.IO; using System.Threading.Tasks; namespace MAUIIronOCRAndroidSample { public partial class MainPage : ContentPage { // Initialize IronTesseract once in a class private readonly IronTesseract ocrTesseract = new IronTesseract(); public MainPage() { InitializeComponent(); // Apply License key if required IronOcr.License.LicenseKey = "IRONOCR.MYLICENSE.KEY.1EF01"; } private async void ReadFileOnImport(object sender, EventArgs e) { try { // Configure the file picker var options = new PickOptions { PickerTitle = "Please select a file" }; // Await user's file selection var result = await FilePicker.PickAsync(options); if (result != null) { using var stream = await result.OpenReadAsync(); // Instantiate OcrInput using var ocrInput = new OcrInput(); // Load image stream for OCR processing ocrInput.AddImage(stream); // Perform OCR var ocrResult = ocrTesseract.Read(ocrInput); // Display extracted text OutputText.Text = ocrResult.Text; } } catch (Exception ex) { // Log and handle exceptions Debug.WriteLine(ex); } } } } Imports IronOcr Imports Microsoft.Maui.Controls Imports System Imports System.Diagnostics Imports System.IO Imports System.Threading.Tasks Namespace MAUIIronOCRAndroidSample Partial Public Class MainPage Inherits ContentPage ' Initialize IronTesseract once in a class Private ReadOnly ocrTesseract As New IronTesseract() Public Sub New() InitializeComponent() ' Apply License key if required IronOcr.License.LicenseKey = "IRONOCR.MYLICENSE.KEY.1EF01" End Sub Private Async Sub ReadFileOnImport(ByVal sender As Object, ByVal e As EventArgs) Try ' Configure the file picker Dim options = New PickOptions With {.PickerTitle = "Please select a file"} ' Await user's file selection Dim result = Await FilePicker.PickAsync(options) If result IsNot Nothing Then Dim stream = Await result.OpenReadAsync() ' Instantiate OcrInput Dim ocrInput As New OcrInput() ' Load image stream for OCR processing ocrInput.AddImage(stream) ' Perform OCR Dim ocrResult = ocrTesseract.Read(ocrInput) ' Display extracted text OutputText.Text = ocrResult.Text End If Catch ex As Exception ' Log and handle exceptions Debug.WriteLine(ex) End Try End Sub End Class End Namespace $vbLabelText $csharpLabel 最后,在.csproj文件中,确保你只为 Android 构建项目。 由于我们添加的软件包仅适用于 Android,因此为所有平台构建项目将会失败。 运行项目 这将向您展示如何运行项目并执行 OCR。 下载.NET MAUI应用程序项目 您可以下载本指南的完整代码。 它作为一个压缩文件提供,您可以在Visual Studio中将其打开为.NET MAUI应用程序项目。 点击此处下载项目。 在 Avalonia 中使用 IronOcr.Android 与 MAUI 类似,IronOcr.Android 可以按照上述相同的设置用于 Avalonia 项目。 如果您想在 iOS 上执行 OCR,请参阅以下文章了解更多信息:"如何在 .NET MAUI 中对 iOS 执行 OCR " 常见问题解答 如何在 .NET MAUI 应用程序中在 Android 上执行 OCR? 要在 Android 上使用 .NET MAUI 执行 OCR,请通过 NuGet 在 Visual Studio 中安装 IronOcr.Android 包。使用 IronTesseract 类处理图像并提取文本。确保通过适当修改 .csproj 文件以目标为 Android。 IronOcr.Android 包的用途是什么? The IronOcr.Android package is specifically designed to bring OCR capabilities to Android devices within .NET MAUI projects. It simplifies the integration of text recognition features in cross-platform applications targeting Android. 如何配置 .NET MAUI 项目的 Android OCR? 通过 NuGet 安装 IronOcr.Android 包来配置您的 .NET MAUI 项目。调整 .csproj 文件以创建一个新的 ItemGroup 元素并将 IronOcr.Android PackageReference 移入其中,以避免其他平台的构建问题。 MainActivity.cs 中需要什么修改来设置 OCR? 在 Platforms -> Android 下的 MainActivity.cs 文件中,添加 MainActivity 方法并调用 IronTesseract 的 Initialize 方法。这为从图像处理中所需的文本 OCR 提供了设置。 如何编辑 MainPage.xaml 以整合 OCR 功能? 编辑 MainPage.xaml 以包含用于导入文件的按钮和显示 OCR 结果的标签。定义必要的 UI 元素以便与 OCR 处理逻辑进行交互并有效管理用户输入。 如果在 OCR 处理过程中遇到错误,我该怎么办? 在您的 OCR 处理代码周围使用 try-catch 块来处理异常。使用 Debug.WriteLine 或其他日志机制记录任何错误,以帮助诊断和解决问题。 IronOcr.Android 包可以在 Avalonia 项目中使用吗? 是的,IronOcr.Android 包可以在 Avalonia 项目中使用,设置过程与 .NET MAUI 相似。请根据需要调整项目配置以启用 OCR 功能。 在何处可以找到使用 .NET MAUI 的 Android OCR 完整示例项目? 可以通过指南中提供的链接下载使用 .NET MAUI 中的 IronOCR 库的 Android OCR 完整示例项目。该项目以压缩文件形式提供,可以在 Visual Studio 中打开。 如何确保 IronOcr.Android 包在多平台项目中仅针对 Android? 要确保 IronOcr.Android 包仅针对 Android,请通过创建 Android 的条件 ItemGroup 并将 IronOcr.Android PackageReference 移入其中来修改 .csproj 文件。这样可以防止在构建其他平台时发生冲突。 Curtis Chau 立即与工程团队聊天 技术作家 Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。 准备开始了吗? Nuget 下载 5,167,857 | Version: 2025.11 刚刚发布 免费 NuGet 下载 总下载量:5,167,857 查看许可证