Products | Support | Email a link to this topic. | Send comments on this topic. | Back to Introduction - All Topics | Help Version 19.0.6.9
|
Leadtools.Forms.Ocr Assembly > Leadtools.Forms.Ocr Namespace : OcrProgressCallback Delegate |
public delegate void OcrProgressCallback( IOcrProgressData data )
'Declaration
Public Delegate Sub OcrProgressCallback( _ ByVal data As IOcrProgressData _ )
'Usage
Dim instance As New OcrProgressCallback(AddressOf HandlerMethod)
public delegate void OcrProgressCallback( IOcrProgressData data )
typedef void (^LTOcrProgressHandler)(LTOcrProgressData *progressData)
void OcrProgressListener.onProgress(OcrProgressData data)
OcrProgressCallback( data )
public delegate void OcrProgressCallback( IOcrProgressData^ data )
The OCR engine will continuously invoke the callback procedure during the different OCR operations with an instance of IOcrProgressData providing the current processing stage and a percentage indicator value. Use this function to indicate progress of application operations.
The callback can set the IOcrProgressData.Status property at any time to OcrProgressStatus.Abort to abort the OCR process and cancel all pending operations.
This example will display a simple "Processing" Windows Forms dialog that can be used to show OCR operation progress as well as allow the user to abort the current operation. You can plug this dialog into your application and call it in response to user-interface commands that correspond to OCR operations.
Imports Leadtools Imports Leadtools.Codecs Imports Leadtools.Forms.Ocr Imports Leadtools.Forms.DocumentWriters Imports Leadtools.Forms Imports Leadtools.WinForms ' Text writer to save the log to Private _log As StreamWriter <TestMethod> Public Sub OcrProgressCallbackExample() Dim logFileName As String = Path.Combine(LEAD_VARS.ImagesDir, "log.txt") Dim multiPageTifFileName As String = Path.Combine(LEAD_VARS.ImagesDir, "Ocr.tif") Dim pdfFileName As String = Path.Combine(LEAD_VARS.ImagesDir, "Ocr1.pdf") ' Create the log text writer _log = File.CreateText(logFileName) ' Create an instance of the engine Using ocrEngine As IOcrEngine = OcrEngineManager.CreateEngine(OcrEngineType.Advantage, False) ' Start the engine using default parameters ocrEngine.Startup(Nothing, Nothing, Nothing, LEAD_VARS.OcrAdvantageRuntimeDir) ' Create an OCR document Using ocrDocument As IOcrDocument = ocrEngine.DocumentManager.CreateDocument() ' Add this image to the document _log.WriteLine("Adding the pages") _log.WriteLine("********************************") ocrDocument.Pages.AddPages(multiPageTifFileName, 1, -1, New OcrProgressCallback(AddressOf MyOcrProgressCallback)) ' Auto-recognize the zones in all the pages _log.WriteLine("Auto-zoning") _log.WriteLine("********************************") ocrDocument.Pages.AutoZone(New OcrProgressCallback(AddressOf MyOcrProgressCallback)) ' Recognize it and save it as PDF _log.WriteLine("Recognizing") _log.WriteLine("********************************") ocrDocument.Pages.Recognize(New OcrProgressCallback(AddressOf MyOcrProgressCallback)) _log.WriteLine("Saving to PDF") _log.WriteLine("********************************") ocrDocument.Save(pdfFileName, DocumentFormat.Pdf, New OcrProgressCallback(AddressOf MyOcrProgressCallback)) End Using ' Shutdown the engine ' Note: calling Dispose will also automatically shutdown the engine if it has been started ocrEngine.Shutdown() End Using _log.WriteLine("********************************") _log.WriteLine("Complete") _log.Flush() _log.Close() End Sub Private Sub MyOcrProgressCallback(data As IOcrProgressData) If data.Percentage = 0 Then _log.WriteLine("--------------------------") End If _log.WriteLine("Page:{0}({1}:{2}) {3}% Operation:{4}", _ data.CurrentPageIndex.ToString("00"), _ data.FirstPageIndex.ToString("00"), _ data.LastPageIndex.ToString("00"), _ data.Percentage.ToString("000"), _ data.Operation) End Sub Public NotInheritable Class LEAD_VARS Public Const ImagesDir As String = "C:\Users\Public\Documents\LEADTOOLS Images" Public Const OcrAdvantageRuntimeDir As String = "C:\LEADTOOLS 19\Bin\Common\OcrAdvantageRuntime" End Class
using Leadtools; using Leadtools.Codecs; using Leadtools.Forms.Ocr; using Leadtools.Forms.DocumentWriters; using Leadtools.Forms; using Leadtools.WinForms; public void OcrProgressCallbackExample() { string logFileName = Path.Combine(LEAD_VARS.ImagesDir, "log.txt"); string multiPageTifFileName = Path.Combine(LEAD_VARS.ImagesDir, "Ocr.tif"); string pdfFileName = Path.Combine(LEAD_VARS.ImagesDir, "Ocr1.pdf"); // Create the log text writer _log = File.CreateText(logFileName); // Create an instance of the engine using (IOcrEngine ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.Advantage, false)) { // Start the engine using default parameters ocrEngine.Startup(null, null, null, LEAD_VARS.OcrAdvantageRuntimeDir); // Create an OCR document using (IOcrDocument ocrDocument = ocrEngine.DocumentManager.CreateDocument()) { // Add this image to the document _log.WriteLine("Adding the pages"); _log.WriteLine("********************************"); ocrDocument.Pages.AddPages(multiPageTifFileName, 1, -1, new OcrProgressCallback(MyOcrProgressCallback)); // Auto-recognize the zones in all the pages _log.WriteLine("Auto-zoning"); _log.WriteLine("********************************"); ocrDocument.Pages.AutoZone(new OcrProgressCallback(MyOcrProgressCallback)); // Recognize it and save it as PDF _log.WriteLine("Recognizing"); _log.WriteLine("********************************"); ocrDocument.Pages.Recognize(new OcrProgressCallback(MyOcrProgressCallback)); _log.WriteLine("Saving to PDF"); _log.WriteLine("********************************"); ocrDocument.Save(pdfFileName, DocumentFormat.Pdf, new OcrProgressCallback(MyOcrProgressCallback)); } // Shutdown the engine // Note: calling Dispose will also automatically shutdown the engine if it has been started ocrEngine.Shutdown(); } _log.WriteLine("********************************"); _log.WriteLine("Complete"); _log.Flush(); _log.Close(); } // Text writer to save the log to private StreamWriter _log; private void MyOcrProgressCallback(IOcrProgressData data) { if (data.Percentage == 0) _log.WriteLine("--------------------------"); _log.WriteLine("Page:{0}({1}:{2}) {3}% Operation:{4}", data.CurrentPageIndex.ToString("00"), data.FirstPageIndex.ToString("00"), data.LastPageIndex.ToString("00"), data.Percentage.ToString("000"), data.Operation); } static class LEAD_VARS { public const string ImagesDir = @"C:\Users\Public\Documents\LEADTOOLS Images"; public const string OcrAdvantageRuntimeDir = @"C:\LEADTOOLS 19\Bin\Common\OcrAdvantageRuntime"; }
using Leadtools; using Leadtools.Codecs; using Leadtools.Forms.Ocr; using Leadtools.Forms.DocumentWriters; using Leadtools.Forms; // Text writer to save the log to private DataWriter _log; private IRandomAccessStream _fileStream; private IOutputStream _outputStream; public async Task OcrProgressCallbackExample() { string logFileName = @"log.txt"; string tifFileName = @"Assets\Ocr1.tif"; string pdfFileName = @"Ocr1.pdf"; // Create the log text writer StorageFile file = await Tools.AppLocalFolder.CreateFileAsync(logFileName); _fileStream = await file.OpenAsync(FileAccessMode.ReadWrite); _outputStream = _fileStream.GetOutputStreamAt(0); _log = new DataWriter(_outputStream); // Create an instance of the engine IOcrEngine ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.Advantage, false); // Start the engine using default parameters ocrEngine.Startup(null, null, String.Empty, Tools.OcrEnginePath); // Create an OCR document IOcrDocument ocrDocument = ocrEngine.DocumentManager.CreateDocument(); // Add this image to the document _log.WriteString("Adding the pages\r\n"); _log.WriteString("********************************\r\n"); using (RasterCodecs codecs = new RasterCodecs()) { StorageFile loadFile = await Tools.AppInstallFolder.GetFileAsync(tifFileName); using (RasterImage image = await codecs.LoadAsync(LeadStreamFactory.Create(loadFile))) ocrDocument.Pages.AddPage(image, new OcrProgressCallback(MyOcrProgressCallback)); } // Auto-recognize the zones in all the pages _log.WriteString("Auto-zoning\r\n"); _log.WriteString("********************************\r\n"); ocrDocument.Pages.AutoZone(new OcrProgressCallback(MyOcrProgressCallback)); // Recognize it and save it as PDF _log.WriteString("Recognizing\r\n"); _log.WriteString("********************************\r\n"); ocrDocument.Pages.Recognize(new OcrProgressCallback(MyOcrProgressCallback)); _log.WriteString("Saving to PDF\r\n"); _log.WriteString("********************************\r\n"); StorageFile saveFile = await Tools.AppLocalFolder.CreateFileAsync(pdfFileName, CreationCollisionOption.ReplaceExisting); await ocrDocument.SaveAsync(LeadStreamFactory.Create(saveFile), DocumentFormat.Pdf, new OcrProgressCallback(MyOcrProgressCallback)); // Shutdown the engine ocrEngine.Shutdown(); _log.WriteString("********************************\r\n"); _log.WriteString("Complete\r\n"); await _log.StoreAsync(); _log.DetachStream(); await _outputStream.FlushAsync(); _fileStream.Dispose(); } private void MyOcrProgressCallback(IOcrProgressData data) { if(data.Percentage == 0) _log.WriteString("--------------------------\r\n"); _log.WriteString(string.Format("Page:{0}({1}:{2}) {3}% Operation:{4}\r\n", data.CurrentPageIndex, data.FirstPageIndex, data.LastPageIndex, data.Percentage, data.Operation)); }