Creating a .NET MAUI document scanner that can capture, normalize, and share documents effortlessly is an exciting task for any developer. With the Dynamsoft Capture Vision MAUI Bundle, you can leverage powerful APIs for document scanning, barcode reading, and MRZ recognition to create a seamless user experience.
In this tutorial, we'll explore how to build a .NET MAUI document scanner that can capture and normalize documents on both Android and iOS platforms. This is part of a 5-part series, where we'll dive deeper into creating various applications using .NET MAUI.
Prerequisites
Before getting started, ensure you have the following tools installed:
- Dynamsoft Capture Vision Trial License
- Xcode
- Android Studio
- Visual Studio for Windows or Visual Studio Code for macOS with the .NET MAUI extension
- Provisioning Profile for iOS (required if building a .NET MAUI app with Visual Studio Code on macOS)
Building a .NET MAUI Document Scanner Step by Step
Dynamsoft provides a .NET MAUI sample project demonstrating how to automatically capture and normalize documents from a camera stream. Based on this sample, we'll make a few modifications:
- Update the target framework from
net7.0tonet8.0. - Add a button to trigger document capture and rectification.
- Allow users to share the normalized document to other apps.
Updating the .NET MAUI Project from .NET 7 to .NET 8
The latest .NET version is 8.0, while the sample project uses .NET 7.0. To update the target framework:
- Open the
.csprojfile and change theTargetFrameworkvalue fromnet7.0tonet8.0. - Add
to thePropertyGroupelement to ensure compatibility:.11.0
Adding a Round Button for Document Capture
- Update the
CameraPage.xamlfile to add a round button for document capture:
`xml
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="AutoNormalize.CameraPage" Title="Auto Normalize">
`
- Implement the
OnCaptureButtonClickedevent handler in theCameraPage.xaml.csfile:
`csharp
bool isReady = false;
private void OnCaptureButtonClicked(object sender, EventArgs e)
{
isReady = true;
}
public void OnNormalizedImagesReceived(NormalizedImagesResult result)
{
if (result?.Items?.Count > 0 && isReady)
{
router?.StopCapturing();
enhancer?.ClearBuffer();
var data = result.Items[0].ImageData;
MainThread.BeginInvokeOnMainThread(async () =>
{
await Navigation.PushAsync(new ImagePage(data));
});
}
}
protected override async void OnAppearing()
{
base.OnAppearing();
isReady = false;
...
}
`
Sharing the Normalized Document
- In the
ImagePage.xamlfile, add aToolbarItemas a share button:
`xml
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="AutoNormalize.ImagePage" Title="ImagePage">
`
- Implement the
OnShareButtonClickedevent handler in theImagePage.xaml.csfile:
`csharp
private async void OnShareButtonClicked(object sender, EventArgs e)
{
if (_item == null)
{
await DisplayAlert("Error", "Image is not shareable.", "OK");
return;
}
var imageSource = _item.ImageData.ToImageSource();
if (imageSource is StreamImageSource streamImageSource)
{
var stream = await streamImageSource.Stream(CancellationToken.None);
var tempFile = Path.Combine(FileSystem.CacheDirectory, "shared_image.jpg");
using (var memoryStream = new MemoryStream())
{
// Convert the image data to a JPEG file
await imageSource.SaveToJpegAsync(tempFile, 100);
}
}
}
`
With these steps, you'll have created a .NET MAUI document scanner that can capture, normalize, and share documents effortlessly. This is just the beginning of your swift app development journey with .NET MAUI.