Click here to Skip to main content
Licence 
First Posted 23 Jan 2007
Views 92,280
Bookmarked 52 times

OCR With MODI in Visual C++

By | 23 Jan 2007 | Article
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

About the Author

donghuih

Software Developer (Senior)

United States United States

Member

The author is a software engineer. His interest is in the imaging and
document automation, and restaurant POS software.
Click followings for more details.
POS Software:
http://www.ezordersystem.com
 



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionA Simple C# Example PinmemberZamirF22:42 26 Sep '11  
BugCXPageException when OCR PinmemberAgentX30005:28 20 Jun '11  
GeneralPlease send me Source code Pinmembervenkadesh 201019:57 10 Mar '11  
QuestionExample doesn't work? PinmemberMember 44279557:10 25 Aug '10  
QuestionPreffered Image resolution for better results Pinmembererappy0:03 18 May '09  
Questionc Pinmembersnawy18:54 20 Jan '09  
QuestionHow OCR method of MODI support Multi language? Pinmembersnawy18:22 20 Jan '09  
GeneralMODI in Java Pinmemberpaolopagano9:28 4 Aug '08  
GeneralRe: MODI in Java Pinmembersrabbis4:14 27 Sep '11  
QuestionHow to include in a Visual C++ project? Pinmemberpaolopagano23:53 3 Aug '08  
QuestionOne Query...MS Office? PinmemberKBM731:05 21 May '08  
Questiona question Pinmemberzhaoyanggolden15:54 30 Mar '08  
QuestionStill error after reinstalling Office 2003 PinmemberHooo18:39 26 Dec '07  
GeneralRe: Still error after reinstalling Office 2003 PinmemberYangchenlong20:04 20 Jan '08  
my problem smae with you please help us
AnswerRe: Still error after reinstalling Office 2003 Pinmemberzhb_sh4:55 15 Mar '09  
Generalhi Pinmemberlulukuku5:41 20 Nov '07  
Generalthanks you! Pinmemberlulukuku5:40 20 Nov '07  
GeneralERROR!!! Pinmemberlulukuku6:08 12 Nov '07  
GeneralRe: ERROR!!! Pinmemberdonghuih10:03 19 Nov '07  
Questionneural network [modified] Pinmemberlulukuku5:27 10 Nov '07  
GeneralI use it but always Error for both two version,how to fix it out PinmemberThomas Shao20:57 9 Oct '07  
AnswerRe: I use it but always Error for both two version,how to fix it out PinmemberChristoph Reimmann23:57 9 Oct '07  
GeneralRe: I use it but always Error for both two version,how to fix it out PinmemberThomas Shao21:30 16 Oct '07  
GeneralRe: I use it but always Error for both two version,how to fix it out Pinmembervenkadesh 201019:52 10 Mar '11  
GeneralRe: I use it but always Error for both two version,how to fix it out PinmemberWiemAbdulAzis22:03 22 Jul '09  

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.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120529.1 | Last Updated 24 Jan 2007
Article Copyright 2007 by donghuih
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid