如何在.NET Maui中執行OCR

.NET MAUI OCR 使用 IronOCR

This article was translated from English: Does it need improvement?
Translated
View the article in English

介紹

Microsoft 發布了 .NET MAUI (多平台應用程式 UI),這是一個用於使用 .NET Framework 建立跨平台應用程式的框架。 它允許您使用相同的程式碼在 Android、iOS 和 Windows 上執行程式碼,為您節省時間、資源和精力。 .NET MAUI 是開源的。 您可以在GitHub上獲取有範例的 .NET MAUI 專案的源程式碼。

在本指南中,我們將學習如何使用 IronOCR 程式庫在 .NET MAUI 上建立 OCR 處理器應用程式,並提供範例。

IronOCR: .NET OCR 程式庫

IronOCR 是一個 .NET OCR NuGet 程式庫,使開發人員能夠輕鬆地將光學字元識別 (OCR) 功能整合到他們的專案中。 使用 IronOCR,PDF 文件可以被掃描並轉換為可搜尋和可編輯的文字/資料,而不會丟失資料質量。 這使得使用者可以輕鬆從 PDF 文件中找到所需的資訊,並在必要時進行更改或修正。

IronOCR 是可用於任何平台的 Tesseract 二進位檔案中最先進的構建。 它提供了更快的速度、更高的準確性,以及支持所有 Tesseract 版本(從 Tesseract 3 到 Tesseract 5)的原生 DLL/API,僅需一次簡單的安裝/下載。

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 應用程式的先決條件如下:

  1. Visual Studio 2022(最新版本)
  2. .NET 6 或 7
  3. 在 Visual Studio 中安裝的 MAUI 套件
  4. 在 Visual Studio 中運行的 .NET MAUI 專案

安裝 IronOCR

第一步是使用 NuGet Package Manager Console 安裝 IronOCR 程式庫。 通過在解決方案資源管理器上右鍵單擊,開啟 NuGet Package Manager Console,並執行以下命令來安裝 IronOCR 程式庫:

Install-Package IronOcr

前端設計

我們將在本節設計應用程式的前端。 打開 MainPage.xaml 文件。

使用 IronOCR 的 .NET MAUI OCR 教學 - 圖 1:MainPage.xaml

MainPage.xaml

我們指定了一個按鈕,將幫助我們選擇圖像或 PDF 文件進行 OCR。 按鈕的 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" />
XML

在這裡,我們建立了一個名稱為 OCRImageImage 元素。 這個圖像框將顯示所選文件。

<Image
    x:Name="OCRImage"
    SemanticProperties.Description="Selected Image"
    HeightRequest="300"
    HorizontalOptions="Center" />
<Image
    x:Name="OCRImage"
    SemanticProperties.Description="Selected Image"
    HeightRequest="300"
    HorizontalOptions="Center" />
XML

接著,我們建立了一個 Editor 控制項。 它將用於顯示從圖像或 PDF 文件中提取的文字。

<Editor
    x:Name="outputText"
    HorizontalOptions="Center"
    WidthRequest="600"
    HeightRequest="300" />
<Editor
    x:Name="outputText"
    HorizontalOptions="Center"
    WidthRequest="600"
    HeightRequest="300" />
XML

以下是完成的 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>
XML

現在,該編寫 OCR 功能的程式碼了。

使用 IronOCR 的 OCR 程式碼

打開 MainPage.xaml.cs 類文件,並編寫以下函式:

使用 IronOCR 的 .NET MAUI OCR 教學 - 圖 2: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
$vbLabelText   $csharpLabel

讓我們分解上述程式碼:

  • 該程式碼使用 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()
$vbLabelText   $csharpLabel
  • Image 控制項被設置為使用其文件路徑顯示所選圖像。
OCRImage.Source = path;
OCRImage.Source = path;
OCRImage.Source = path
$vbLabelText   $csharpLabel
  • 建立了一個 IronTesseract 物件來執行 OCR。 將所選圖像新增到 OcrInput 物件中。 調用 Read 方法從圖像中提取文字,然後在 Editor 控制項中顯示。
:path=/static-assets/ocr/content-code-examples/get-started/net-maui-ocr-tutorial-9.cs
var ocr = new IronTesseract();
using (var input = new OcrInput())
{
    input.LoadImage(path);
    OcrResult result = ocr.Read(input);
    string text = result.Text;
    outputText.Text = text; 
}
Imports IronTesseract

Dim ocr As New IronTesseract()
Using input As New OcrInput()
    input.LoadImage(path)
    Dim result As OcrResult = ocr.Read(input)
    Dim text As String = result.Text
    outputText.Text = text
End Using
$vbLabelText   $csharpLabel

結果輸出

運行專案後,下面的 UI 出現。當您點擊按鈕時,它會提示您從任何位置選擇一個圖像/PDF。

.NET MAUI OCR Tutorial Using IronOCR - Figure 3: OCR 結果

OCR 結果

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

.NET MAUI OCR Tutorial Using IronOCR - Figure 4: OCR 圖像

OCR 圖像

從結果中可以看出,IronOCR 在處理帶有圖案的複雜圖像方面做得非常出色,顯示出準確的結果。 IronOCR 能夠通過其預訓練模型檢測小細節並選取所需的精確字母。

請注意Running the project in release mode with debugging attached might cause issues. 在這種情況下,您可以將專案發佈為非打包的 .NET MAUI 應用程式,如下方連結所示,以確保應用程式正常運行。

結論

欲了解更多資訊,請參閱此 教程,其中提供有關如何使用 IronOCR 從圖像中讀取文字的其他訊息。

IronOCR 是免費的開發用途。 您可以以相當便宜的價格購買它,起價僅 $999。 在這裡查看價格方案。

常見問題

在應用開發中,.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應用程式,利用其全面的文件和支持來實施高級功能。

Curtis Chau
技術作家

Curtis Chau擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。

除了開發,Curtis對物聯網(IoT)有濃厚的興趣,探索創新的方法來整合硬體和軟體。在空閒時間,他喜歡玩遊戲和建立Discord機器人,結合他對技術的熱愛與創造力。

由...審核
Jeff Fritz
Jeffrey T. Fritz
首席計劃經理 - .NET社區團隊
Jeff還是.NET和Visual Studio團隊的首席計劃經理。他是.NET Conf虛擬會議系列的執行製作人,並主持每週兩次的開發者直播節目'Fritz and Friends',在節目中討論技術並與觀眾一起撰寫程式碼。Jeff撰寫工作坊、演講和內容計劃,為微軟開發者的最大活動如Microsoft Build、Microsoft Ignite、.NET Conf和Microsoft MVP Summit提供內容支援。
準備開始了嗎?
Nuget 下載 6,151,372 | 版本: 2026.7 剛剛發布
Still Scrolling Icon

還在滾動?

想要快速證明? PM > Install-Package IronOcr
執行範例 觀看您的圖像轉變為可搜尋文字。