Runs a job
public OcrAutoRecognizeManagerJobStatus RunJob(IOcrAutoRecognizeJob job)
Function RunJob( _ByVal job As Leadtools.Forms.Ocr.IOcrAutoRecognizeJob _) As Leadtools.Forms.Ocr.OcrAutoRecognizeManagerJobStatus
- (LTOcrAutoRecognizeManagerJobStatus)runJob:(LTOcrAutoRecognizeJob *)job error:(NSError **)error public OcrAutoRecognizeManagerJobStatus runJob(OcrAutoRecognizeJob job) Leadtools.Forms.Ocr.OcrAutoRecognizeManagerJobStatus RunJob(Leadtools.Forms.Ocr.IOcrAutoRecognizeJob^ job)
job
The IOcrAutoRecognizeJob to run this parameter cannot be null (Nothing in VB). Use CreateJob to create a job.
An OcrAutoRecognizeManagerJobStatus enumeration member that determines whether the job was completed successfully or aborted due to errors or user action.
If you call this method from the same thread that created IOcrAutoRecognizeManager, then the current thread will block till this method returns. To run a job asynchronously, use RunJobAsync.
When this method returns, the IOcrAutoRecognizeJob.Errors member of job will contain any errors that might have occurred during the recognition process.
To use this method, initialize a new OcrAutoRecognizeJobData object with the job's parameters (input image file name, pages, output document format, output document name, optional zones file name, etc.), then use CreateJob to create the IOcrAutoRecognizeJob object passed as job to this method. Finally, call RunJob passing the IOcrAutoRecognizeJob object.
This method will perform the following operations:
The JobStarted event is triggered.
Creates one or more IOcrDocument object to store the pages into. The number of OCR documents created is dependent on MaximumThreadsPerJob. If this value is 0 (maximum CPUs/cores) or is greater than 1 and multiple threads is supported by this engine, then more than one document might be created to participate in the recognition process. The document will be created as disk-based.
Loops through the pages specified in OcrAutoRecognizeJobData.FirstPageNumber and in OcrAutoRecognizeJobData.LastPageNumber in OcrAutoRecognizeJobData.ImageFileName and for each page:
The page is created using IOcrEngine.CreatePage.
If OcrAutoRecognizeJobData.ZonesFileName contains a valid multi-page zone file name and has an entry for the current page, then the zones are loaded with IOcrPage.LoadZones(fileName, pageNumber) and applied to the page. If OcrAutoRecognizeJobData.ZonesFileName is a null (Nothing in VB) reference or it does not contain an equivalent page number, auto-decomposing of the page is performed instead with IOcrPage.AutoZone. A valid multi-page zone file that has entries for all document pages is generated by saving document zones using IOcrDocument.SaveZones or IOcrDocument.SaveAsync not IOcrPage.SaveZones since IOcrPage.SaveZones does not save the page number.
IOcrPage.Recognize is called to get the OCR data of the page.
For OCR Advantage engine, the page is added to the document using IOcrDocument.Pages.Add.
For other engines that OCR Advantage: If multiple documents are used or current number of recognized pages is greater than the maximum specified in MaximumPagesBeforeLtd, then current recognition data is saved to a temporary LTD file and the OCR document is cleared.
When all pages are processed their saved to result file name specified in OcrAutoRecognizeJobData.DocumentFileName using the format specified OcrAutoRecognizeJobData.Format If LTD was used, the temporary file is converted to the final document using DocumentWriter.Convert and optionally DocumentWriter.AppendLtd.
All OCR documents and temporary files are deleted.
The JobCompleted event is triggered.
You can use the JobProgress event to show the operation progress or to abort it if threading is not used. For more information and an example, refer to OcrProgressCallback.
You can use the JobOperation event to get information regarding the current operation being performed. For more information and an example, refer to JobOperation.
The IOcrAutoRecognizeManager interface also has the following options to use with this method:
| Option | Description |
|---|---|
| MaximumPagesBeforeLtd |
Add support for converting a document with unlimited number of pages. An OCR recognition operation on a document that contains a large amount of pages (10 and more) might result in an out of memory error. All of the LEADTOOLS OCR engines supports saving the intermediate recognition results to a temporary LTD file (DocumentFormat.LTD). The result of subsequent pages will be appended to this temporary file. When all the pages of the document have been recognized, the engine will convert the temporary LTD file to the desired output format. The MaximumPagesBeforeLtd property defines the maximum number of pages processed as a whole. For example, if the original document has 20 pages and the value of this property is 8, the engine will recognize the first 8 pages and saves the result to a temporary file, recognizes the second 8 pages and append the results, and finally, recognize the last 4 pages and convert the temporary document to the final format. |
| PreprocessPageCommands |
Holds an array of OcrAutoPreprocessPageCommand items to control what auto-preprocess operation to perform on each page document prior to recognition. |
| MaximumThreadsPerJob |
Maximum number of threads to use per job. You can instruct IOcrAutoRecognizeManager to use all available machine CPUs/cores when recognizing a document. This will greatly reduce the time required to finish the OCR operation. |
| JobErrorMode |
Ability to resume on none critical errors. For example, if a source document has a page that could not be recognized. The offending page will be added to the final document as a graphics images and recognition will continue to the next page. |
| JobStarted, JobProgress, JobOperation and JobCompleted events |
Events to track when both synchronous and asynchronous jobs has started, being run and completed. |
| AbortAllJobs |
Aborts all running and pending jobs. |
| EnableTrace |
Output debug messages to the standard .NET trace listeners. |
This example will OCR all the images in given folder and convert them to PDF documents. It will use multiple threads to maximize the recognition performance and supports abortion and continue on non-critical errors. This example supports converting images of any number of pages.
using Leadtools;using Leadtools.Codecs;using Leadtools.Forms.Ocr;using Leadtools.Forms.DocumentWriters;using Leadtools.Forms;using Leadtools.WinForms;public class RunJobExample{// Number of documents that are pendingprivate int _documentsPending;// Event to trigger when all documents are finishedprivate AutoResetEvent _allDocumentsFinishedEvent;public void Start(){string imagesDirectory = LEAD_VARS.ImagesDir;string documentsDirectory = Path.Combine(LEAD_VARS.ImagesDir, "RunJobExample");// Create the output (documents) directoryif (!Directory.Exists(documentsDirectory)){Directory.CreateDirectory(documentsDirectory);}// Get all TIF files in input (images) directorystring[] imageFileNames = Directory.GetFiles(imagesDirectory, "*.tif");if (imageFileNames.Length == 0){Console.WriteLine("No images to OCR");return;}// Create a new OCR engine instanceOcrEngineType engineType = OcrEngineType.Advantage;Console.WriteLine(string.Format("Starting up {0} engine", engineType));using (IOcrEngine ocrEngine = OcrEngineManager.CreateEngine(engineType, false)){ocrEngine.Startup(null, null, null, LEAD_VARS.OcrAdvantageRuntimeDir);// Setup document PDF save options: Image/Text with CCITT G4 encoding for B/WDocumentWriter docWriter = ocrEngine.DocumentWriterInstance;PdfDocumentOptions pdfOptions = docWriter.GetOptions(DocumentFormat.Pdf) as PdfDocumentOptions;pdfOptions.ImageOverText = true;pdfOptions.DocumentType = PdfDocumentType.Pdf;pdfOptions.FontEmbedMode = DocumentFontEmbedMode.None;pdfOptions.OneBitImageCompression = OneBitImageCompressionType.FaxG4;docWriter.SetOptions(DocumentFormat.Pdf, pdfOptions);// We are going to use multiple-threads, so disable threading in// IOcrAutoRecognizeManagerIOcrAutoRecognizeManager autoRecognizeManager = ocrEngine.AutoRecognizeManager;autoRecognizeManager.MaximumThreadsPerJob = 1;// Tell the recognize manager to continue on errorsautoRecognizeManager.JobErrorMode = OcrAutoRecognizeManagerJobErrorMode.Continue;// Instead of using events to trigger when documents are done,// we will use the JobCompleted events of IOcrAutoRecognizeManager// to decrement a counter and trigger one event when the counter reaches 0autoRecognizeManager.JobStarted += new EventHandler<OcrAutoRecognizeRunJobEventArgs>(autoRecognizeManager_JobStarted);autoRecognizeManager.JobCompleted += new EventHandler<OcrAutoRecognizeRunJobEventArgs>(autoRecognizeManager_JobCompleted);int count = imageFileNames.Length;_documentsPending = count;_allDocumentsFinishedEvent = new AutoResetEvent(false);for (int i = 0; i < count; i++){// Create the job datastring imageFileName = imageFileNames[i];string name = "Document " + (i + 1).ToString();Console.WriteLine("Queuing {0} file {1}", name, imageFileName);JobData data = new JobData();data.AutoRecognizeManager = autoRecognizeManager;data.ImageFileName = imageFileName;data.DocumentFileName = Path.Combine(documentsDirectory, Path.GetFileNameWithoutExtension(imageFileName) + ".pdf");data.JobName = name;// Queue this jobThreadPool.QueueUserWorkItem(new WaitCallback(RunJob), data);}// Wait for all documents to finish_allDocumentsFinishedEvent.WaitOne();_allDocumentsFinishedEvent.Close();autoRecognizeManager.JobStarted -= new EventHandler<OcrAutoRecognizeRunJobEventArgs>(autoRecognizeManager_JobStarted);autoRecognizeManager.JobCompleted -= new EventHandler<OcrAutoRecognizeRunJobEventArgs>(autoRecognizeManager_JobCompleted);Console.WriteLine("All documents finished, check the result files in {0}", documentsDirectory);}}private void autoRecognizeManager_JobStarted(object sender, OcrAutoRecognizeRunJobEventArgs e){// This is not strictly needed in this example, we will// use it to show informationConsole.WriteLine("{0} started...", e.Job.JobData.JobName);// Check if we need to abortif (AbortJobs(e.Job)){// Yes, abort all jobse.Job.AutoRecognizeManager.AbortAllJobs();}}private void autoRecognizeManager_JobCompleted(object sender, OcrAutoRecognizeRunJobEventArgs e){string message = string.Format("{0} completed ", e.Job.JobData.JobName);IOcrAutoRecognizeJob job = e.Job;// Show any errorsif (job.Errors.Count == 0){message += "successfully...";}else{message += "with errors, first error is " + job.Errors[0].Exception.Message;// And save the errors to a text file in the document directorystring documentFileName = job.JobData.DocumentFileName;string textPathName = Path.Combine(Path.GetDirectoryName(documentFileName), Path.GetFileNameWithoutExtension(documentFileName) + "_errors.txt");using (StreamWriter writer = File.CreateText(textPathName)){writer.WriteLine(job.JobData.JobName);writer.WriteLine("Data:");writer.WriteLine(" Image file name: " + job.JobData.ImageFileName);writer.WriteLine(" First page number: " + job.JobData.FirstPageNumber);writer.WriteLine(" Last page number: " + job.JobData.LastPageNumber);writer.WriteLine(" Format:" + job.JobData.Format);writer.WriteLine(" Document file name: " + job.JobData.DocumentFileName);writer.WriteLine("Errors:");foreach (OcrAutoRecognizeManagerJobError error in job.Errors){writer.WriteLine(" Page: {0} during {1}. Error: {2}", error.ImagePageNumber, error.Operation, error.Exception.Message);}}}Console.WriteLine(message);// Decrement the documents count, when we reach 0, we are done// Since this will be called from multiple threads, we need// to use a thread-safety procedureint pending = Interlocked.Decrement(ref _documentsPending);// If we are the last document, wait up main threadif (pending == 0){_allDocumentsFinishedEvent.Set();}}private class JobData{public IOcrAutoRecognizeManager AutoRecognizeManager;public string ImageFileName;public string DocumentFileName;public string JobName;}private void RunJob(object state){JobData data = state as JobData;Console.WriteLine("Running {0}", data.JobName);// Run itOcrAutoRecognizeJobData jobData = new OcrAutoRecognizeJobData(data.ImageFileName, DocumentFormat.Pdf, data.DocumentFileName);jobData.JobName = data.JobName;IOcrAutoRecognizeJob job = data.AutoRecognizeManager.CreateJob(jobData);data.AutoRecognizeManager.RunJob(job);}private bool AbortJobs(IOcrAutoRecognizeJob ocrJob){// In your application, you can check if abortion is required, for example, if the user// has pressed the Cancel button on a progress bar or if your service is shutting down.// In this example, we will never abort, but you can change this code to return true// upon any condition (or when a specific job is about to start)// and the engine will abort all current and pending jobsreturn false;}}static class LEAD_VARS{public const string ImagesDir = @"C:\Users\Public\Documents\LEADTOOLS Images";public const string OcrAdvantageRuntimeDir = @"C:\LEADTOOLS 19\Bin\Common\OcrAdvantageRuntime";}
Imports LeadtoolsImports Leadtools.CodecsImports Leadtools.Forms.OcrImports Leadtools.Forms.DocumentWritersImports Leadtools.FormsImports Leadtools.WinFormsPublic Class RunJobExample' Number of documents that are pendingPrivate _documentsPending As Integer' Event to trigger when all documents are finishedPrivate _allDocumentsFinishedEvent As AutoResetEventPublic Sub Start()Dim imagesDirectory As String = LEAD_VARS.ImagesDirDim documentsDirectory As String = Path.Combine(LEAD_VARS.ImagesDir, "RunJobExample")' Create the output (documents) directoryIf Not Directory.Exists(documentsDirectory) ThenDirectory.CreateDirectory(documentsDirectory)End If' Get all TIF files in input (images) directoryDim imageFileNames As String() = Directory.GetFiles(imagesDirectory, "*.tif")If imageFileNames.Length = 0 ThenConsole.WriteLine("No images to OCR")ReturnEnd If' Create a new OCR engine instanceDim engineType As OcrEngineType = OcrEngineType.AdvantageConsole.WriteLine(String.Format("Starting up {0} engine", engineType))Using ocrEngine As IOcrEngine = OcrEngineManager.CreateEngine(engineType, False)ocrEngine.Startup(Nothing, Nothing, Nothing, LEAD_VARS.OcrAdvantageRuntimeDir)' Setup document PDF save options: Image/Text with CCITT G4 encoding for B/WDim docWriter As DocumentWriter = ocrEngine.DocumentWriterInstanceDim pdfOptions As PdfDocumentOptions = TryCast(docWriter.GetOptions(DocumentFormat.Pdf), PdfDocumentOptions)pdfOptions.ImageOverText = TruepdfOptions.DocumentType = PdfDocumentType.PdfpdfOptions.FontEmbedMode = DocumentFontEmbedMode.NonepdfOptions.OneBitImageCompression = OneBitImageCompressionType.FaxG4docWriter.SetOptions(DocumentFormat.Pdf, pdfOptions)' We are going to use multiple-threads, so disable threading in' IOcrAutoRecognizeManagerDim autoRecognizeManager As IOcrAutoRecognizeManager = ocrEngine.AutoRecognizeManagerautoRecognizeManager.MaximumThreadsPerJob = 1' Tell the recognize manager to continue on errorsautoRecognizeManager.JobErrorMode = OcrAutoRecognizeManagerJobErrorMode.[Continue]' Instead of using events to trigger when documents are done,' we will use the JobCompleted events of IOcrAutoRecognizeManager' to decrement a counter and trigger one event when the counter reaches 0AddHandler autoRecognizeManager.JobStarted, AddressOf autoRecognizeManager_JobStartedAddHandler autoRecognizeManager.JobCompleted, AddressOf autoRecognizeManager_JobCompletedDim count As Integer = imageFileNames.Length_documentsPending = count_allDocumentsFinishedEvent = New AutoResetEvent(False)For i As Integer = 0 To count - 1' Create the job dataDim imageFileName As String = imageFileNames(i)Dim name As String = "Document " & (i + 1).ToString()Console.WriteLine("Queuing {0} file {1}", name, imageFileName)Dim data As New JobData()data.AutoRecognizeManager = autoRecognizeManagerdata.ImageFileName = imageFileNamedata.DocumentFileName = Path.Combine(documentsDirectory, Path.GetFileNameWithoutExtension(imageFileName) & ".pdf")data.JobName = name' Queue this jobThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf RunJob), data)Next' Wait for all documents to finish_allDocumentsFinishedEvent.WaitOne()_allDocumentsFinishedEvent.Close()RemoveHandler autoRecognizeManager.JobStarted, AddressOf autoRecognizeManager_JobStartedRemoveHandler autoRecognizeManager.JobCompleted, AddressOf autoRecognizeManager_JobCompletedConsole.WriteLine("All documents finished, check the result files in {0}", documentsDirectory)End UsingEnd SubPrivate Sub autoRecognizeManager_JobStarted(sender As Object, e As OcrAutoRecognizeRunJobEventArgs)' This is not strictly needed in this example, we will' use it to show informationConsole.WriteLine("{0} started...", e.Job.JobData.JobName)' Check if we need to abortIf AbortJobs(e.Job) Then' Yes, abort all jobse.Job.AutoRecognizeManager.AbortAllJobs()End IfEnd SubPrivate Sub autoRecognizeManager_JobCompleted(sender As Object, e As OcrAutoRecognizeRunJobEventArgs)Dim message As String = String.Format("{0} completed ", e.Job.JobData.JobName)Dim job As IOcrAutoRecognizeJob = e.Job' Show any errorsIf job.Errors.Count = 0 Thenmessage += "successfully..."Elsemessage += "with errors, first error is " + job.Errors(0).Exception.Message' And save the errors to a text file in the document directoryDim documentFileName As String = job.JobData.DocumentFileNameDim textPathName As String =Path.Combine(Path.GetDirectoryName(documentFileName), Path.GetFileNameWithoutExtension(documentFileName) & "_errors.txt")Using writer As StreamWriter = File.CreateText(textPathName)writer.WriteLine(job.JobData.JobName)writer.WriteLine("Data:")writer.WriteLine(" Image file name: " & job.JobData.ImageFileName)writer.WriteLine(" First page number: " & job.JobData.FirstPageNumber)writer.WriteLine(" Last page number: " & job.JobData.LastPageNumber)writer.WriteLine(" Format:" & job.JobData.Format)writer.WriteLine(" Document file name: " & job.JobData.DocumentFileName)writer.WriteLine("Errors:")For Each [error] As OcrAutoRecognizeManagerJobError In job.Errorswriter.WriteLine(" Page: {0} during {1}. Error: {2}", [error].ImagePageNumber, [error].Operation, [error].Exception.Message)NextEnd UsingEnd IfConsole.WriteLine(message)' Decrement the documents count, when we reach 0, we are done' Since this will be called from multiple threads, we need' to use a thread-safety procedureDim pending As Integer = Interlocked.Decrement(_documentsPending)' If we are the last document, wait up main threadIf pending = 0 Then_allDocumentsFinishedEvent.Set()End IfEnd SubPrivate Class JobDataPublic AutoRecognizeManager As IOcrAutoRecognizeManagerPublic ImageFileName As StringPublic DocumentFileName As StringPublic JobName As StringEnd ClassPrivate Sub RunJob(state As Object)Dim data As JobData = TryCast(state, JobData)Console.WriteLine("Running {0}", data.JobName)' Run itDim jobData As New OcrAutoRecognizeJobData(data.ImageFileName, DocumentFormat.Pdf, data.DocumentFileName)jobData.JobName = data.JobNameDim job As IOcrAutoRecognizeJob = data.AutoRecognizeManager.CreateJob(jobData)data.AutoRecognizeManager.RunJob(job)End SubPrivate Function AbortJobs(ocrJob As IOcrAutoRecognizeJob) As Boolean' In your application, you can check if abortion is required, for example, if the user' has pressed the Cancel button on a progress bar or if your service is shutting down.' In this example, we will never abort, but you can change this code to return true' upon any condition (or when a specific job is about to start)' and the engine will abort all current and pending jobsReturn FalseEnd FunctionEnd ClassPublic NotInheritable Class LEAD_VARSPublic Const ImagesDir As String = "C:\Users\Public\Documents\LEADTOOLS Images"Public Const OcrAdvantageRuntimeDir As String = "C:\LEADTOOLS 19\Bin\Common\OcrAdvantageRuntime"End Class
IOcrAutoRecognizeManager Interface
IOcrAutoRecognizeManager Members
Raster .NET | C API | C++ Class Library | JavaScript HTML5
Document .NET | C API | C++ Class Library | JavaScript HTML5
Medical .NET | C API | C++ Class Library | JavaScript HTML5
Medical Web Viewer .NET
