Click here to Skip to main content
11,576,977 members (52,068 online)
Click here to Skip to main content

OCR With MODI in Visual C++

, 23 Jan 2007 135K 11.1K 57
Rate this:
Please Sign up or sign in to vote.
An article on how to use Microsoft Office Document Imaging Library (MODI) for OCR in Visual C++

MODI VC Demo

Introduction

Microsoft Office Document Imaging Library (MODI) which comes with the Office 2003 package, allows us easily integrate OCR functionality into our own applications. Although there is a good C# sample: "OCR with Microsoft® Office" posted on this web site, I would need something in C++. After searching on the Internet and the Microsoft web site and can't find anything good regarding MODI's OCR for Visual C++. I decided to dig this thing out and write this sample demo program to show the basic thing of MODI's OCR feature. I believe that some people may be interested in this program, so, I post it on the codeproject web site to share the common interest.

Project Background

This project was firstly started in Visual C++ 6.0 and then updated to Visual Studio .Net 2003 and I have included two project file in the demo program. To run it in Visual C++ 6.0, open MODIVCDemo.dsp manually.

Build Project and Use Code

Add MODI Active-X into the project

In visual C++ 6.0, click "Project->Add To Project->Components and Controls->Registered ActiveX Control" and select MODI ActiveX as shown below.

 MODI Active-X Control

Mapping Active-X into the project

MODI Active-X Control Mapping

Once map MODI Active-X control into the project, all Active-X control wrapped classes will be automatically added into the project.

HOW TO OCR it in Visual C++.

Following is the sample code showing how to use MODI for OCR.

BOOL CMODIVC6Dlg::bReadOCRByMODIAXCtrl(CString csFilePath, 
                                       CString &csText)
{
   BOOL bRet = TRUE;
   HRESULT hr = 0;
   csText.Empty();

   IUnknown *pVal = NULL;
   IDocument *IDobj = NULL;
   ILayout *ILayout = NULL;
   IImages *IImages = NULL;
   IImage *IImage = NULL;
   IWords *IWords = NULL;
   IWord *IWord = NULL;

   pVal = (IUnknown *) m_MIDOCtrl.GetDocument(); 

   if ( pVal != NULL )
   {
      //Already has image in it, Don't need to create again
      //Just get IDocument interface
      pVal->QueryInterface(IID_IDocument,(void**) &IDobj);
      if ( SUCCEEDED(hr) )
      {
         hr = IDobj->OCR(miLANG_SYSDEFAULT,1,1);

         if ( SUCCEEDED(hr) )
         {
            IDobj->get_Images(&IImages);
            long iImageCount=0;
    
            Images->get_Count(&iImageCount);
            for ( int img =0; img<iImageCount;img++)
            {
               IImages->get_Item(img,(IDispatch**)&IImage);
               IImage->get_Layout(&ILayout);

               long numWord=0;
               ILayout->get_NumWords(&numWord);
               ILayout->get_Words(&IWords);

               IWords->get_Count(&numWord);

               for ( long i=0; i<numWord;i++)
               {
                  IWords->get_Item(i,(IDispatch**)&IWord);
                  CString csTemp;
                  BSTR result;
                  IWord->get_Text(&result);
                  char buf[256];
                  sprintf(buf,"%S",result);
                  csTemp.Format("%s",buf);

                  csText += csTemp;
                  csText +=" ";
               }

            //Release all objects
            IWord->Release();
            IWords->Release();
            ILayout->Release();
            IImage->Release();
         }
         IImages->Release();

      } else {
         bRet = FALSE;
      }
   } else {
      bRet = FALSE;
   }

   IDobj->Close(0);
   IDobj->Release();
   pVal->Release();

   } else {
      bRet = FALSE;
   }

   return bRet;
}

That is!

Version History

Version 1: No Active-X ctrl in the dialogue, use bReadOCRByMODI(...)

Version 2: Add Active-X ctrl in the dialogue,use bReadOCRByMODIAXCtrl(...)

and version 2 is the 1st demo program posted on the codeproject.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

donghuih
Software Developer (Senior)
United States United States

The author is a senior software engineer in US.


You may also be interested in...

