Signature of the callback to use for redirecting OCR runtime files.
public delegate RasterExceptionCode OcrRuntimeFileCallback(
IOcrEngine engine,
OcrRuntimeFile runtimeFile
)
engine
Caller OCR engine.
runtimeFile
Runtime file information.
The caller should return RasterExceptionCode.Success if the operation is successful, or an appropriate error code if needed. For instance:
Refer to IOcrEngine.RuntimeFileCallback for more information.
using Leadtools;
using Leadtools.Codecs;
using Leadtools.Ocr;
using Leadtools.Document.Writer;
public static void OcrRuntimeFileCallbackExample()
{
// This example assumes that some or all of the OCR runtime files are copied into "C:\MyDir" folder and simulates
// an environment where the runtime files can be obtained through helper methods HasResource and GetResourceStream.
// Create an OCR engine instance
IOcrEngine ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.LEAD);
// Install a OCR file runtime handler
ocrEngine.RuntimeFileCallback = MyRuntimeFileCallback;
// Startup the engine
ocrEngine.Startup(null, null, null,
null /* startupParameters is null*/);
// Perform OCR operation
ocrEngine.AutoRecognizeManager.Run(
@"C:\LEADTOOLS22\Resources\Images\ocr1.tif",
@"C:\LEADTOOLS22\Resources\Images\out.pdf",
DocumentFormat.Pdf,
null,
null);
// Shutdown
ocrEngine.Shutdown();
// Remove the handler
ocrEngine.RuntimeFileCallback = null;
// Dispose the engine
ocrEngine.Dispose();
}
private static RasterExceptionCode MyRuntimeFileCallback(IOcrEngine engine, OcrRuntimeFile runtimeFile)
{
// Called by the OCR engine for each runtime file operation
RasterExceptionCode result = RasterExceptionCode.Success;
// Check the operation:
switch (runtimeFile.Mode)
{
case OcrRuntimeFileMode.Exists:
Debug.WriteLine($"MyRuntimeFileCallback does '{runtimeFile.FileName}' exist");
// The engine is checking if a certain file exists, call our HasResource helper
if (!HasResource(runtimeFile.FileName))
result = RasterExceptionCode.FileNotFound;
break;
case OcrRuntimeFileMode.Open:
Debug.WriteLine($"MyRuntimeFileCallback open '{runtimeFile.FileName}'");
// The engine requested to open the file for reading, call our GetResourceStream helper
// and set the Stream property accordingly
runtimeFile.Stream = GetResourceStream(runtimeFile.FileName);
break;
case OcrRuntimeFileMode.Close:
Debug.WriteLine($"MyRuntimeFileCallback close '{runtimeFile.FileName}'");
// The engine requested to close the file after reading.
// Dispose our stream
runtimeFile.Stream.Dispose();
break;
}
return result;
}
private static bool HasResource(string resourceName)
{
// Do we have this resource?
// In our simulation, we will simply check if the file exist on disk
// In an Android application, this can check if a resource with this name was embedded
return System.IO.File.Exists(Path.Combine(@"C:\MyDir", resourceName));
}
private static Stream GetResourceStream(string resourceName)
{
// Get a stream that can read from resource
// In our simulation, we will simply check if the file exist on disk
// In an Android application, this can call the appropriate platform API to obtain
// a stream to the resource
return System.IO.File.OpenRead(Path.Combine(@"C:\MyDir", resourceName));
}