如何在.NET MAUI應用程式中使用文件掃描SDK
隨著移動技術的興起,像Scanbot SDK和Native SDK這樣的文件掃描應用程式,已成為個人和企業不可或缺的工具。 在本教程中,我們將探討如何使用最新版本的.NET Multi-platform App UI (MAUI)和功能強大的IronOCR OCR(光學字元識別)程式庫來建立文件掃描應用程式。 .NET MAUI讓跨平台移動應用的建立變得簡單,確保可無縫部署到像Android等裝置上。 在本教程結束時,您將能夠開發您自己的文件掃描SDK應用程式,可以輕鬆從圖像和掃描文件中提取文字。
如何在.NET MAUI應用程式中使用文件掃描SDK
- 安裝IronOCR C#程式庫以使用文件掃描SDK。
- 使用必要的控制項設計.NET MAUI表單。
- 使用MediaPicker.CapturePhotoAsync方法捕捉照片。
- 將捕捉的照片轉換為Stream。
- 將流傳遞給OcrInput LoadImage方法。
- 使用IronTesseract的Read方法進行OCR。
- 使用OcrResult的Text屬性顯示文件文字。
IronOCR - The C# OCR Library
IronOCR是由Iron Software, LLC開發的先進光學字元識別(OCR)軟體,旨在準確且高效地將圖像和掃描文件轉換為可編輯文字。 OCR技術徹底改變了企業處理文件的方式,使從掃描文件、PDF和圖像等各種來源中提取有價值的資訊變得更加簡單。
IronOCR在OCR解決方案中脫穎而出的是其先進的功能、穩健的性能和易於整合的能力。 無論您是希望將OCR功能整合到應用程式中的開發者,或者是希望優化文件管理流程的企業,IronOCR都提供了全面的解決方案。
IronOCR的主要功能
- 高準確性: IronOCR 使用尖端算法和機器學習技術在文字識別中達到卓越的準確性。 它可以準確地從複雜文件中提取文字,包括解析度低或掃描質量不佳的圖像。
- 多語言支持:IronOCR支持超過125種語言的文字識別,非常適合在多語言環境中運營的企業。
- 圖像預處理: IronOCR提供各種圖像預處理能力,如噪音減少、對比度調整和去斜處理,以提高準確性。 這些技術改善了OCR的結果,尤其是針對變形或不完美的圖像。
- 支持各種文件格式: IronOCR支持包括TIFF、JPEG、PNG和PDF在內的各種文件格式,確保與不同的文件來源相容。
- 自訂選項:開發人員可以自訂IronOCR的行為以滿足特定需求,提供識別參數和工作流程整合的靈活性。
- 快速且可擴展: IronOCR針對性能進行了優化,幾乎瞬間從大量文件中提取文字。 其可擴展架構確保無論文件量多大都能無縫運行。
- 與.NET應用程式整合: IronOCR與.NET應用程式無縫整合,提供易於使用的API以新增OCR功能。 這簡化了開發過程並加快了OCR功能應用程式的上市時間。
- 文件分類和資料提取:除了基本的文字識別,IronOCR還提供文件分類和資料提取的高級功能,識別特定的資料字段,如姓名、地址或發票號碼。
必要條件
- C#程式設計的基本知識。
- 安裝Visual Studio 2022且載有.NET MAUI工作負載。
- 通過NuGet套件管理器安裝IronOCR程式庫。
1. 設置您的.NET MAUI專案
- 打開Visual Studio 2022並建立一個新的.NET MAUI App專案。

- 選擇合適的專案名稱並配置專案設置。

- 確保您已安裝了針對目標平台裝置開發所需的Android和iOS SDK。
2. 安裝IronOCR程式庫
- 在Visual Studio中右鍵點擊您解決方案。
- 選擇"管理解決方案的NuGet套件",並在瀏覽選項卡中搜尋"IronOCR"。