Comments and Discussions

 
QuestionMODI dll has dependency on Office package. Pin
suvidh shetty16-Oct-14 2:30
membersuvidh shetty16-Oct-14 2:30 
QuestionNot able to launch the MODIVCDEMO.exe Pin
suvidh shetty7-Oct-14 20:09
membersuvidh shetty7-Oct-14 20:09 
QuestionA Simple C# Example Pin
ZamirF26-Sep-11 22:42
memberZamirF26-Sep-11 22:42 
BugCXPageException when OCR Pin
AgentX300020-Jun-11 5:28
memberAgentX300020-Jun-11 5:28 
GeneralPlease send me Source code Pin
venkadesh 201010-Mar-11 19:57
membervenkadesh 201010-Mar-11 19:57 
QuestionExample doesn't work? Pin
Member 442795525-Aug-10 7:10
memberMember 442795525-Aug-10 7:10 
AnswerRe: Example doesn't work? Pin
donghuih1-Feb-13 3:31
memberdonghuih1-Feb-13 3:31 
AnswerRe: Example doesn't work? Pin
JasMineLeaf19-Oct-14 16:07
memberJasMineLeaf19-Oct-14 16:07 
QuestionPreffered Image resolution for better results Pin
erappy18-May-09 0:03
membererappy18-May-09 0:03 
Questionc Pin
snawy20-Jan-09 18:54
membersnawy20-Jan-09 18:54 
QuestionHow OCR method of MODI support Multi language? Pin
snawy20-Jan-09 18:22
membersnawy20-Jan-09 18:22 
QuestionOne Query...MS Office? Pin
KBM7321-May-08 1:05
memberKBM7321-May-08 1:05 
Questiona question Pin
zhaoyanggolden30-Mar-08 15:54
memberzhaoyanggolden30-Mar-08 15:54 
QuestionStill error after reinstalling Office 2003 Pin
Hooo26-Dec-07 18:39
memberHooo26-Dec-07 18:39 
GeneralRe: Still error after reinstalling Office 2003 Pin
Yangchenlong20-Jan-08 20:04
memberYangchenlong20-Jan-08 20:04 
AnswerRe: Still error after reinstalling Office 2003 Pin
zhb_sh15-Mar-09 4:55
memberzhb_sh15-Mar-09 4:55 
Generalhi Pin
lulukuku20-Nov-07 5:41
memberlulukuku20-Nov-07 5:41 
Generalthanks you! Pin
lulukuku20-Nov-07 5:40
memberlulukuku20-Nov-07 5:40 
GeneralERROR!!! Pin
lulukuku12-Nov-07 6:08
memberlulukuku12-Nov-07 6:08 
GeneralRe: ERROR!!! Pin
donghuih19-Nov-07 10:03
memberdonghuih19-Nov-07 10:03 
Questionneural network [modified] Pin
lulukuku10-Nov-07 5:27
memberlulukuku10-Nov-07 5:27 
GeneralI use it but always Error for both two version,how to fix it out Pin
Thomas Shao9-Oct-07 20:57
memberThomas Shao9-Oct-07 20:57 
AnswerRe: I use it but always Error for both two version,how to fix it out Pin
Christoph Reimmann9-Oct-07 23:57
memberChristoph Reimmann9-Oct-07 23:57 
GeneralRe: I use it but always Error for both two version,how to fix it out Pin
Thomas Shao16-Oct-07 21:30
memberThomas Shao16-Oct-07 21:30 
GeneralRe: I use it but always Error for both two version,how to fix it out Pin
venkadesh 201010-Mar-11 19:52
membervenkadesh 201010-Mar-11 19:52 
GeneralRe: I use it but always Error for both two version,how to fix it out Pin
WiemAbdulAzis22-Jul-09 22:03
memberWiemAbdulAzis22-Jul-09 22:03 
QuestionHow to set a text selection ? Pin
Christoph Reimmann27-Sep-07 1:11
memberChristoph Reimmann27-Sep-07 1:11 
GeneralChinese OCR Pin
x_solider14-Aug-07 22:52
memberx_solider14-Aug-07 22:52 
GeneralRe: Chinese OCR Pin
donghuih20-Aug-07 10:18
memberdonghuih20-Aug-07 10:18 
GeneralRe: Chinese OCR Pin
hb_zhang2-Mar-08 4:39
memberhb_zhang2-Mar-08 4:39 
GeneralRe: Chinese OCR Pin
hb_zhang2-Mar-08 4:43
memberhb_zhang2-Mar-08 4:43 
Generalrecognition candidates Pin
zxw095-Jul-07 21:31
memberzxw095-Jul-07 21:31 
Questioncan I deploy it without office2003 installed Pin
liberpike21-Jun-07 4:37
memberliberpike21-Jun-07 4:37 
QuestionUnable to add ref to MODI in VC++ 8.0. Pin
Purusothaman Chennai8-Jun-07 2:51
memberPurusothaman Chennai8-Jun-07 2:51 
AnswerRe: Unable to add ref to MODI in VC++ 8.0. Pin
surezu11-Jun-07 1:18
membersurezu11-Jun-07 1:18 
Questionhow 2 office 2007 Pin
Elaaber26-May-07 23:04
memberElaaber26-May-07 23:04 
AnswerRe: how 2 office 2007 Pin
talbot18-Feb-09 4:28
membertalbot18-Feb-09 4:28 
GeneralOCR from bitmap Pin
mazhar_cse26-Apr-07 18:52
membermazhar_cse26-Apr-07 18:52 
GeneralRe: OCR from bitmap Pin
B4stard26-Apr-07 20:35
memberB4stard26-Apr-07 20:35 
GeneralOnly works on second try... [modified] Pin
B4stard16-Apr-07 19:48
memberB4stard16-Apr-07 19:48 
GeneralRe: Only works on second try... Pin
byallop3-Jan-08 7:15
memberbyallop3-Jan-08 7:15 
AnswerRe: Only works on second try... Pin
byallop9-Jan-08 10:02
memberbyallop9-Jan-08 10:02 
GeneralProblem for long running applications that use MODI, 1500 document max! Pin
T-luv5-Apr-07 14:09
memberT-luv5-Apr-07 14:09 
GeneralThanks for referencing my article Pin
Martin Welker25-Jan-07 21:39
memberMartin Welker25-Jan-07 21:39 
GeneralVery good, but a little crash on close... Pin
Thierry Maurel24-Jan-07 0:10
memberThierry Maurel24-Jan-07 0:10 
GeneralRe: Very good, but a little crash on close... Pin
donghuih26-Jan-07 11:40
memberdonghuih26-Jan-07 11:40 
GeneralRe: Very good, but a little crash on close... Pin
sybase_20061-Feb-07 17:53
membersybase_20061-Feb-07 17:53 
GeneralRe: Very good, but a little crash on close... Pin
xy_sun22-May-11 5:18
memberxy_sun22-May-11 5:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150603.1 | Last Updated 24 Jan 2007
Article Copyright 2007 by donghuih
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid