public RasterCodecs RasterCodecsInstance { get; }
The RasterCodecs object being used inside this IOcrEngine.
You can pass an instance of an already initialized RasterCodecs object to the Startup method. This RasterCodecs objects will then be used internally by the engine when loading image files from disk or memory. Otherwise, when passing null (Nothing in VB), the IOcrEngine will create and use its own version of RasterCodecs during the startup procedure.
The internal RasterCodecs object will be disposed of by the engine automatically when Shutdown or Dispose is called. If you passed your own instance of RasterCodecs, then the engine will not dispose it and you can continue to use it as normal after the engine instance has been disposed. When passing your own instance of RasterCodecs, make sure this instance stays valid as long as the engine is started.
When new IOcrDocument objects are created using the IOcrDocumentManager.CreateDocument, a new object of type RasterCodecs is created by this IOcrEngine, and assigned to IOcrDocument.RasterCodecsInstance. All image load and save operation (such as IOcrPageCollection.AddPage, IOcrPageCollection.AddPages, IOcrPageCollection.InsertPage, IOcrPageCollection.InsertPages, IOcrPageCollection.ExportPage and IOcrPageCollection.ExportPages) that is performed inside by OCR document or objects inside the OCR document will use that object.
If the value of IOcrDocument.UseEngineInstanceOptions is true, then the options will be copied from the engine's RasterCodecs to the document RasterCodecs before any methods is called.
For more information on how this object is used by the OCR engine during its lifetime, refer to OCR Engine and RasterCodecs/DocumentWriter Usage.
using Leadtools;
using Leadtools.Codecs;
using Leadtools.Ocr;
using Leadtools.Document.Writer;
public void StartupEngineExample()
{
// Use RasterCodecs to load an image file
// Note: You can let the engine load the image file directly as shown in the other examples
RasterCodecs codecs = new RasterCodecs();
RasterImage image = codecs.Load(Path.Combine(LEAD_VARS.ImagesDir, "Ocr1.tif"));
// Assume you copied the engine runtime files to C:\MyApp\Ocr
string engineDir = @"C:\MyApp\Ocr";
// Store the engine work directory into a path inside our application
string workDir = @"C:\MyApp\OcrTemp";
// Delete all files in the work directory in case the previous version of our application exited abnormally and
// the engine did not get the chance to clean all of its temporary files (if any)
Directory.Delete(workDir, true);
// Re-create the work directory
Directory.CreateDirectory(workDir);
// Create an instance of the engine
using (IOcrEngine ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.LEAD))
{
// Show that the engine has not been started yet
Console.WriteLine("Before calling Startup, IsStarted = " + ocrEngine.IsStarted);
// Start the engine using our parameters
// Since we already have a RasterCodecs object, we can re-use it to save memory and resources
ocrEngine.Startup(codecs, null, workDir, engineDir);
// Make sure the engine is using our working directory
Console.WriteLine("workDir passed is {0}, the value of WorkDirectory after Startup is {1}", workDir, ocrEngine.WorkDirectory);
// Show that the engine has started fine
Console.WriteLine("After calling Startup, EngineType is {0}, IsStarted = {1}", ocrEngine.EngineType, ocrEngine.IsStarted);
// Maks sure the engine is using our own version of RasterCodecs
Debug.Assert(codecs == ocrEngine.RasterCodecsInstance);
// Create a page from the raster image as page to the document
IOcrPage ocrPage = ocrEngine.CreatePage(image, OcrImageSharingMode.AutoDispose);
// image belongs to the page and will be dispose when the page is disposed
// Recognize the page
// Note, Recognize can be called without calling AutoZone or manually adding zones. The engine will
// check and automatically auto-zones the page
ocrPage.Recognize(null);
// Create a file based document
using (IOcrDocument ocrDocument = ocrEngine.DocumentManager.CreateDocument(null, OcrCreateDocumentOptions.AutoDeleteFile))
{
// Add the page
ocrDocument.Pages.Add(ocrPage);
// No need for the page anymore
ocrPage.Dispose();
// Save the document we have as PDF
string pdfFileName = Path.Combine(LEAD_VARS.ImagesDir, "Ocr1.pdf");
ocrDocument.Save(pdfFileName, DocumentFormat.Pdf, null);
}
// Shutdown the engine
// Note: calling Dispose will also automatically shutdown the engine if it has been started
ocrEngine.Shutdown();
}
}
static class LEAD_VARS
{
public const string ImagesDir = @"C:\LEADTOOLS23\Resources\Images";
}