使用 IronOCR 的 .NET MAUI OCR
介紹
微軟發布了 .NET MAUI(多平台應用程式使用者介面),這是一個使用 .NET Framework 建立跨平台應用程式的框架。 它允許您使用同一套程式碼庫編寫可在 Android、iOS 和 Windows 上運行的程式碼,從而節省您的時間、資源和精力。 .NET MAUI 是開源的。 您可以在GitHub上取得 .NET MAUI 專案的原始碼及範例。
在本操作指南中,我們將學習如何使用 IronOCR 程式庫在 .NET MAUI 上建立 OCR 處理器應用程序,並附有範例。
如何在 .NET MAUI 中執行 OCR
- 下載用於在 .NET MAUI 中執行 OCR 的 C# 庫
- 配置 MAUI 專案的前端
- 使用FilePicker類別傳遞映像的完整路徑
- 呼叫
Read方法對影像執行 OCR 辨識。 - 透過存取Text屬性來取得提取的文字並將其顯示出來。
IronOCR:.NET OCR庫
IronOCR是一個 .NET OCR NuGet 庫,它使開發人員能夠輕鬆地將光學字元辨識 (OCR) 功能整合到他們的專案中。 使用 IronOCR,可以掃描 PDF 文件並將其轉換為可搜尋和可編輯的文字/數據,而不會損失任何數據品質。 這樣一來,使用者可以輕鬆地從 PDF 文件中找到所需的信息,並在必要時進行更改或更正。
IronOCR 是目前適用於所有平台的 Tesseract 二進位檔案的最高版本。 它提供更高的速度、準確性和原生 DLL/API,只需一次簡單的安裝/下載即可支援所有版本的 Tesseract(從 Tesseract 3 到 Tesseract 5)。
IronOCR 的語言支援非常廣泛,使用者可以使用 125 種國際語言。 該工具/DLL 預設安裝了英語語言套件。 不過,您可以透過 NuGet 安裝或下載 DLL 檔案輕鬆新增更多語言。
與超立方體的比較
IronOCR 專為 C# 開發人員設計,可與 .NET 應用程式無縫整合。 相較之下,Tesseract 是一個通用的 OCR 函式庫,需要開發人員編寫自己的包裝器才能將其與 C# 一起使用。 此外,與其他資料庫相比,IronOCR 憑藉其創新的人工智慧演算法,提供了更優異的準確性和速度。
IronOCR 附帶全面的文件和技術支援,即使是新手開發人員也能輕鬆快速上手。
IronOCR 比 Tesseract 準確得多。 事實上,它的準確率超過 99%,而 Tesseract 的準確率只有 70.2% 到 92.9% 左右。 觀看此YouTube 視頻,了解更多關於 IronOCR 和 Tesseract 對比的資訊和支援。
建立 OCR MAUI 應用的步驟
請依照以下步驟,使用 IronOCR 在 .NET MAUI 框架中建立 OCR 應用程式。
先決條件
要在.NET MAUI中建立OCR應用程序,需要滿足以下先決條件:
- Visual Studio 2022(最新版本)
- .NET 6 或 7
- Visual Studio 中已安裝 MAUI 套件
- 一個在 Visual Studio 中執行的 .NET MAUI 專案
安裝 IronOCR
第一步是使用 NuGet 套件管理器控制台安裝 IronOCR 庫。 右鍵點選解決方案資源管理器,開啟 NuGet 套件管理器控制台,然後執行下列命令來安裝 IronOCR 庫:
Install-Package IronOcr
前端設計
本節我們將設計應用程式的前端。 開啟MainPage.xaml檔案。

