Transym OCR wrapper functions for Harbour
Posted: Tue Mar 28, 2017 11:12 pm
Hello everyone;
I finally got Transym OCR to work from API using (x)Harbour. Here is the code in case anyone is interested in the future.
Reinaldo.
I finally got Transym OCR to work from API using (x)Harbour. Here is the code in case anyone is interested in the future.
Code: Select all
//Reinaldo Crespo-Bazan 3/28/2017 6:39:11 PM
//reinaldo.crespo@gmail.com
//reinaldo.crespo@structuredsystems.com
//This is sample code and wrapper functions to use Transym OCR engine.
//www.Transym.com
#define TOCRJOBTYPE_TIFFFILE 0 // specifies a tiff file
#define TOCRPROCESS_OPTIONS_MERGE 0x00000001 //Merge options ON
#define TOCRPROCESS_OPTIONS_DESKEW 0x00000010 //deskew ON
FUNCTION MAIN()
LOCAL TestTifFile := "sample.tif"
IF !FILE( TestTifFile )
Alert( "testfile sample.tif not found" )
return NIL
ENDIF
MsgInfo( OCRFromFileUsingTransym( TestTifFile ), "TOCR dll results using default options" )
//I find MergeBreakOff and DeskOff turned off works better for some documents.
MsgInfo( OCRFromFileUsingTransym( TestTifFile, ;
TOCRJOBTYPE_TIFFFILE, ;
TOCRPROCESS_OPTIONS_DESKEW | TOCRPROCESS_OPTIONS_MERGE, "TOCR dll results with Merge and Deskew OFF" ) )
RETURN NIL
//-----------------------------------------------------------
//-------------------------------------------------------------------------------------
/**/
#pragma BEGINDUMP
#include <hbapi.h>
#include <windows.h>
#include <TOCRdll.h>
#include <TOCRuser.h>
#include <TOCRerrs.h>
BOOL OCRWait( long JobNo, TOCRJOBINFO2 JobInfo2 );
BOOL GetResults( long JobNo, TOCRRESULTSEX ** Results );
BOOL FormatResults( TOCRRESULTSEX * Results, char * Msg );
//--------------------------------------------------------
//parameters
// 1. input file with image
// 2. type of file to ocr defaults to TIFF
// 3. options mask defaults to 0
//returns OCRed text
HB_FUNC( OCRFROMFILEUSINGTRANSYM )
{
TOCRJOBINFO2 JobInfo2;
TOCRRESULTSEX * Results = 0;
long Status;
long JobNo;
byte OptionsMask = ISNUM( 3 ) ? hb_parnl( 3 ) : 0x00000000 ;
char Msg[8192];
char * InputFile = ( char * ) hb_parcx( 1 ); //parm 1 is input file
//Sets Transym to print error to show errors on screen dialog
TOCRSetConfig( TOCRCONFIG_DEFAULTJOB, TOCRCONFIG_DLL_ERRORMODE, TOCRERRORMODE_MSGBOX );
memset( &JobInfo2, 0, sizeof( TOCRJOBINFO2 ) );
JobInfo2.JobType = ISNUM( 2 ) ? hb_parni( 2 ) : TOCRJOBTYPE_TIFFFILE ;
JobInfo2.InputFile = InputFile ;
//if MergeBreakOff and DeskewOff parameters are sent then change options accordingly.
JobInfo2.ProcessOptions.MergeBreakOff = OptionsMask & 0x00000001 ; //1 bit MergeBreakOff
JobInfo2.ProcessOptions.DeskewOff = OptionsMask>>1 & 0x00000001 ; //2nd bit DeskewOff
Status = TOCRInitialise( &JobNo );
if ( Status == TOCR_OK ) {
if ( OCRWait( JobNo, JobInfo2 ) ) {
if ( GetResults( JobNo, &Results ) ) {
FormatResults( Results, Msg );
hb_xfree( Results );
}
}
TOCRShutdown( JobNo );
}
hb_retc( Msg ) ;
}
//--------------------------------------------------------
BOOL OCRWait( long JobNo, TOCRJOBINFO2 JobInfo2 )
{
long Status;
long JobStatus;
long ErrorMode;
char Msg[4096];
Status = TOCRDoJob2( JobNo, &JobInfo2 );
if (Status == TOCR_OK) {
Status = TOCRWaitForJob(JobNo, &JobStatus);
}
if (Status == TOCR_OK && JobStatus == TOCRJOBSTATUS_DONE)
{
return TRUE;
} else {
// If something hass gone wrong extract the error message and log or return
// not doing anything with it for the time being.
// Check that the OCR engine hasn't already displayed a message first.
TOCRGetConfig(JobNo, TOCRCONFIG_DLL_ERRORMODE, &ErrorMode);
if ( ErrorMode == TOCRERRORMODE_NONE ) {
TOCRGetJobStatusMsg(JobNo, Msg);
}
return FALSE;
}
} // OCRWait()
//--------------------------------------------------------
// Get the results from TOCR
BOOL getresults(long JobNo, long mode, void **Results)
{
long Status;
long ResultsInf;
char Msg[4096];
Status = TOCRGetJobResultsEx(JobNo, mode, &ResultsInf, 0);
if ( Status != TOCR_OK ) return FALSE;
if ( ResultsInf > 0 ) {
// Allocate memory for results
*Results = ( char * ) hb_xgrab( ResultsInf + 1 );
// Retrieve the results
Status = TOCRGetJobResultsEx(JobNo, mode, &ResultsInf, *Results);
if ( Status != TOCR_OK ) {
hb_xfree( Results );
*Results = 0;
return FALSE;
}
} else return FALSE ;
return TRUE;
} // getresults()
//--------------------------------------------------------
// Get extended results
BOOL GetResults( long JobNo, TOCRRESULTSEX **Results )
{
return getresults( JobNo, TOCRGETRESULTS_EXTENDED, (void **)Results );
} // GetResults()
//--------------------------------------------------------
// Convert extended results to a string
BOOL FormatResults(TOCRRESULTSEX *Results, char *Msg)
{
long ItemNo;
long APos = 0;
BOOL Status = FALSE;
if ( Results->Hdr.NumItems > 0 ) {
for (ItemNo = 0; ItemNo < Results->Hdr.NumItems; ItemNo ++ ) {
if ( Results->Item[ItemNo].OCRCha == '\r' )
Msg[APos] = '\n';
else
Msg[APos] = (char)Results->Item[ItemNo].OCRCha;
APos ++;
}
Msg[APos] = 0;
Status = TRUE;
}
return Status;
} // FormatResults()
#pragma ENDDUMP