Load and Save OCR Zones - C# .NET Core

This tutorial shows how to load and save OCR zones in a C# .NET Core application using the LEADTOOLS SDK.

Overview  
Summary This tutorial covers how to work with OCR zones in a C# .NET Core Console application.
Completion Time 30 minutes
Visual Studio Project Download tutorial project (1 KB)
Platform C# .NET Core Console Application
IDE Visual Studio 2017, 2019
Development License Download LEADTOOLS
Try it in another language

Required Knowledge

Get familiar with the basic steps of creating a project by reviewing the Add References and Set a License tutorial, before working on the Load and Save OCR Zones - C# .NET Core tutorial.

Create the Project and Add LEADTOOLS References

Start with a copy of the project created in the Add References and Set a License tutorial. If you do not have that project, follow the steps in that tutorial to create it.

The references needed depend upon the purpose of the project. References can be added via NuGet packages.

This tutorial requires the following NuGet package:

For a complete list of which DLL files are required for your application, refer to Files to be Included With Your Application.

Set the License File

The License unlocks the features needed for the project. It must be set before any toolkit function is called. For details, including tutorials for different platforms, refer to Setting a Runtime License.

There are two types of runtime licenses:

Note

Adding LEADTOOLS NuGet references and setting a license are covered in more detail in the Add References and Set a License tutorial.

Add the OCR Zones Code

With the project created, the references added, and the license set, coding can begin.

In the Solution Explorer, open Program.cs. Add the following statements to the using block at the top of Program.cs.

C#
using System; 
using System.IO; 
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.Ocr; 

Add a new method to the Program class named LoadandSaveOcrZones(). Call the LoadandSaveOcrZones() method inside the Main() method below the set license code, as shown below.

C#
static void Main(string[] args) 
{ 
    if (!SetLicense()) 
        Console.WriteLine("Error setting license"); 
    else 
        Console.WriteLine("License file set successfully"); 
 
    LoadandSaveOcrZones(); 
} 

Add the code below to the LoadandSaveOcrZones() method to load OCR zones to an IOcrPage, output the OCR zone properties to the console, add a new OCR zone programmatically, and then export the OCR zones to file.

C#
static void LoadandSaveOcrZones() 
{ 
    using (RasterCodecs codecs = new RasterCodecs()) 
    using (IOcrEngine ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.LEAD)) 
    { 
        ocrEngine.Startup(codecs, null, null, @"C:\LEADTOOLS21\Bin\Common\OcrLEADRuntime"); 
        string zonesFile = @"PATH TO OCR ZONES XML"; 
        string imageFile = @"SAMPLE IMAGE TO CREATE OCR PAGE"; 
        string zonesOutFile = @"PATH TO SAVE OCR ZONES XML"; 
        // Load the TIFF file as image 
        RasterImage image = ocrEngine.RasterCodecsInstance.Load(imageFile, 1); 
        using (IOcrPage ocrPage = ocrEngine.CreatePage(image, OcrImageSharingMode.None)) 
        { 
            Console.WriteLine($"{ocrPage.Zones.Count} Zones after IOcrPage creation.\n"); 
            ocrPage.LoadZones(zonesFile); 
            Console.WriteLine($"{ocrPage.Zones.Count} Zones after loading zones from file.\n"); 
            foreach (OcrZone ocrZone in ocrPage.Zones) 
            { 
                int index = ocrPage.Zones.IndexOf(ocrZone); 
                Console.WriteLine("Zone index: {0}", index); 
                Console.WriteLine("  Id                  {0}", ocrZone.Id); 
                Console.WriteLine("  Bounds              {0}", ocrZone.Bounds); 
                Console.WriteLine("  ZoneType            {0}", ocrZone.ZoneType); 
                Console.WriteLine("  CharacterFilters:   {0}", ocrZone.CharacterFilters); 
                Console.WriteLine("----------------------------------"); 
            } 
            // Add an extra zone, this is our user-defined one 
            OcrZone zone = new OcrZone(); 
            zone.Name = "Custom zone"; 
            zone.ZoneType = OcrZoneType.Text; 
            zone.Bounds = new LeadRect(10, 10, ocrPage.Width - 20, 100); 
            ocrPage.Zones.Add(zone); 
 
            Console.WriteLine($"{ocrPage.Zones.Count} Zones after adding zone manually.\n"); 
 
            ocrPage.SaveZones(zonesOutFile); 
            Console.WriteLine($"Zones successfully saved to {zonesOutFile}"); 
            Console.WriteLine("Press any key to exit..."); 
            Console.ReadKey(true); 
        } 
    } 
} 

Run the Project

Run the project by pressing F5, or by selecting Debug -> Start Debugging.

If the steps were followed correctly, the console appears and the application creates an IOcrPage object, loads OCR zones from the specified file, adds a new OcrZone to the IOcrPage, and then exports the zones in XML to the specified file path.

Wrap-up

This tutorial showed how to work with OCR zones using the OCRZone structure. Also, we covered how to use the IOcrEngine and IOcrPage interfaces.

See Also

Help Version 21.0.2023.3.1
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2021 LEAD Technologies, Inc. All Rights Reserved.

Products | Support | Contact Us | Intellectual Property Notices
© 1991-2021 LEAD Technologies, Inc. All Rights Reserved.