#include "ltocr.h"
L_LTOCR_API L_INT EXT_FUNCTION L_OcrPage_GetText(page, zoneIndex, text, textLength)
L_OcrPage page; | handle to the OCR page |
L_INT zoneIndex; | zone index |
L_WCHAR** text; | address for L_WCHAR* variable to be updated with zone/page text |
L_UINT* textLength; | address for L_UINT variable to be updated with length of the retrieved text |
Gets the recognition OCR data for a zone in this L_OcrPage as a string.
Parameter | Description |
---|---|
page | Handle to the OCR page. |
zoneIndex | Zero based index of the zone. If you passed -1 for this parameter then all page text will be retrieved. |
text | Address for L_WCHAR* variable to be updated with zone/page text. You must call L_OcrMemory_Free method to free this string buffer when no longer needed. |
textLength | Address for L_UINT variable to be updated with length of the retrieved text. |
SUCCESS | The function was successful. |
< 1 | An error occurred. Refer to Return Codes. |
Gets the recognition OCR data for a zone in this L_OcrPage as a string.
If the passed 'zoneIndex' parameter contains -1 then all page text will be retrieved.
Use this method to get the page result in a simple string. Getting the result as text is helpful in situations when adding zones manually for form processing. For example, suppose the form you are processing has two areas of interests, a name field at coordinates 100, 100, 400, 120 and a social security number at coordinates 100, 200, 400, 220. You can structure your application as follows:
1. Create a new L_OcrPage handle from the form bitmap using L_OcrPage_FromBitmap.
2. Add the name zone manually:
L_OcrZone nameZone = { 0 };
nameZone.StructSize = sizeof(L_OcrZone);
[L_OcrZone_Default](l-ocrzone-default.md)(&nameZone);
nameZone.ZoneType = L_OcrZoneType_Text;
L_RECT rect = {100, 100, 400, 120};
nameZone.Bounds = rect;
L_OcrPage_AddZone(ocrPage, nameZone);
3. Recognize the page (only this one zone will recognized):
4. Get the value of the name field:
L_OcrPage_GetText();
5. Remove the name zone from the page:
6. Repeat the steps from (2) above to get the social security field.
Note: You must call L_OcrMemory_Free method to free this string buffer when no longer needed.
Required DLLs and Libraries
LTOCR For a listing of the exact DLLs and Libraries needed, based on the toolkit version, refer to Files To Be Included With Your Application. |
#define MAKE_IMAGE_PATH(pFileName) TEXT("C:\\Users\\Public\\Documents\\LEADTOOLS Images\\")pFileName
#define OCR_ADVANTAGE_RUNTIME_DIR TEXT("C:\\LEADTOOLS 19\\Bin\\Common\\OcrAdvantageRuntime")
L_INT L_OcrPage_GetTextExample()
{
BITMAPHANDLE bitmap = { 0 };
L_OcrEngine ocrEngine = NULL;
L_OcrPage ocrPage = NULL;
// Create an instance of the engine
std::wcout << L"Creating OCR engine instance...\n";
L_INT retCode = L_OcrEngineManager_CreateEngine(L_OcrEngineType_Advantage, &ocrEngine);
if(retCode != SUCCESS)
return retCode;
// Start the engine using default parameters
std::wcout << L"Starting up OCR engine...\n";
L_OcrEngine_Startup(ocrEngine, NULL, OCR_ADVANTAGE_RUNTIME_DIR);
// Load an image to process
std::wcout << L"Loading page to process...\n";
L_LoadBitmap(MAKE_IMAGE_PATH(L_TEXT("Ocr1.tif")), &bitmap, sizeof(BITMAPHANDLE), 0, ORDER_RGB, NULL, NULL);
// Create an OCR page to handle processing
L_OcrPage_FromBitmap(ocrEngine, &ocrPage, &bitmap, L_OcrBitmapSharingMode_AutoFree, NULL, NULL);
// Transfer ownership
bitmap.Flags.Allocated = 0;
// Find text in the image
std::wcout << L"Finding text within page...\n";
L_OcrPage_AutoZone(ocrPage, NULL, NULL);
// Recognize the text
std::wcout << L"Recognizing the text...\n";
L_OcrPage_Recognize(ocrPage, NULL, NULL);
// Loop through zones and output the text
L_UINT zoneCount = 0;
L_OcrPage_GetZoneCount(ocrPage, &zoneCount);
std::wcout << L"Zone count: " << zoneCount << std::endl;
std::wcout << L"Show recognized text:\n"
<< L"**********************************************\n";
for(L_UINT zoneIndex = 0; zoneIndex < zoneCount; zoneIndex++)
{
L_WCHAR* text = NULL;
L_UINT textLength = 0;
L_OcrPage_GetText(ocrPage, zoneIndex, &text, &textLength);
std::wcout << text << std::endl;
L_OcrMemory_Free(text);
}
std::wcout << L"**********************************************\n";
//CLEANUP
std::wcout << L"Disposing of resources used...";
L_OcrPage_Destroy(ocrPage);
L_OcrEngine_Destroy(ocrEngine);
return SUCCESS;
}