[SerializableAttribute()]
public struct OcrZone
The OcrZone structure contains all the necessary information for describing a zone. A zone is a rectangular area on an image containing a feature of interest to the user. The image data covered by each zone is handled and processed (typically recognized) separately.
Access the zones of an IOcrPage object with the IOcrPage.Zones property. This is an IOcrZoneCollection allowing the user to examine and modify the various zones on the page.
Zones can be divided into two basic categories: whether a zone is to be recognized or whether it should be treated as graphic. Zones containing text information can be considered to be either of the flowing type or of a table type. This basic classification of zones helps the OCR engine handle text information correctly.
Use the CharacterFilters property to modify the set of valid characters for recognition of text zone. Specifying the character set correctly is a recognition accuracy issue.
Other members of the zone structure specify how the checking subsystem will function for the particular zone.
Construct the zones of a page automatically using IOcrPage.AutoZone. You must call this method and finish any modification you might have to the zones before calling IOcrPage.Recognize.
To modify a zone, use IOcrZoneCollection.Item to get the OcrZone object of interest, modify the zone then set it back through IOcrZoneCollection.Item. Note that since OcrZone is a structure (value type), use the setter. The following code snippet illustrates how to change the zone type of the 4th zone in an ocrPage:
// Remember, the index is zero-based, so 4th zone has an index of 3
OcrZone zone = ocrPage.Zones[3];
// Prevent this zone from being recognized
zone.ZoneType = OcrZoneType.Graphic;
ocrPage.Zones[3] = zone;
When adding or updating a zone directly initialize the OcrZone.Bounds, OcrZone.CharacterFilters and the OcrZone.ZoneType properties of the zone, since they will not take on their default values.
Do not modify the OcrZone.Id property. When adding zones, leave this value to the default of 0.
The OcrZone.ZoneType property of the updating zone should be one of the following: OcrZoneType.Text, OcrZoneType.Table or OcrZoneType.Graphics.
The preferred method of updating a zone is as illustrated in the code snippet above, first get the zone of interest, update it and set it back into the collection.
LEADTOOLS OCR .NET also supports OMR (Optical Mark Recognition) in all the engines. For more information, refer to Using OMR in LEADTOOLS .NET OCR.
If the zone is a table, the you can use IOcrTableZoneManager to get information and manipulate the cells properties.
To create an OcrZone object with all values initialized to default values, use OcrTypeManager.CreateDefaultOcrZone. This is a shortcut for languages that do not automatically initialize all the members of a structure, such as JavaScript.
using Leadtools;
using Leadtools.Codecs;
using Leadtools.Ocr;
using Leadtools.Forms.Common;
using Leadtools.Document.Writer;
using Leadtools.WinForms;
using Leadtools.Drawing;
using Leadtools.ImageProcessing;
using Leadtools.ImageProcessing.Color;
public void OcrAutoZoneExample()
{
// Create an image with some text in it
RasterImage image = new RasterImage(RasterMemoryFlags.Conventional, 320, 200, 24, RasterByteOrder.Bgr, RasterViewPerspective.TopLeft, null, IntPtr.Zero, 0);
Rectangle imageRect = new Rectangle(0, 0, image.ImageWidth, image.ImageHeight);
IntPtr hdc = RasterImagePainter.CreateLeadDC(image);
using (Graphics g = Graphics.FromHdc(hdc))
{
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.FillRectangle(Brushes.White, imageRect);
using (Font f = new Font("Arial", 20, FontStyle.Regular))
g.DrawString("Normal line", f, Brushes.Black, 0, 0);
using (Font f = new Font("Courier New", 20, FontStyle.Regular))
g.DrawString("Monospaced line", f, Brushes.Black, 0, 80);
}
RasterImagePainter.DeleteLeadDC(hdc);
string zonesFileName = Path.Combine(LEAD_VARS.ImagesDir, "MyZones.xml");
// Create an instance of the engine
using (IOcrEngine ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.LEAD))
{
// Start the engine using default parameters
ocrEngine.Startup(null, null, null, LEAD_VARS.OcrLEADRuntimeDir);
// Create an OCR page
using (IOcrPage ocrPage = ocrEngine.CreatePage(image, OcrImageSharingMode.AutoDispose))
{
// Show the zones, there should be no zones yet
ShowZones("Right after the page was created", ocrPage);
// Perform default AutoZoning on the page
ocrPage.AutoZone(null);
// Show the zones, there should be two zones, one for each line
ShowZones("AutoZone with default parameters", ocrPage);
// Update the first zone manually
OcrZone ocrZone = ocrPage.Zones[0];
ocrZone.ZoneType = OcrZoneType.Text;
ocrPage.Zones[0] = ocrZone;
// Show the zones
ShowZones("After updating the type of the first zone", ocrPage);
// Save the zones to a file and then clear them
ocrPage.SaveZones(zonesFileName);
ocrPage.Zones.Clear();
// Show the zones, there should be no zones since we just cleared them
ShowZones("After calling save and clear", ocrPage);
// Re-load the zones
ocrPage.LoadZones(zonesFileName);
ShowZones("After re-loading the zones", ocrPage);
}
// Shutdown the engine
// Note: calling Dispose will also automatically shutdown the engine if it has been started
ocrEngine.Shutdown();
}
}
private void ShowZones(string message, IOcrPage ocrPage)
{
Console.WriteLine("Zones after {0}:", message);
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("----------------------------------");
}
Console.WriteLine("Hit enter to continue");
//Console.ReadLine();
}
static class LEAD_VARS
{
public const string ImagesDir = @"C:\LEADTOOLS22\Resources\Images";
public const string OcrLEADRuntimeDir = @"C:\LEADTOOLS22\Bin\Common\OcrLEADRuntime";
}