Load and Save OCR Zones - Console C#

This tutorial shows how to create a C# Windows Console application that uses the LEADTOOLS SDK to load and save OCR zones.

Overview  
Summary This tutorial covers how to use LEADTOOLS OCR SDK technology in a C# Windows Console application
Completion Time 30 minutes
Visual Studio Project Download tutorial project (3 KB)
Platform C# Windows 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 - Console C# tutorial.

Create the Project and Add the LEADTOOLS References

In Visual Studio, create a new C# Windows Console project, and add the following necessary LEADTOOLS references.

The references needed depend upon the purpose of the project. References can be added by one or the other of the following two methods (but not both). For this project, the following references are needed:

If using NuGet references, this tutorial requires the following NuGet package:

If local DLL references are used, the following DLLs are needed. The DLLs are located at <INSTALL_DIR>\LEADTOOLS21\Bin\Dotnet4\x64:

For a complete list of which Codec DLLs are required for specific formats, refer to File Format Support.

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 and local references and setting a license are covered in more detail in the Add References and Set a License tutorial.

Add the Load and Save OCR Zones Code

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

In Solution Explorer, open Program.cs and add the following statements to using block at the top:

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

Add a new method called LoadandSaveOcrZones() and call it inside the Main method after the SetLicense() method call. Add the below code to initialize the IOcrEngine, create the IOcrPage, load OCR zones from file, add a new OcrZone, and save the zones to a new XML 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"; 
        // 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 application runs and creates an IOcrPage, 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 load and save OCR zones. Also it covered how to use the IOcrEngine and IOcrPage interfaces, as well as the OcrZone structure.

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.