.NET MAUI OCR 搭配 IronOCR
簡介
微軟發布了 .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 檔案,輕鬆新增更多語言。
與 Tesseract 的比較
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" />
在此,我們建立一個名為 Image 的 OCRImage 元素。 此圖片框將顯示所選的檔案。
<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 會處理該圖片,並在 Editor 控制項中顯示已辨識的文字。 您可以從編輯器控制項中複製文字。
OCR 圖片
從結果來看,IronOCR 在處理帶有圖案的複雜圖像時表現出色,呈現了準確的結果。 IronOCR 能透過其預訓練模型偵測細微細節,並精準選取所需的字母。
<{i:(Running the project in release mode with debugging attached might cause issues. 在這種情況下,您可以將專案發佈為未打包的 .NET MAUI 應用程式(如下方連結所示),以確保應用程式能正常運作。)}>
結論
如需進一步了解,請參閱此教學文章,其中提供了關於如何使用 IronOCR 從圖像中讀取文字的詳細資訊。
IronOCR 供開發用途免費使用。 您只需支付低至 $999 的價格即可購買。 請在此查看定價方案。
常見問題
在應用程式開發中,.NET MAUI 用於什麼用途?
.NET MAUI(多平台應用程式使用者介面)用於透過單一程式碼庫建置跨平台應用程式,讓開發人員能同時支援 Android、iOS 和 Windows 平台。
開發人員如何在 .NET MAUI 應用程式中執行 OCR?
開發人員可透過整合 .NET OCR 程式庫 IronOCR,在 .NET MAUI 應用程式中執行 OCR 功能。IronOCR 能將圖像和 PDF 檔案轉換為可搜尋且可編輯的文字。
在 .NET MAUI 專案中設定 IronOCR 的步驟為何?
要在 .NET MAUI 專案中設定 IronOCR,請透過 NuGet 安裝 IronOCR程式庫,在 Visual Studio 中配置前端,並實作必要的 C# 程式碼,以使用 IronTesseract 物件執行 OCR。
IronOCR 在處理文字時的準確度如何?
IronOCR 在處理文字時提供超過 99% 的高準確度,憑藉其先進的 AI 演算法,使其比 Tesseract 等其他 OCR 解決方案更為可靠。
IronOCR 能否處理多種語言?
是的,IronOCR 支援 125 種國際語言,預設安裝為英文。可透過 NuGet 或下載特定語言的 DLL 檔案來新增其他語言。
在 .NET MAUI 應用程式中,該如何選取要進行 OCR 處理的圖像檔案?
在 .NET MAUI 應用程式中,您可以透過 FilePicker 類別選取圖片檔案進行 OCR,該類別允許使用者從裝置中選擇圖片以進行文字擷取。
在顯示 OCR 結果時,Editor 控制項扮演什麼角色?
.NET MAUI 應用程式中的 Editor 控制項用於顯示由 IronOCR 處理的圖像所提取的文字,為使用者提供檢視 OCR 結果的介面。
使用 IronOCR 進行開發是否會產生費用?
IronOCR 可免費用於開發用途。然而,若用於生產環境則需取得授權,授權價格極具競爭力。
是什麼讓 IronOCR 成為 C# 開發者的首選?
IronOCR 之所以是 C# 開發者的首選,是因為它能與 .NET 應用程式無縫整合,且具備高準確度、高速處理能力以及多語言支援,使其優於許多其他 OCR 程式庫。
開發人員該如何提升其 .NET MAUI OCR 應用程式的功能?
開發人員可透過探索 IronOCR 提供的額外資源,並利用其詳盡的文件與支援服務來實作進階功能,從而強化其 .NET MAUI OCR 應用程式。

