OCR in .NET MAUI: Building an Image Processing Application
Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (175).NET Core  (29).NET MAUI  (208)Angular  (109)ASP.NET  (51)ASP.NET Core  (82)ASP.NET MVC  (89)Azure  (41)Black Friday Deal  (1)Blazor  (220)BoldSign  (15)DocIO  (24)Essential JS 2  (107)Essential Studio  (200)File Formats  (67)Flutter  (133)JavaScript  (221)Microsoft  (119)PDF  (81)Python  (1)React  (101)Streamlit  (1)Succinctly series  (131)Syncfusion  (920)TypeScript  (33)Uno Platform  (3)UWP  (4)Vue  (45)Webinar  (51)Windows Forms  (61)WinUI  (68)WPF  (159)Xamarin  (161)XlsIO  (37)Other CategoriesBarcode  (5)BI  (29)Bold BI  (8)Bold Reports  (2)Build conference  (8)Business intelligence  (55)Button  (4)C#  (151)Chart  (132)Cloud  (15)Company  (443)Dashboard  (8)Data Science  (3)Data Validation  (8)DataGrid  (63)Development  (633)Doc  (8)DockingManager  (1)eBook  (99)Enterprise  (22)Entity Framework  (5)Essential Tools  (14)Excel  (41)Extensions  (22)File Manager  (7)Gantt  (18)Gauge  (12)Git  (5)Grid  (31)HTML  (13)Installer  (2)Knockout  (2)Language  (1)LINQPad  (1)Linux  (2)M-Commerce  (1)Metro Studio  (11)Mobile  (508)Mobile MVC  (9)OLAP server  (1)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (43)Performance  (12)PHP  (2)PivotGrid  (4)Predictive Analytics  (6)Report Server  (3)Reporting  (10)Reporting / Back Office  (11)Rich Text Editor  (12)Road Map  (12)Scheduler  (52)Security  (3)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (31)Solution Services  (4)Spreadsheet  (11)SQL  (11)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (12)Tips and Tricks  (112)UI  (387)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (2)Visual Studio  (31)Visual Studio Code  (19)Web  (597)What's new  (333)Windows 8  (19)Windows App  (2)Windows Phone  (15)Windows Phone 7  (9)WinRT  (26)
OCR in .NET MAUI Building an Image Processing Application

OCR in .NET MAUI: Building an Image Processing Application

.NET MAUI is a cross-platform framework that allows developers to create desktop and mobile applications from a single codebase with C#. In this article, we develop a simple .NET MAUI OCR scanner to scan images and convert them to PDF with searchable text using the Syncfusion OCR library.

The Syncfusion .NET Optical Character Recognition (OCR) library extracts text from scanned PDFs and images. It uses the Tesseract OCR engine. The Syncfusion OCR library does not work on mobile platforms with the Tesseract engine, so starting from version 20.3.0.47, we added support to use any external OCR service, such as Azure Cognitive Services OCR, with our existing OCR library to process OCR in mobile platforms.

Steps to build an OCR scanner application in .NET MAUI

In the following steps, we will create a .NET MAUI app, install different dependencies, and use the Syncfusion OCR library to convert an image into a readable PDF.

Step 1: Create a .NET MAUI project

Create a simple .NET MAUI project by referring to the documentation build your first .NET MAUI app.

Step 2: Install the dependencies

Install the following NuGet packages:

Step 3: Add UI elements

In this application, we get images from the user in the following ways:

  • Open the camera and capture images such as receipts, notes, documents, photos, and business cards.
  • Select images from the device’s photo gallery.

Open camera and capture image

Add a button in the UI to open the camera.

<Button x:Name=”CameraBtn”
	  Text=”Open Camera”
          Clicked=”OnCameraClicked”
          HorizontalOptions=”Center” />

Use the MAUI MediaPicker API to open and capture images using the camera. The MediaPicker API needs permission to access the camera and internal storage. Refer to the get started section of the API documentation to set the permissions.

Call the CapturePhotoAsync method to open the camera and capture the image. Refer to the complete code below.

private void OnCameraClicked(object sender, EventArgs e)
{
   TakePhoto();
}
//Open camera and take photo
public async void TakePhoto()
{
   if (MediaPicker.Default.IsCaptureSupported)
   {
      FileResult photo = await MediaPicker.Default.CapturePhotoAsync();
      if (photo != null)
      {
          // Save the file into local storage.
          string localFilePath = Path.Combine(FileSystem.CacheDirectory, photo.FileName);
                
          //Reduce the size of the image. 
          using Stream sourceStream = await photo.OpenReadAsync();
          using SKBitmap sourceBitmap=SKBitmap.Decode(sourceStream);
          int height = Math.Min(794, sourceBitmap.Height);
                int width = Math.Min(794, sourceBitmap.Width);
                
          using SKBitmap scaledBitmap = sourceBitmap.Resize(new SKImageInfo(width, height),SKFilterQuality.Medium);
          using SKImage scaledImage = SKImage.FromBitmap(scaledBitmap);
                
          using (SKData data = scaledImage.Encode())
          {
              File.WriteAllBytes(localFilePath, data.ToArray());
          }
               
           //Create model and add to the collection
           ImageModel model = new ImageModel() { ImagePath = localFilePath, Title = "sample", Description = "Cool" };
           viewModel.Items.Add(model);
      }
   }
}

