LEADTOOLS has defined the following data types for referencing an OCR engine handle:
Data Type | Definition and Usage |
---|---|
L_OcrEngine | Defined as Handle, used as a handle of the OCR engine. |
L_INT EXT_CALLBACK MyOcrProgressCallback(L_OcrProgressData* data, L_VOID* /*userData*/)
{
if (data->Percentage == 0)
std::cout << "--------------------------\n";
std::wcout << L"Page:" << data->CurrentPageIndex << L"(" << data->FirstPageIndex << L":" << data->LastPageIndex << L") "
<< data->Percentage << L"% Operation: ";
switch (data->Operation)
{
case L_OcrProgressOperation_LoadImage:
std::wcout << L"L_OcrProgressOperation_LoadImage" << std::endl;
break;
case L_OcrProgressOperation_SaveImage:
std::wcout << L"L_OcrProgressOperation_SaveImage" << std::endl;
break;
case L_OcrProgressOperation_PreprocessImage:
std::wcout << L"L_OcrProgressOperation_PreprocessImage" << std::endl;
break;
case L_OcrProgressOperation_AutoZone:
std::wcout << L"L_OcrProgressOperation_AutoZone" << std::endl;
break;
case L_OcrProgressOperation_Recognize:
std::wcout << L"L_OcrProgressOperation_Recognize" << std::endl;
break;
case L_OcrProgressOperation_SaveDocumentPrepare:
std::wcout << L"L_OcrProgressOperation_SaveDocumentPrepare" << std::endl;
break;
case L_OcrProgressOperation_SaveDocument:
std::wcout << L"L_OcrProgressOperation_SaveDocument" << std::endl;
break;
case L_OcrProgressOperation_SaveDocumentConvertImage:
std::wcout << L"L_OcrProgressOperation_SaveDocumentConvertImage" << std::endl;
break;
case L_OcrProgressOperation_Formatting:
std::wcout << L"L_OcrProgressOperation_Formatting" << std::endl;
break;
case L_OcrProgressOperation_RecognizeOMR:
std::wcout << L"L_OcrProgressOperation_RecognizeOMR" << std::endl;
break;
}
return SUCCESS;
}
L_INT L_OcrEngineExample()
{
BITMAPHANDLE bitmap = { 0 };
L_OcrEngine ocrEngine = NULL;
L_OcrPage ocrPage = NULL;
L_OcrDocumentManager ocrDocumentManager = NULL;
L_OcrDocument ocrDocument = NULL;
// Create an instance of the engine
L_INT retCode = L_OcrEngineManager_CreateEngine(L_OcrEngineType_LEAD, &ocrEngine);
if(retCode != SUCCESS)
return retCode;
// Start the engine using default parameters
retCode = L_OcrEngine_Startup(ocrEngine, NULL, OCR_LEAD_RUNTIME_DIR);
if(retCode != SUCCESS)
goto CLEANUP;
// Load an image to process
retCode = L_LoadBitmap(MAKE_IMAGE_PATH(L_TEXT("Ocr1.tif")), &bitmap, sizeof(BITMAPHANDLE), 0, ORDER_RGB, NULL, NULL);
if(retCode != SUCCESS)
goto CLEANUP;
// Add an image to OCR page. Transfer ownership of the bitmap to the page
retCode = L_OcrPage_FromBitmap(ocrEngine, &ocrPage, &bitmap, L_OcrBitmapSharingMode_AutoFree, MyOcrProgressCallback, NULL);
if(retCode != SUCCESS)
goto CLEANUP;
// We have a valid page and bitmap ownership has transfered. So, we do not need to free the bitmap anymore.
// Bitmap will be freed when ocrPage is destroyed.
bitmap.Flags.Allocated = 0;
// Automatically find areas/zones on the page where text is located
retCode = L_OcrPage_AutoZone(ocrPage, MyOcrProgressCallback, NULL);
if(retCode != SUCCESS)
goto CLEANUP;
// 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
retCode = L_OcrPage_Recognize(ocrPage, MyOcrProgressCallback, NULL);
if(retCode != SUCCESS)
goto CLEANUP;
retCode = L_OcrEngine_GetDocumentManager(ocrEngine, &ocrDocumentManager);
if(retCode != SUCCESS)
goto CLEANUP;
// Create an OCR document
retCode = L_OcrDocumentManager_CreateDocument(ocrDocumentManager, &ocrDocument, L_OcrCreateDocumentOptions_AutoDeleteFile, NULL);
if(retCode != SUCCESS)
goto CLEANUP;
// In Document File Mode, add OcrPage to OcrDocument after recognition
retCode = L_OcrDocument_AddPage(ocrDocument, ocrPage);
if(retCode != SUCCESS)
goto CLEANUP;
// Adding the page to a file based document will take a snap shot of the recognition data and store it in the document. At this
// point, the page is no longer needed. So destroy it to free up memory not used anymore
L_OcrPage_Destroy(ocrPage);
// Set the handle to NULL so we do not free it in our clean-up code
ocrPage = NULL;
// Save the document we have as PDF
retCode = L_OcrDocument_Save(ocrDocument, MAKE_IMAGE_PATH(L_TEXT("Ocr1.pdf")), DOCUMENTFORMAT_PDF, MyOcrProgressCallback, NULL);
CLEANUP:
if(bitmap.Flags.Allocated)
L_FreeBitmap(&bitmap);
if(ocrPage != NULL)
L_OcrPage_Destroy(ocrPage);
if(ocrDocument != NULL)
L_OcrDocument_Destroy(ocrDocument);
if(ocrEngine != NULL)
L_OcrEngine_Destroy(ocrEngine);
return retCode;
}