Each AutoFormsEngine works on one IMasterFormsRepository which is provided through its constructor.
Important: Different OCR engines might have slight differences in form alignment and recognition. Therefore, it is best to use the same Leadtools.Ocr.OcrEngineType when generating the master forms and when recognizing and processing forms.
The following code shows how to AutoFormsEngine can be used in a multi-threaded server to process forms on demand:
// The server class, can be a Windows or WCF service
public class MyServer
{
// This is the request to process
public void ProcessRequest(string fileName)
{
// Unlock the LEADTOOLS support
// Get the license file path and developer key
// This can be fixed or read from a configuration file
string licenseFilePath = @"C:\LicenseFile.lic";
string developerKey = @"MyDeveloperKey";
RasterSupport.SetLicense(licenseFilePath, developerKey);
// Get the repository instance. This example assumes a disk repository
// The path can be fixed or read from configuration file
IMasterFormsRepository repository = new DiskMasterFormsRepository(new RasterCodecs(), @"C:\MyRepository");
// Get the OCR engine type
// Again, this can be fixed or read from a configuration file
OcrEngineType ocrEngineType = OcrEngineType.LEAD;
// int threadCount = Environment.ProcessorCount;
// Create the OCR engines needed for this operation
IOcrEngine ocrEngine = OcrEngineManager.CreateEngine(ocrEngineType);
// Start the OCR engine
ocrEngine.Startup(null, null, null, null);
// Optional. Setup the barcode engine
BarcodeEngine barcodeEngine = new BarcodeEngine();
// Create the auto-forms engine
AutoFormsEngine engine = new AutoFormsEngine(repository, ocrEngine, barcodeEngine, AutoFormsRecognitionManager.Default);
bool runInThread = true; // Set this value depending on your server setup
if (runInThread)
{
using (AutoResetEvent finishedEvent = new AutoResetEvent(false))
{
// If this is a Windows Service, then the call to Run must be done in a separate thread to not block subsequent calls
ThreadPool.QueueUserWorkItem((state) =>
{
try
{
// Process the request, assuming all categories
AutoFormsRunResult result = engine.Run(fileName, null);
// Call your function to process the result
MyProcessResult(result);
}
catch (Exception ex)
{
Console.WriteLine("Error {0}", ex.Message);
}
finally
{
// We are done, inform the main thread
finishedEvent.Set();
}
});
}
}
else
{
// Another type of server, such as a WCF HTTP server, no need to run
// in a separate thread since every request is done in its separate process (IIS process)
// Process the request, assuming all categories
AutoFormsRunResult result = engine.Run(fileName, null);
// Call your function to process the result
MyProcessResult(result);
}
}
}