Select an image from the gallery

Add a button in the UI to select an image.

<Button
	x:Name="CounterBtn"
	Text="Select Image"
	Clicked="OnCounterClicked"
	HorizontalOptions="Center" />

Use the MAUI MediaPicker API to select images from the device’s photo gallery. Use the PickPhotoAsync method to select images from the gallery. Refer to the following code snippet.

private void OnCounterClicked(object sender, EventArgs e)
{
    PickPhoto();
}

//Select images from gallery.
public async void PickPhoto()
{
   if (MediaPicker.Default.IsCaptureSupported)
   {
       FileResult photo = await MediaPicker.Default.PickPhotoAsync();
       
       if (photo != null)
       {
          // Save the file into local storage.
          string localFilePath = Path.Combine(FileSystem.CacheDirectory, photo.FileName);
                
          using Stream sourceStream = await photo.OpenReadAsync();
          using FileStream localFileStream = File.OpenWrite(localFilePath);
                
          await sourceStream.CopyToAsync(localFileStream);
                
          ImageModel model = new ImageModel() { ImagePath = localFilePath, Title = "sample", Description = "Cool" };
          viewModel.Items.Add(model);
      }
    }
}

Once we receive the images, present those images in the UI using the CollectionView. Refer to the MAUI data templates documentation to bind the image data into the CollectionView.

Finally, convert the image to PDF with OCR, so our simple UI looks like the following picture.

Select an image from GalleryRefer to the MainPage.xaml file on GitHub for the complete UI.

Step 4: Convert images to PDF with OCR text

Here, we use the Syncfusion OCR library with the external Azure OCR engine to convert images to PDF.

Please refer to this article to configure and use the Azure Computer Vision OCR services. We have already created a class named AzureOcrEngine.cs to process images. You can use this class as it is in your project without any changes. You just need to add your subscription key and endpoint in the code.

The AzureOCREngine implements the interface IOcrEngine. The IOcrEngine has a method named PerformOCR that takes an image stream as an argument. Now, call the external OCR service with the input image and it returns OCRLayoutResult as the result.

Assign AzureOCREngine as an external OCR engine using the API ExternalEngine available in the OCRProcessor class from the Syncfusion PDF OCR library.

Refer to the following code example.

public void ConvertToPDF()
{
    Task.Run(async () =>
    { 
    
       PdfDocument finalDcoument = new PdfDocument();
       if (viewModel.Items.Count == 0)
          return;
       using (OCRProcessor processor = new OCRProcessor())
       {
 
          processor.ExternalEngine = new AzureOcrEngine();
          foreach (var item in viewModel.Items)
          {
             FileStream imageStream = new FileStream(item.ImagePath,FileMode.Open);
             PdfDocument document = processor.PerformOCR(imageStream);
             MemoryStream saveStream = new MemoryStream();
             document.Save(saveStream);
             document.Close(true);
             PdfDocument.Merge(finalDcoument,saveStream);
         }
 
      }
 
    MemoryStream fileSave = new MemoryStream();
    finalDcoument.Save(fileSave);
    fileSave.Position = 0;
    finalDcoument.Close(true);
            
    Dispatcher.Dispatch(() =>
    {
        popup.Close();
        SaveService service = new SaveService();
        service.SaveAndView("Output.pdf", "application/pdf", fileSave);
    });
 
  });
}

After executing the above code, you will get the following output.

Converting image to PDF with OCROCR PDF file

Note: To save the PDF document to external storage, add the platform-specific service classes. Refer to the respective classes in the following links:

GitHub sample

For more details about this project, refer to the complete .NET MAUI OCR scanner application on GitHub.

Conclusion

In this blog post, we created a simple .NET MAUI OCR scanner application to process existing images and new images captured using the camera into PDFs with machine-readable text.

Take a moment to explore the documentation, where you will find other options and features of the Syncfusion OCR library, all with accompanying code samples.

If you have any questions or comments, you can contact us through our support forums, support portal, or feedback portal. We are always happy to assist you!

Test Flight
App Center Badge
Google Play Store Badge
Microsoft Badge
Github Store Badge

Related blogs

If you liked this article, we think you would also like the following articles about our PDF Library:

Tags:

Share this post:

Comments (1)

Hi Paraveen,
Do you have anything for image cropping and document scanning like one used by banks to take picture of the checks ?
Thanks

Comments are closed.

Popular Now

Be the first to get updates

Subscribe RSS feed

Be the first to get updates

Subscribe RSS feed