Create an L_OcrPage from a bitmap.
#include "ltocr.h"
L_LTOCR_API L_INT EXT_FUNCTION L_OcrPage_FromBitmap(engine, page, bitmapHandle, sharingMode, callback, userData)
Handle to the OCR engine.
Address of L_OcrPage variable to be updated with the created OCR page handle.
Source bitmap handle. This parameter cannot be NULL.
Options to determine the ownership of image.
Optional callback to show operation progress.
Optional user data to pass to the callback function.
Value | Meaning |
---|---|
SUCCESS | The function was successful. |
< 1 | An error occurred. Refer to Return Codes. |
Use this function to quickly create an L_OcrPage from a bitmap handle directly, call the necessary method (such as L_OcrPage_Recognize) and then obtain the text directly using L_OcrPage_GetText or L_OcrPage_GetRecognizedCharacters.
To save the results of L_OcrPage to a document file such as PDF or Microsoft Word by directly, you must create an L_OcrDocument handle in file mode and add the page to it using L_OcrDocument_AddPage.
You must call L_OcrPage_Destroy after you finish using this L_OcrPage to free its memory.
The value of sharingMode determines what happens to the bitmap handle after it has been used to create the page as follows:
L_OcrImageSharingMode_None
The resulting L_OcrPage does not own the bitmap handle. When L_OcrPage is destroyed, the bitmap is not freed. This mode is useful for quickly creating an OCR page (and getting the recognition result) from a bitmap that is already used in other parts of your application. Using this option will save memory because you do not have to create a copy of the image and the page will use the same image data when performing recognition.
Important: In this mode, it is the user's responsibility to keep the bitmap handle alive for the whole duration the page. Only free the bitmap after the L_OcrPage object is destroyed and no longer used.
L_OcrBitmapSharingMode_AutoFree
The resulting L_OcrPage owns the image. When L_OcrPage is destroyed, the bitmap is freed as well. This mode is useful when the bitmap is no longer used by other parts of your application. For example, the bitmap is obtained from scanning or the camera and is only to be used for OCRing. Using this option will transfer the ownership of the bitmap handle to the page and it will be freed when the page is destroyed.
static L_INT CreateMultiPageFile(L_TCHAR* outputFileName)
{
// Create a muti-page TIF from Ocr1.tif, Ocr2.tif, Ocr3.tif and Ocr4.tif
HBITMAPLIST bitmapList;
L_INT retCode = -1;
SAVEFILEOPTION saveOptions = {0};
retCode = L_CreateBitmapList(&bitmapList);
if(retCode != SUCCESS)
return retCode;
BITMAPHANDLE bitmaps[4];
L_TCHAR *imageFiles[] = {MAKE_IMAGE_PATH(L_TEXT("OCR1.tif")),
MAKE_IMAGE_PATH(L_TEXT("OCR2.tif")),
MAKE_IMAGE_PATH(L_TEXT("OCR3.tif")),
MAKE_IMAGE_PATH(L_TEXT("OCR4.tif"))};
for(L_INT i = 0; i < _countof(imageFiles); i++)
{
retCode = L_LoadBitmap(imageFiles[i], &bitmaps[i], sizeof(BITMAPHANDLE), 0, ORDER_RGB, NULL, NULL);
if(retCode != SUCCESS)
return retCode;
retCode = L_InsertBitmapListItem(bitmapList, (L_UINT)-1, &bitmaps[i]);
if(retCode != SUCCESS)
goto CLEANUP;
}
// Save the output file
retCode = L_GetDefaultSaveFileOption(&saveOptions, sizeof(SAVEFILEOPTION));
if(retCode != SUCCESS)
goto CLEANUP;
// Save all pages
saveOptions.PageNumber = -1;
retCode = L_SaveBitmapList(outputFileName, bitmapList, FILE_CCITT_GROUP4, 1, 0, &saveOptions);
CLEANUP:
//free all bitmaps in list & dispose of handle
L_DestroyBitmapList(bitmapList);
return retCode;
}
L_INT L_OcrPage_FromBitmapExample()
{
L_INT retCode = -1;
L_OcrEngine ocrEngine = NULL;
L_OcrDocumentManager ocrDocumentManager = NULL;
L_OcrDocument ocrDocument = NULL;
L_OcrPage ocrPage = NULL;
LOADFILEOPTION loadOpts;
FILEINFO fileInfo;
// For this example, we need a multi-page TIF file.
// If you have a different sample file, replace the file name below
L_TCHAR* multiPageFile = MAKE_IMAGE_PATH(L_TEXT("Ocr.tif"));
if(CreateMultiPageFile(multiPageFile) != SUCCESS)
return FAILURE;
// Create an instance of the engine
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)
{
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;
// Load each page of the multi-page tif file we created and process them
L_GetDefaultLoadFileOption(&loadOpts, sizeof(LOADFILEOPTION));
retCode = L_FileInfo(multiPageFile, &fileInfo, sizeof(FILEINFO), FILEINFO_TOTALPAGES, &loadOpts);
if(retCode != SUCCESS)
goto CLEANUP;
for(L_INT pageNum = 1; pageNum <= fileInfo.TotalPages; pageNum++)
{
// Load page
BITMAPHANDLE bitmap = { 0 };
loadOpts.PageNumber = pageNum;
retCode = L_LoadBitmap(multiPageFile, &bitmap, sizeof(BITMAPHANDLE), 0, ORDER_RGB, &loadOpts, &fileInfo);
if(retCode != SUCCESS)
goto CLEANUP;
// Create Ocr page
retCode = L_OcrPage_FromBitmap(ocrEngine, &ocrPage, &bitmap, L_OcrBitmapSharingMode_AutoFree, NULL, NULL);
if(retCode != SUCCESS)
{
L_FreeBitmap(&bitmap);
goto CLEANUP;
}
// Transfer ownership to the OCR page
bitmap.Flags.Allocated = 0;
// Find zones in the page
retCode = L_OcrPage_AutoZone(ocrPage, NULL, NULL);
if(retCode != SUCCESS)
goto CLEANUP;
// Recognize
retCode = L_OcrPage_Recognize(ocrPage, NULL, 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;
//Destroy the page to free the bitmap
L_OcrPage_Destroy(ocrPage);
ocrPage = NULL;
}
// Save
retCode = L_OcrDocument_Save(ocrDocument, MAKE_IMAGE_PATH(L_TEXT("Ocr.pdf")), DOCUMENTFORMAT_PDF, NULL, NULL);
}
CLEANUP:
if(ocrPage != NULL)
L_OcrPage_Destroy(ocrPage);
if(ocrDocument != NULL)
L_OcrDocument_Destroy(ocrDocument);
if(ocrEngine != NULL)
L_OcrEngine_Destroy(ocrEngine);
return retCode;
}