- 將IronOCR程式庫安裝到您的專案中。
3. 設計UI
讓我們從設計我們的MainPage.xaml的佈局開始。 我們將建立一個簡單的佈局,裡面有一個用於顯示捕獲照片的影像控制項,一個用來拍照的Capture按鈕以及一個用於顯示提取文字的Label。
這是MainPage.xaml的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"
xmlns:d="http://schemas.microsoft.com/dotnet/2021/maui/design"
x:Class="DocumentScanner.MainPage">
<ScrollView>
<VerticalStackLayout Padding="30,0" Spacing="25">
<Image Source="dotnet_bot.png"
HeightRequest="185"
Aspect="AspectFit"
SemanticProperties.Description="dot net bot in a race car number eight" />
<Label Text="Welcome to .NET MAUI Document Scanner SDK"
style="{StaticResource Headline}"
SemanticProperties.HeadingLevel="Level1" />
<Label Text="Using IronOCR"
style="{StaticResource SubHeadline}"
SemanticProperties.HeadingLevel="Level2"
SemanticProperties.Description="Welcome to .NET MAUI Document Scanner SDK" />
<!-- Camera preview -->
<Image x:Name="cameraPreview" />
<!-- Capture button -->
<Button Text="Capture" Clicked="OnCaptureClicked" />
<!-- Text display area -->
<Label x:Name="textLabel" Text="Recognized Text:"/>
</VerticalStackLayout>
</ScrollView>
</ContentPage>
在這個佈局中:
- 我們使用VerticalStackLayout來垂直堆疊控制項。
- 名為cameraPreview的Image控制項用於顯示捕獲的照片。
- Button控件在點擊時觸發OnCaptureClicked事件處理程式。
- 名為textLabel的Label控件用於顯示提取的文字。
輸出

