Working with Recognition Results (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_GET_OUTPUTFORMAT

Get Output Format

 

IDC_SAVE_DOCUMENT

Save Document

 

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_GET_OUTPUTFORMAT.

 

d.

In the Messages list box, select BN_CLICKED

 

e.

Click the Add function button. Choose OK for the default function name (OnGetOutputformat).

 

f.

Click the Edit Code button and code the OnGetOutputformat procedure as follows:

   CString csBuffer;
   nRet = m_pRasterDoc->GetOutputFileFormats ();
   if (nRet != 0)
   {
      csBuffer.Format (TEXT("Error %d in enumerating available file formats"), 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_SAVE_DOCUMENT.

 

d.

In the Messages list box, select BN_CLICKED

 

e.

Click the Add function button. Choose OK for the default function name (OnSaveDocument).

 

f.

Click the Edit Code button and code the OnSaveDocument procedure as follows:

   CString csBuffer;

   int nRet = m_pRasterDoc->GetRecognitionResultOptions ();
   if (nRet == 0) 
   {
      m_pRasterDoc->GetResultOptions ()->FormatLevel = FORMATLEVEL_FULL; 
      m_pRasterDoc->GetResultOptions()->GetDocumentOptions ()->PaperSizeMode = SELPREDEFINED; 
      m_pRasterDoc->GetResultOptions ()->GetDocumentOptions ()->PaperType = PAPER_A4; 

      m_pRasterDoc->SetRecognitionResultOptions ();
      nRet = m_pRasterDoc->SaveDocument ("c:\\test.doc");
      if (nRet != 0) 
      {
         csBuffer.Format (TEXT("Error %d in saving recognition result to a file"), nRet); 
         AfxMessageBox (csBuffer); 
      }
   }

5.

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 OnEnumOutputFileFormats (FORMATTYPE Format);

 

f.

Edit the source for the Sink class:

 

g.

In the Project Workspace, click the FileView tab.

 

h.

Double-click the Source Files folder.

 

i.

Double-click the RasterOCRSink.cpp file to edit it.

 

j.

Add the following to the DISPATCH_MAP:

DISP_FUNCTION_ID(CRasterOCRSink," EnumOutputFileFormats",3, OnEnumOutputFileFormats, VT_EMPTY, VTS_I2)

 

k.

Add the following to the end of the file:

   int nRet; 
   CString csBuffer; 

   nRet = this->m_pDlg->m_pRasterDoc->GetFileFormatInfo (Format); 
   if (nRet == 0) 
   {
      csBuffer.Format (TEXT("Format Name = %s\nFormat DLL Name = %s\nFormat Ext Name = %s\n"),
               m_pDlg->m_pRasterDoc->GetFileFormatInfo ()->FormatName
               m_pDlg->m_pRasterDoc->GetFileFormatInfo ()->DLLName
               m_pDlg->m_pRasterDoc->GetFileFormatInfo ()->ExtName); 

      AfxMessageBox(csBuffer); 

FORMAT_OUTPUT_TYPE type = m_pDlg->m_pRasterDoc->GetFileFormatInfo ()->Type

      switch (type) 
      {
      case OUTTYPE_PLAIN: 
         csBuffer.Format (TEXT("Format Type = Plain"));
         break; 
      case OUTTYPE_WORD_PROCESSOR: 
         csBuffer.Format (TEXT("Format Type = Word Processor"));
         break; 
      case OUTTYPE_TRUE_WORD_PROCESSOR: 
         csBuffer.Format (TEXT("Format Type = True Word Processor"));
         break; 
      case OUTTYPE_TABLE: 
         csBuffer.Format (TEXT("Format Type = Table"));
         break; 
      case OUTTYPE_UNKNOWN: 
         csBuffer.Format (TEXT("Format Type = Unknown"));
         break; 
      }

      AfxMessageBox(csBuffer); 
   }

6.

Build OCRTutor.exe.

7.

Run OCRTutor.exe.