如何在 .NET MAUI 上對 iOS 進行 OCR
.NET MAUI(多平臺應用程式介面)是 Xamarin.Forms 框架的演進版,旨在使用 .NET 為 Android、iOS、macOS 和 Windows 開發跨平臺的應用程式。 MAUI 旨在簡化構建可在多個平台上運行的原生使用者介面的過程。
IronOcr.iOS 套件為 iOS 提供 OCR 支援!
如何在 .NET MAUI 的 iOS 上使用 IronOCR
- 下載 C# 函式庫以在 iOS 上執行 OCR
- 建立一個 .NET MAUI 應用程式專案
- 編輯 XAML 文件以顯示啟動按鈕和輸出文字
- 編輯相應的 C# 文件以執行 OCR
- 下載範例專案以快速入門
IronOCR iOS 套件
IronOcr.iOS 套件透過 .NET 跨平台專案在 iOS 裝置上啟用 OCR 功能。 不需要 vanilla 版的 IronOCR 套件。
Install-Package IronOcr.iOS
使用 NuGet 安裝
Install-Package IronOcr.iOS
建立一個 .NET MAUI 專案
在多平台部分,選擇 .NET MAUI 應用程式並繼續。

包含 IronOCR.iOS 函式庫
可透過多種方式新增此程式庫。 最簡單的方法可能是使用 NuGet。
- 在 Visual Studio 中,右鍵點擊 "Dependencies > NuGet" 並選擇 "Manage NuGet Packages ... "。
- 選擇 "Browse" 分頁,然後搜尋 "IronOcr.iOS"。
- 選擇 "IronOcr.iOS" 套件並點擊 "Add Package"。

為避免與其他平台問題,修改 csproj 檔案,以便在針對 iOS 平台時才包含該套件。 如此一來:
- 右鍵點擊您專案的 *.csproj 檔案並選擇 "Edit Project File"。
-
建立一個新的
ItemGroup元素如下:<ItemGroup Condition="$(TargetFramework.Contains('ios')) == true"> <PackageReference Include="IronOcr.iOS" Version="YOUR_PACKAGE_VERSION" /> </ItemGroup><ItemGroup Condition="$(TargetFramework.Contains('ios')) == true"> <PackageReference Include="IronOcr.iOS" Version="YOUR_PACKAGE_VERSION" /> </ItemGroup>XML - 將 "IronOcr.iOS" PackageReference 移入我們剛建立的
ItemGroup內。
以上步驟將防止 "IronOcr.iOS" 套件被用於例如安卓平台(為此,請安裝IronOcr.Android)。
編輯"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="MAUIIronOCRiOSSample.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="MAUIIronOCRiOSSample.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 實例對圖片執行 OCR 並返回文字。 最後,將結果文字顯示在標籤中。
當前實現僅限於圖像文件。 該套件目前不支援處理 PDF 文件。 考慮到這一點,任何與 PDF 文件相關的配置應保持停用。
using System;
using IronOcr;
using Microsoft.Maui.Controls;
namespace MAUIIronOCRiOSSample;
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 System;
using IronOcr;
using Microsoft.Maui.Controls;
namespace MAUIIronOCRiOSSample;
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);
}
}
}
Imports System
Imports IronOcr
Imports Microsoft.Maui.Controls
Namespace MAUIIronOCRiOSSample
Partial Public Class MainPage
Inherits ContentPage
' Initialize IronTesseract once in a class
Private ocrTesseract As New IronTesseract()
Public Sub New()
InitializeComponent()
' Apply license key
IronOcr.License.LicenseKey = "IRONOCR-MYLICENSE-KEY-1EF01"
End Sub
Private Async Sub ReadFileOnImport(ByVal sender As Object, ByVal e As EventArgs)
Try
Dim options = New PickOptions With {.PickerTitle = "Please select a file"}
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
ocrInput.LoadImage(stream)
' Perform OCR
Dim ocrResult = ocrTesseract.Read(ocrInput)
OutputText.Text = ocrResult.Text
End If
Catch ex As Exception
' Handle exceptions
System.Diagnostics.Debug.WriteLine(ex)
End Try
End Sub
End Class
End Namespace
最後,將構建目標切換到 iOS 模擬器並運行專案。
執行專案
這將告訴您如何運行專案並執行 OCR。
下載 .NET MAUI 應用程式專案
您可以下載本指南的完整程式碼。它是作為壓縮文件來的,您可以在 Visual Studio 中將其作為 .NET MAUI 應用程式專案打開。
在 Avalonia 中使用 IronOcr.iOS
在 Avalonia 中設置 IronOcr.iOS 與 MAUI 類似,但有一個關鍵區別:除了最新的 .NET SDK 版本,您還需安裝 .NET SDK 8.0.101 才能成功運行 IronOcr.iOS。 之後,IronOcr.iOS 可以用於 Avalonia 專案中,設置方式與上面描述的一樣。
如果您想在安卓上進行 OCR,請瀏覽以下文章了解更多:"如何在 .NET MAUI 的安卓上進行 OCR"
常見問題
如何在 iOS 的 .NET MAUI 應用中整合 OCR 功能?
您可以通過使用 IronOCR.iOS 套件將 OCR 功能整合到 iOS 的 .NET MAUI 應用中。在 Visual Studio 中通過 NuGet 安裝,然後修改您的專案文件以便有條件地包含 iOS 平台的套件。使用 IronTesseract 處理圖像並提取文字。
我可以使用 IronOCR.iOS 套件進行 PDF 文件處理嗎?
不行,IronOCR.iOS 套件目前僅限於處理圖像文件,不支持 PDF 文件。請確保在專案中禁用與 PDF 相關的配置。
using .NET MAUI 為 iOS 應用程式設定 OCR 涉及什麼步驟?
using .NET MAUI 為 iOS 應用程式設定 OCR 涉及通過 NuGet 下載 IronOcr.iOS 套件,修改專案文件以有條件地包含 iOS 平台的套件,並編輯 MainPage.xaml 和 MainPage.xaml.cs 文件來建立使用者介面及處理 OCR。
在 Avalonia 專案中使用 IronOCR 有什麼額外需求?
在 Avalonia 專案中使用 IronOCR,您需要確保安裝了最新的 .NET SDK 版本以及 .NET SDK 8.0.101。此設置類似於 MAUI 但需要此附加的 SDK。
我如何在 .NET MAUI 專案中使用 IronOCR 在圖像上執行 OCR?
在 .NET MAUI 專案中,使用 IronTesseract 對圖像執行 OCR。使用 FilePicker.PickAsync 選擇圖像文件,將其載入到 OcrInput 物件中,然後使用 IronTesseract 讀取圖像並提取文字。
是否有實例專案來使用 .NET MAUI 在 iOS 上實現 OCR?
有,您可以從 Iron Software 網站下載使用 IronOCR.iOS 的 .NET MAUI 範例專案。該範例專案作為壓縮文件提供,您可以在 Visual Studio 中打開以加速開發過程。
IronOCR易於整合到現有專案中嗎?
是的,IronOCR易於整合到現有的C#專案中。憑藉詳盡的文件和支援,開發者可以輕鬆地為他們的應用程式新增OCR功能。
IronOCR是否提供任何圖像預處理功能?
IronOCR包括圖像預處理功能以提高OCR的精確度,例如噪聲減少、旋轉校正和對比度調整。
IronOCR可以用於雲應用程式嗎?
的確,IronOCR可以部署在雲環境中,非常適合需要OCR功能的網頁應用程式和服務。
如何通過IronOCR提高OCR結果的準確性?
為了提高IronOCR的OCR準確性,確保高品質輸入圖片,使用適當的語言包,並利用該程式庫的圖像預處理功能。

