在生产环境中测试,无水印。
随时随地满足您的需求。
获得30天的全功能产品。
几分钟内就能启动并运行。
在您的产品试用期间,全面访问我们的支持工程团队。
随着移动技术的兴起,像 Scanbot SDK 和 Native SDKs 这样的文档扫描应用程序已成为个人和企业专家不可或缺的工具。在本教程中,我们将探讨如何使用最新版本的 .NET 多平台应用程序界面 (MAUI) 和 IronOCR(一个功能强大的 .NET OCR(光学字符识别)库)创建文档扫描应用程序。 .NET MAUI 简化了跨平台移动应用程序(如 Android)的创建,确保在最终用户设备上的无缝部署。本指南结束时,您将能够开发自己的文档扫描仪 SDK 应用程序,轻松地从图像和扫描文件中提取文本。
安装IronOCR C#库以使用文档扫描SDK。
使用必要的控件设计 .NET MAUI 表单。
使用 MediaPicker.CapturePhotoAsync 方法捕捉照片框。
将捕获的照片转换为流。
将流传递给 OcrInput 的 LoadImage 方法。
使用 IronTesseract 的Read方法执行 OCR。
IronOCR 是由 Iron Software, LLC 开发的尖端光学字符识别 (OCR) 软件,旨在准确高效地将图像和扫描文档转换为可编辑文本。 OCR 技术彻底改变了企业处理文档的方式,使从扫描文档、PDF 和图像等各种来源提取有价值信息变得更加容易。
IronOCR 凭借其先进的功能、强大的性能和易于集成的特点,在众多 OCR 解决方案中脱颖而出。 无论您是希望将 OCR 功能纳入应用程序的开发人员,还是希望简化数据生成的文档管理流程的企业,IronOCR 都能为您提供全面的解决方案。
以下是 IronOCR 的一些重要关键功能:
高精度: IronOCR 使用最先进的算法和机器学习技术来实现卓越的文本识别精度。 它可以从复杂的文档中准确提取文本,包括 QR 低分辨率或劣质扫描的图像。
多语言支持:IronOCR 的一大特点是其广泛的语言支持。 它可以识别超过 127 种语言的文本,适合在不同语言环境下运营的企业。
图像预处理:为了提高准确性,IronOCR 提供了各种图像预处理功能,例如降噪、对比度调整和去偏斜。 这些预处理技术有助于改善 OCR 结果,尤其是在处理扭曲或不完美的图像时。
支持各种文件格式:IronOCR 支持多种文件格式,包括 TIFF、JPEG、PNG 和 PDF。 这种灵活性使用户可以处理不同来源的文档,而不必担心兼容性问题。
自定义选项:开发人员可以根据他们的特定需求自定义 IronOCR 的行为。 无论是微调识别参数还是与现有工作流程整合,IronOCR 都能提供高度的灵活性和定制性。
快速且可扩展:IronOCR经过优化以提升性能,能够快速从大量文档中提取文本。 无论是处理少量文档还是处理海量文档库,其可扩展的架构都能确保无缝操作。
与.NET应用程序集成:IronOCR无缝集成到.NET应用程序中,为开发人员提供易于使用的API和库,以便在他们的软件项目中加入OCR功能。 这种紧密集成简化了开发过程,加快了 OCR 应用程序的上市速度。
打开 Visual Studio 2022 并创建一个新的 .NET MAUI App 项目。

选择合适的项目名称并配置项目设置。

右键单击 Visual Studio 中的解决方案。

让我们从设计 MainPage.xaml 的布局开始。 我们将创建一个简单的布局,其中包含一个用于显示捕获照片的图像控件、一个用于拍照的捕获按钮和一个用于显示提取文本的标签。
以下是 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><?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>在此版面中:

为了将文本提取功能集成到我们的 .NET MAUI 文档扫描应用程序中,我们将遵循以下步骤:
利用相机 API:首先通过 .NET MAUI 提供的相机 API 在应用程序中直接捕获图像文件。
传递图像到 IronOCR:一旦图像被捕获,就将其传递给 IronOCR,一个强大的 OCR 库,用于文本提取。 IronOCR 提供了强大的功能,可以高精度地从图像中提取文本。
显示提取文本:最后,在应用程序用户界面上的指定区域显示提取的文本。 这样,用户就可以轻松查看从捕获的图像或 QR 码中提取的文本。
以下是实现这些步骤的相应代码片段:
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");
            }
        }
    }
}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 Namespace让我们逐步分解代码:
如果文本被成功提取,我们会在名为textLabel的Label控件中显示它。 如果未检测到文本,我们将使用DisplayAlert显示错误消息。
如需更强大的 IronOCR 使用和代码详细信息,请访问此代码示例页面。


通过本教程,您已经学会了如何在 .NET MAUI 中使用 IronOCR 文档扫描仪 SDK。 文档扫描应用程序有许多实际应用,从纸质文档数字化到从收据和发票中提取存储信息,不一而足。 借助 IronOCR for .NET 的强大功能和 .NET MAUI 的灵活性,您可以构建功能丰富的文档扫描仪应用程序,满足各种使用情况。 尝试不同的功能,探索更多的库,继续磨练自己的技能,创建更多令人印象深刻的应用程序。
有关IronOCR功能的详细信息,请访问此文档页面。
IronOCR提供免费试用,以商业模式测试其完整功能。 其永久 Lite license 起价为 $749。 从 下载 页面 下载库并尝试一下。