Recognizing Zones(Visual C++ 5.0 and later)
Take the following steps to add code to the existing project that will let you recognize pages:
1. |
Start with the program you created in Working with Zones. | |
2. |
Add the following command buttons to the main window and set the ID and Caption properties as follows: | |
|
ID |
Caption |
|
IDC_RECOGNIZE |
Recognize |
|
IDC_GETSTATUS |
Get Status |
3. |
Press Ctrl-W to go to the MFC Class Wizard; then do the following: | |
|
a. |
Click the Message Maps tab. |
|
b. |
In the Class Name combo box, select COCRTutorDlg. |
|
c. |
In the Object IDs list box, select IDC_RECOGNIZE. |
|
d. |
In the Messages list box, select BN_CLICKED |
|
e. |
Click the Add function button. Choose OK for the default function name (OnRecognize). |
|
f. |
Click the Edit Code button and code the OnRecognize procedure as follows: |
CString csBuffer;
m_pRasterDoc->SpellLanguageID = LANGID_ENGLISH;
m_pRasterDoc->EnableSubSystem = TRUE;
m_pRasterDoc->EnableCorrection = TRUE;
m_pRasterDoc->RecognitionDataFileName = "c:\\testrdf.rdf";
int nRet = m_pRasterDoc->Recognize (0,1);
if(nRet == 0)
AfxMessageBox(TEXT("The engine finished recognizing the specified pages successfully."));
else
{
csBuffer.Format (TEXT("Error %d in recognized specified pages"), nRet);
AfxMessageBox (csBuffer);
}
4. |
Press Ctrl-W to go to the MFC Class Wizard; then do the following: |
|
a. |
Click the Message Maps tab. |
|
b. |
In the Class Name combo box, select COCRTutorDlg. |
|
c. |
In the Object IDs list box, select IDC_GETSTATUS. |
|
d. |
In the Messages list box, select BN_CLICKED |
|
e. |
Click the Add function button. Choose OK for the default function name (OnGetstatus). |
|
f. |
Click the Edit Code button and code the OnGetstatus procedure as follows: |
CString csBuffer;
int nRet = m_pRasterDoc->GetStatus ();
if (nRet == 0)
{
csBuffer.Format (TEXT("Recognition Time = %d\nTotal Recognized Characters = %d\nTotal Recognized Words = %d\nTotal Rejected Characters = %d\nTotal Rejected Words = %d\n"),
m_pRasterDoc->GetRecognizeStatus()->RecognizedTime,
m_pRasterDoc->GetRecognizeStatus()->RecognizedCharacterCount,
m_pRasterDoc->GetRecognizeStatus()->RecognizedWordCount,
m_pRasterDoc->GetRecognizeStatus()->RejectCharacterCount,
m_pRasterDoc->GetRecognizeStatus()->RejectWordCount);
AfxMessageBox(csBuffer);
}
5. |
Edit the OCRTutorDlg.h file and change the definition of COCRTutorDlg : CDialog by inserting the following line after DECLARE_MESSAGE_MAP: |
public:
CRasterOCRSink * m_pRasterOCRSink;
IConnectionPoint *m_pOCRCP;
DWORD m_dwOCRCookie;
6. |
Press Ctrl-W to go to the MFC Class Wizard; then do the following: | |
|
a. |
Click the Add Class button. |
|
b. |
Click New.... |
|
c. |
Type CRasterOCRSink for the name of the class. |
|
d. |
Select CCmdTarget for the base class of the new class. |
|
e. |
Under Automation, click the Automation radio button. |
|
f. |
Click OK to create the class. |
|
g. |
In the RasterOCRSink.h file, move the destructor so that it is public: |
// Implementation
virtual ~ CRasterOCRSink();
protected:
|
h. |
In the RasterOCRSink.h file, add the following to the top of the file: |
class COCRTutorDlg;
|
i. |
In the RasterOCRSink.h file, add the following to the CRasterOCRSink class in the //Attributes public section: |
// Attributes
public:
COCRTutorDlg *m_pDlg;
|
j. |
In the RasterOCRSink.cpp file, add the following to the top of the file (after the #include "RasterOCRSink.h") |
#include "OCRTutorDlg.h"
7. |
Add #include statements so you can access the new class: |
|
a. |
In the Project Workspace, click the FileView tab. |
|
b. |
Double-click the OCRTutor files folder to open it. |
|
c. |
Double-click the Header files folder to open it. |
|
d. |
Double-click the StdAfx.h file to edit it. |
|
e. |
Add the following lines to the end of the file: |
#include "RasterOCRSink.h"
8. |
Edit the header for the Sink class: | |
|
a. |
In the Project Workspace, click the FileView tab. |
|
b. |
Double-click the OCRTutor Files folder to open it. |
|
c. |
Double-click the Header Files folder to open it. |
|
d. |
Double-click the RasterOCRSink.h file to edit it. |
|
e. |
Add the following just before //}}AFX_MSG: |
afx_msg void OnRecognitionStatus (short iRecogPage, short iError);
|
f. |
Double-click the Source Files folder to open it. |
|
g. |
Double-click the RasterOCRSink.cpp file to edit it. |
|
h. |
Inside the BEGIN_INTERFACE_MAP section, change the INTERFACE_PART to the following: |
INTERFACE_PART(CRasterOCRSink, DIID__LEADRasterDocEvents, Dispatch)
9. |
Edit the source for the Sink class: | |
|
a. |
In the Project Workspace, click the FileView tab. |
|
b. |
Double-click the Source Files folder. |
|
c. |
Double-click the RasterOCRSink.cpp file to edit it. |
|
d. |
Add the following to the DISPATCH_MAP: |
DISP_FUNCTION_ID(CRasterOCRSink,"RecognitionStatus",2, OnRecognitionStatus,VT_EMPTY, VTS_I2 VTS_I2)
Add the following to the end of the file:
void CRasterOCRSink::OnRecognitionStatus (short iRecogPage, short iError)
{
CString csBuffer;
csBuffer.Format (TEXT("The page # %d has finished its recognition.\nThe recognition return code = %d\n"), iRecogPage, iError);
AfxMessageBox(csBuffer);
// continue to recognize next page
m_pDlg->m_pRasterDoc->EnableStopRecognizeStatus = FALSE;
}
10. |
Edit the OCRTutorDlg.cpp file and add the following to the OnStartup procedure: |
m_pRasterOCRSink = new CRasterOCRSink;
m_pRasterOCRSink->m_pDlg = this;
LPUNKNOWN pUnkSink = m_pRasterOCRSink->GetIDispatch(FALSE);
AfxConnectionAdvise(m_pRasterDoc, DIID__LEADRasterOCREvents, pUnkSink, FALSE, &m_dwOCRCookie);
11. |
Edit the OCRTutorDlg.cpp file and add the following to the OnShutdown procedure: |
LPUNKNOWN pUnkSink = m_pRasterOCRSink->GetIDispatch(FALSE);
AfxConnectionUnadvise(m_pRasterDoc, DIID__LEADRasterOCREvents, pUnkSink, FALSE, m_dwOCRCookie);
delete m_pRasterOCRSink;
m_pRasterOCR->Release ();
12. |
On the Build menu, select Build OCRTutor.exe. |
13. |
On the Build menu, select Execute OCRTutor.exe. |
14. |
Save this project to use for testing other code samples. |