4. 實現文件掃描功能
要在我們的.NET MAUI文件掃描應用中整合提取文字功能,我們將遵循以下步驟:
- 利用相機 API:利用 .NET MAUI 提供的相機 API,直接在您的應用程式中捕捉圖像文件。
- 將圖像傳遞給 IronOCR:一旦捕獲圖像,將其傳遞給 IronOCR,利用其強大的功能進行文字提取。
- 顯示提取的文字:在您的應用程式使用者介面的指定區域中顯示提取的文字供使用者查看。
以下是實施這些步驟的相應程式碼片段:
using IronOcr;
namespace DocumentScanner
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
private async void OnCaptureClicked(object sender, EventArgs e)
{
License.LicenseKey = "YOUR-LICENSE-KEY-HERE";
try
{
// Request camera permissions
var status = await Permissions.RequestAsync<Permissions.Camera>();
if (status == PermissionStatus.Granted)
{
// Take photo
var photo = await MediaPicker.CapturePhotoAsync();
if (photo != null)
{
// Display captured photo in Image
cameraPreview.Source = ImageSource.FromStream(() => photo.OpenReadAsync().Result);
using (var stream = await photo.OpenReadAsync())
{
// Use a stream from the captured photo for OCR
var ocr = new IronTesseract();
using var ocrInput = new OcrInput();
ocrInput.LoadImage(stream);
var ocrResult = ocr.Read(ocrInput);
if (string.IsNullOrEmpty(ocrResult.Text))
{
await DisplayAlert("Error", "No Text Detected!", "OK");
}
else
{
await DisplayAlert("Text Detected!", ocrResult.Text, "OK");
// Display extracted text
textLabel.Text = ocrResult.Text;
}
}
}
}
else
{
// Camera permission denied
await DisplayAlert("Permission Denied", "Camera permission is required to capture photos.", "OK");
}
}
catch (Exception ex)
{
// Handle exception
await DisplayAlert("Error", ex.Message, "OK");
}
}
}
}Imports IronOcr
Namespace DocumentScanner
Partial Public Class MainPage
Inherits ContentPage
Public Sub New()
InitializeComponent()
End Sub
Private Async Sub OnCaptureClicked(ByVal sender As Object, ByVal e As EventArgs)
License.LicenseKey = "YOUR-LICENSE-KEY-HERE"
Try
' Request camera permissions
Dim status = Await Permissions.RequestAsync(Of Permissions.Camera)()
If status = PermissionStatus.Granted Then
' Take photo
Dim photo = Await MediaPicker.CapturePhotoAsync()
If photo IsNot Nothing Then
' Display captured photo in Image
cameraPreview.Source = ImageSource.FromStream(Function() photo.OpenReadAsync().Result)
Using stream = Await photo.OpenReadAsync()
' Use a stream from the captured photo for OCR
Dim ocr = New IronTesseract()
Dim ocrInput As New OcrInput()
ocrInput.LoadImage(stream)
Dim ocrResult = ocr.Read(ocrInput)
If String.IsNullOrEmpty(ocrResult.Text) Then
Await DisplayAlert("Error", "No Text Detected!", "OK")
Else
Await DisplayAlert("Text Detected!", ocrResult.Text, "OK")
' Display extracted text
textLabel.Text = ocrResult.Text
End If
End Using
End If
Else
' Camera permission denied
Await DisplayAlert("Permission Denied", "Camera permission is required to capture photos.", "OK")
End If
Catch ex As Exception
' Handle exception
Await DisplayAlert("Error", ex.Message, "OK")
End Try
End Sub
End Class
End NamespaceImports IronOcr
Namespace DocumentScanner
Partial Public Class MainPage
Inherits ContentPage
Public Sub New()
InitializeComponent()
End Sub
Private Async Sub OnCaptureClicked(ByVal sender As Object, ByVal e As EventArgs)
License.LicenseKey = "YOUR-LICENSE-KEY-HERE"
Try
' Request camera permissions
Dim status = Await Permissions.RequestAsync(Of Permissions.Camera)()
If status = PermissionStatus.Granted Then
' Take photo
Dim photo = Await MediaPicker.CapturePhotoAsync()
If photo IsNot Nothing Then
' Display captured photo in Image
cameraPreview.Source = ImageSource.FromStream(Function() photo.OpenReadAsync().Result)
Using stream = Await photo.OpenReadAsync()
' Use a stream from the captured photo for OCR
Dim ocr = New IronTesseract()
Dim ocrInput As New OcrInput()
ocrInput.LoadImage(stream)
Dim ocrResult = ocr.Read(ocrInput)
If String.IsNullOrEmpty(ocrResult.Text) Then
Await DisplayAlert("Error", "No Text Detected!", "OK")
Else
Await DisplayAlert("Text Detected!", ocrResult.Text, "OK")
' Display extracted text
textLabel.Text = ocrResult.Text
End If
End Using
End If
Else
' Camera permission denied
Await DisplayAlert("Permission Denied", "Camera permission is required to capture photos.", "OK")
End If
Catch ex As Exception
' Handle exception
Await DisplayAlert("Error", ex.Message, "OK")
End Try
End Sub
End Class
End Namespace程式碼解釋
讓我們逐步分解這段程式碼:
- 在MainPage.xaml.cs文件中,OnCaptureClicked方法被定義來處理Capture按鈕的點擊事件。
- 設置IronOCR許可證金鑰,這是使用IronOCR程式庫所需的。 將
"YOUR-LICENSE-KEY-HERE"替換為實際的許可證金鑰。 - 使用Permissions.
RequestAsync<Permissions.Camera>()請求相機許可權,以確保應用可以存取裝置的相機。 - 調用
MediaPicker.CapturePhotoAsync()使用相機拍攝照片。 如果成功,照片將顯示在cameraPreview影像控制項中。 - 從捕獲照片中開啟一個流並用作IronOCR的輸入,建立一個IronTesseract實例,將影像流載入到OcrInput物件中, 並調用Read方法執行OCR。
- 如果成功,提取的文字將顯示在textLabel控制項中。 如果未檢測到文字,則使用DisplayAlert顯示錯誤消息。
要進一步探索IronOCR和額外的程式碼範例,請存取此程式碼範例頁面。
5. 測試文件掃描應用程式
- 在各個平台(Android、iOS和Windows)上運行應用以確保跨平台的相容性。
- 測試不同的場景,比如掃描具有不同字體、大小和方向的文件。
- 驗證提取的文字是否準確並正確顯示在UI上。
輸出 - 無文字的掃描文件

輸出 - 有文字的掃描文件

結論
通過按照本教程,您已學會如何在.NET MAUI中使用IronOCR文件掃描SDK。 文件掃描應用程式有許多實用的應用,從數字化紙質文件到從收據和發票中提取儲存的資訊。 利用IronOCR的強大功能和.NET MAUI的靈活性,您可以建立功能豐富的文件掃描應用程式,以滿足各種使用需求。 嘗試不同的功能,探索額外的程式庫,並持續提升您的技能以創造更令人印象深刻的應用。
欲瞭解IronOCR功能的更多詳細資訊,請存取此文件頁面。
IronOCR提供免費試用,在商業模式下測試其全功能。 其永久lite授權從$999開始。 從下載頁面下載程式並試試看。
Curtis Chau擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。