MainPage.xaml
我們指定一個按鈕,幫助我們選擇要進行 OCR 辨識的影像或 PDF 文件。 按鈕的Clicked屬性設定為執行IOCR函數,我們將在下一節中定義該函數。
<Button
x:Name="OCR"
Text="Click to OCR"
Clicked="IOCR"
HorizontalOptions="Center" /><Button
x:Name="OCR"
Text="Click to OCR"
Clicked="IOCR"
HorizontalOptions="Center" />在這裡,我們建立一個名為OCRImage Image元素。 此圖像框將顯示所選檔案。
<Image
x:Name="OCRImage"
SemanticProperties.Description="Selected Image"
HeightRequest="300"
HorizontalOptions="Center" /><Image
x:Name="OCRImage"
SemanticProperties.Description="Selected Image"
HeightRequest="300"
HorizontalOptions="Center" />接下來,我們建立一個Editor控制項。 它將用於顯示從圖像或 PDF 文件中提取的文字。
<Editor
x:Name="outputText"
HorizontalOptions="Center"
WidthRequest="600"
HeightRequest="300" /><Editor
x:Name="outputText"
HorizontalOptions="Center"
WidthRequest="600"
HeightRequest="300" />以下是完整的 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="IronOCR_MAUI_Test.MainPage">
<ScrollView>
<VerticalStackLayout
Spacing="25"
Padding="30,0"
VerticalOptions="Center">
<Button
x:Name="OCR"
Text="Click to OCR"
Clicked="IOCR"
HorizontalOptions="Center" />
<Image
x:Name="OCRImage"
SemanticProperties.Description="Selected Image"
HeightRequest="300"
HorizontalOptions="Center" />
<Editor
x:Name="outputText"
HorizontalOptions="Center"
WidthRequest="600"
HeightRequest="300" />
</VerticalStackLayout>
</ScrollView>
</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="IronOCR_MAUI_Test.MainPage">
<ScrollView>
<VerticalStackLayout
Spacing="25"
Padding="30,0"
VerticalOptions="Center">
<Button
x:Name="OCR"
Text="Click to OCR"
Clicked="IOCR"
HorizontalOptions="Center" />
<Image
x:Name="OCRImage"
SemanticProperties.Description="Selected Image"
HeightRequest="300"
HorizontalOptions="Center" />
<Editor
x:Name="outputText"
HorizontalOptions="Center"
WidthRequest="600"
HeightRequest="300" />
</VerticalStackLayout>
</ScrollView>
</ContentPage>現在,是時候編寫 OCR 功能的程式碼了。
使用 IronOCR 的 OCR 代碼
開啟MainPage.xaml.cs類別文件,並編寫以下函數:

MainPage.xaml.cs
private async void IOCR(object sender, EventArgs e)
{
// Prompt user to select an image using FilePicker
var images = await FilePicker.Default.PickAsync(new PickOptions
{
PickerTitle = "Pick image",
FileTypes = FilePickerFileType.Images
});
// Get the full path of the selected image
var path = images.FullPath.ToString();
// Display the selected image in the Image control
OCRImage.Source = path;
// Create an IronTesseract object to perform OCR
var ocr = new IronTesseract();
// Perform OCR and extract text from the selected image
using (var input = new OcrInput())
{
input.AddImage(path); // Add image to the OCR input
OcrResult result = ocr.Read(input); // Perform OCR
string text = result.Text; // Extract text
// Display extracted text in the Editor control
outputText.Text = text;
}
}private async void IOCR(object sender, EventArgs e)
{
// Prompt user to select an image using FilePicker
var images = await FilePicker.Default.PickAsync(new PickOptions
{
PickerTitle = "Pick image",
FileTypes = FilePickerFileType.Images
});
// Get the full path of the selected image
var path = images.FullPath.ToString();
// Display the selected image in the Image control
OCRImage.Source = path;
// Create an IronTesseract object to perform OCR
var ocr = new IronTesseract();
// Perform OCR and extract text from the selected image
using (var input = new OcrInput())
{
input.AddImage(path); // Add image to the OCR input
OcrResult result = ocr.Read(input); // Perform OCR
string text = result.Text; // Extract text
// Display extracted text in the Editor control
outputText.Text = text;
}
}Private Async Sub IOCR(ByVal sender As Object, ByVal e As EventArgs)
' Prompt user to select an image using FilePicker
Dim images = Await FilePicker.Default.PickAsync(New PickOptions With {
.PickerTitle = "Pick image",
.FileTypes = FilePickerFileType.Images
})
' Get the full path of the selected image
Dim path = images.FullPath.ToString()
' Display the selected image in the Image control
OCRImage.Source = path
' Create an IronTesseract object to perform OCR
Dim ocr = New IronTesseract()
' Perform OCR and extract text from the selected image
Using input = New OcrInput()
input.AddImage(path) ' Add image to the OCR input
Dim result As OcrResult = ocr.Read(input) ' Perform OCR
Dim text As String = result.Text ' Extract text
' Display extracted text in the Editor control
outputText.Text = text
End Using
End Sub讓我們來分析一下上面的程式碼:
- 程式碼使用
FilePicker允許使用者從裝置中選擇映像檔。文件選擇器配置為僅允許選擇影像檔案。
var images = await FilePicker.Default.PickAsync(new PickOptions
{
PickerTitle = "Pick image",
FileTypes = FilePickerFileType.Images
});
var path = images.FullPath.ToString();var images = await FilePicker.Default.PickAsync(new PickOptions
{
PickerTitle = "Pick image",
FileTypes = FilePickerFileType.Images
});
var path = images.FullPath.ToString();Dim images = Await FilePicker.Default.PickAsync(New PickOptions With {
.PickerTitle = "Pick image",
.FileTypes = FilePickerFileType.Images
})
Dim path = images.FullPath.ToString()Image控制項設定為使用檔案路徑顯示選定的影像。
OCRImage.Source = path;OCRImage.Source = path;OCRImage.Source = path- 建立一個
IronTesseract物件來執行 OCR。 將選定的影像加入OcrInput物件中。 呼叫Read方法從圖像中提取文本,然後在Editor控制項中顯示該文本。
var ocr = new IronTesseract();
using (var input = new OcrInput())
{
input.AddImage(path);
OcrResult result = ocr.Read(input);
string text = result.Text;
outputText.Text = text;
}var ocr = new IronTesseract();
using (var input = new OcrInput())
{
input.AddImage(path);
OcrResult result = ocr.Read(input);
string text = result.Text;
outputText.Text = text;
}Dim ocr = New IronTesseract()
Using input = New OcrInput()
input.AddImage(path)
Dim result As OcrResult = ocr.Read(input)
Dim text As String = result.Text
outputText.Text = text
End Using輸出
項目運行後,將顯示以下使用者介面。點擊按鈕後,系統會提示您從任何位置選擇影像/PDF 檔案。

