如何在 .NET MAUI 中在 Android 上執行 OCR
.NET MAUI(多平台應用程式用戶介面)是 Xamarin.Forms 框架的演進,旨在使用 .NET 為 Android、iOS、macOS 和 Windows 創建跨平台應用程式。 .NET MAUI 旨在簡化在多個平台上運行原生用戶界面的建構過程。
IronOcr.Android 套件為 Android 提供了 OCR 支持!
如何在 .NET MAUI 的 Android 上使用 IronOCR
IronOCR Android 套件
IronOcr.Android 套件通過 .NET 跨平台專案在 Android 裝置上啟用 OCR 功能。 不需要vanilla IronOCR包。
PM > Install-Package IronOcr.Android
使用 NuGet 安裝
Install-Package IronOCR.Android
建立一個 .NET MAUI 專案
打開 Visual Studio 並點擊「建立新專案」。 搜索 MAUI,選擇 .NET MAUI 應用程式並點擊“下一步”。
包括 IronOCR.Android 函式庫
此程式庫可以通過多種方式添加。 或許使用 NuGet 是最簡單的方法。
在 Visual Studio 中,右鍵點擊「依賴項」並選擇「管理 NuGet 套件...」。
選擇「瀏覽」標籤並搜尋「IronOcr.Android」。
選擇“IronOcr.Android”套件並點擊“安裝”。
為了避免與其他平台發生問題,在針對Android平台時修改csproj檔案以僅包含該套件。 為了做到這點:
在專案上按右鍵,然後選擇「編輯專案檔案」。
- 建立一個新的 ItemGroup 元素,如下:
<ItemGroup Condition="$(TargetFramework.Contains('android')) == true">
</ItemGroup>
將 "IronOcr.Android" PackageReference 移至我們剛剛建立的 ItemGroup 內。
上述步驟將防止 "IronOcr.Android" 套件被用於例如 iOS 平台(為此目的,請安裝 IronOcr.iOS)。
編輯 "MainActivity.cs"
- 打開 "MainActivity.cs" 檔案,請導航至 Platforms -> Android。
- 添加
MainActivity
方法並調用Initialize
方法。
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()
{
IronTesseract.Initialize(this);
}
}
}
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()
{
IronTesseract.Initialize(this);
}
}
}
編輯 "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>
編輯 "MainPage.xaml.cs"
首先,創建一個 IronTesseract 對象的實例。 請確保在一個類中只初始化一次 IronTesseract,如下面的代碼所示。 在方法中實例化它可能會無效且可能導致意外錯誤。
接下來,使用FilePicker.PickAsync
方法選擇一個檔案,然後從FileResult開啟一個讀取流。 創建一個新的 OcrInput 對象並將圖像加載進去。 使用 tesseract 實例對圖像進行光學字符識別並檢索文本。 最後,在標籤中顯示結果文字。
請注意,當前的實施只支持圖像文件。 此套件尚不支持 PDF 文件。 因此,所有與PDF相關的配置預設都是停用的,且應保持停用狀態。
using IronOcr;
namespace MAUIIronOCRAndroidSample;
public partial class MainPage : ContentPage
{
// Initialize IronTesseract once in a class
private IronTesseract ocrTesseract = new IronTesseract();
public MainPage()
{
InitializeComponent();
// Apply License key
IronOcr.License.LicenseKey = "IRONOCR.MYLICENSE.KEY.1EF01";
}
private async void ReadFileOnImport(object sender, EventArgs e)
{
try
{
var options = new PickOptions
{
PickerTitle = "Please select a file"
};
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
ocrInput.LoadImage(stream);
// Perform OCR
var ocrResult = ocrTesseract.Read(ocrInput);
OutputText.Text = ocrResult.Text;
}
}
catch (Exception ex)
{
// Handle exceptions
System.Diagnostics.Debug.WriteLine(ex);
}
}
}
using IronOcr;
namespace MAUIIronOCRAndroidSample;
public partial class MainPage : ContentPage
{
// Initialize IronTesseract once in a class
private IronTesseract ocrTesseract = new IronTesseract();
public MainPage()
{
InitializeComponent();
// Apply License key
IronOcr.License.LicenseKey = "IRONOCR.MYLICENSE.KEY.1EF01";
}
private async void ReadFileOnImport(object sender, EventArgs e)
{
try
{
var options = new PickOptions
{
PickerTitle = "Please select a file"
};
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
ocrInput.LoadImage(stream);
// Perform OCR
var ocrResult = ocrTesseract.Read(ocrInput);
OutputText.Text = ocrResult.Text;
}
}
catch (Exception ex)
{
// Handle exceptions
System.Diagnostics.Debug.WriteLine(ex);
}
}
}
最後,在.csproj檔案中,請確保您只為Android建構專案。 由於我們添加的套件僅適用於Android,因此為所有平台構建項目將會失敗。
執行專案
這將指導您如何運行項目並執行OCR。

下載 .NET MAUI 應用程式專案
您可以下載本指南的完整代碼。它以壓縮檔的形式提供,您可以在Visual Studio中作為.NET MAUI應用程序項目打開。
使用 IronOcr.Android 在 Avalonia
與 MAUI 類似,IronOcr.Android 可以在 Avalonia 項目中使用,設置方式如上所述。
如果您想在 iOS 上執行 OCR,請瀏覽以下文章以了解更多資訊:「How to Perform OCR on iOS in .NET MAUI」