public void SaveZones(
string fileName,
int pageNumber
)
fileName
The name of the file to save the zones to. The file must exist and must contain a valid multipage zones file.
pageNumber
The 1-based page number of the zones in the file to replace (or append). If the zones file contain zones for page 'pageNumber', then this method will replace these zones with the zones of the current IOcrPage. If the file does not contain zones for page 'pageNumber', then this method will append these zones at the end of the file.
To save and load the zones of OCR pages, you can use one of these methods:
IOcrPage.SaveZones(fileName) or IOcrPage.SaveZones(stream): These methods save the zones of a particular OCR page to a single-page disk file or stream. These methods will not use the page number, and hence, the file or stream will contain zones that are not tied to any particular page and can be loaded back into any OCR page regardless of its number. To load these zones back into any OCR page, use IOcrPage.LoadZones(fileName) or IOcrPage.LoadZones(stream).
IOcrPage.SaveZones(fileName, pageNumber) or IOcrPage.SaveZones(stream, pageNumber): These methods save the zones of a particular OCR page to a multipage disk file or stream. If the file or stream exist previously, these methods will replace the zones specified in 'pageNumber' with the zones of the IOcrPage. If the file or stream does not contain zones for the specified page number, the zones will be appended to the file or stream at the end and can be loaded later using IOcrPage.LoadZones(fileName, pageNumber) or IOcrPage.LoadZones(stream, pageNumber).
IOcrDocument.SaveZones(fileName) or IOcrDocument.SaveZones(stream): These methods save the zones of all the OCR pages in a document to a multipage disk file or stream. The saved data will contain the page number of the zones. To load these zones, you can either use IOcrDocument.LoadZones(fileName) or IOcrDocument.LoadZones(stream) to load the zones from a multipage file or stream back into a multipage OCR document. Or IOcrPage.LoadZones(fileName, pageNumber) and IOcrPage.LoadZones(stream, pageNumber) to load any single page from a multipage OCR document into a particular OCR page.
Note on loading zones from a multipage zone file: If the file does not contain zones data with the correct page number, the engine will not load any zones for this page. After the method returns, any OCR page that did not have zones data will contain zero zones (the Zones property contains 0 items). You can then use IOcrPage.AutoZone if required to re-zone this page.
Use the SaveZones(fileName) method to save zones to a single-page zones file name.
To load and save the zones to a .NET stream, use LoadZones(stream) and SaveZones(stream).
Saving zones to an external file or a stream could be useful when you are processing forms. For example, you can load one of the forms and automatically find the zones inside it using AutoZone, if the automatic zone detection was not 100 percent satisfactory, you can update the zones in the Zones collection manually and then save the result with SaveZones(fileName). Once the zones are saved. You can now process all similar forms in the following manner:
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";
}