OCR 輸出
選擇圖像後,IronOCR 會處理圖像並在編輯器控制項中顯示辨識出的單字。 您可以從編輯器控制項複製文字。

OCR影像
結果表明,IronOCR 在處理具有圖案的複雜影像方面表現出色,結果準確無誤。 IronOCR 能夠利用其預訓練模型檢測細微之處並選擇所需的確切字母。
<{i:(在發布模式下運行項目並附加偵錯功能可能會導致問題。 在這種情況下,您可以將專案發佈為未打包的 .NET MAUI 應用程式(如下連結所示),以確保應用程式正常運作。
結論
如需進一步閱讀,請參閱本教程,其中提供了有關如何使用 IronOCR從圖像中讀取文字的更多資訊。
IronOCR 可免費用於開發目的。 您可以以非常低的價格購買,起價僅為$799 。 點此查看定價方案。
常見問題解答
.NET MAUI 在應用開發中是用來做什麼的?
.NET MAUI (多平台應用 UI) 用於構建跨平台應用程式,使用單一代碼庫讓開發人員能夠針對 Android、iOS 和 Windows 平台進行開發。
開發人員如何在 .NET MAUI 應用程式中執行 OCR?
開發人員可以通過集成 IronOCR 這個 .NET OCR 庫來在 .NET MAUI 應用程式中執行 OCR。IronOCR 允許將圖像和 PDF 轉換為可搜索和可編輯的文本。
在 .NET MAUI 專案中設置 IronOCR 的步驟是什麼?
要在 .NET MAUI 專案中設置 IronOCR,請通過 NuGet 安裝 IronOCR 庫,在 Visual Studio 中配置您的前端,並實施必要的 C# 代碼以使用 IronTesseract 對象執行 OCR。
IronOCR 在處理文本時的精確度如何?
IronOCR 在處理文本時提供了超過 99% 的高精確度,由於其先進的 AI 演算法,比其他 OCR 解決方案如 Tesseract 更可靠。
IronOCR 能夠處理多種語言嗎?
是的,IronOCR 支持 125 種國際語言,默認安裝英語。可以通過 NuGet 或下載特定語言的 DLL 添加其他語言。
如何在 .NET MAUI 應用程式中選擇用於 OCR 的圖像文件?
在 .NET MAUI 應用程式中,可以使用 FilePicker 類選擇圖像文件進行 OCR,這允許用戶從其設備中選擇圖像以進行文本提取。
Editor 控件在顯示 OCR 結果中的角色是什麼?
在 .NET MAUI 應用程式中,Editor 控件用於顯示由 IronOCR 處理的圖像提取的文本,為用戶提供查看 OCR 結果的介面。
使用 IronOCR 進行開發是否會產生費用?
IronOCR 開放免費使用於開發目的。然而,生產使用需要取得許可證,可以以具有競爭力的價格獲得。
什麼使得IronOCR成為C#開發者的首選?
IronOCR 是 C# 開發人員的首選,因為它與 .NET 應用程式無縫集成,具有高精確度、快速和多語言支持,比許多其他 OCR 庫優越。
開發人員如何增強其 .NET MAUI OCR 應用程式的功能?
開發人員可以藉由探索 IronOCR 提供的額外資源來增強其 .NET MAUI OCR 應用程式,利用其全面的文件和支持來實施高級功能。







