使用 office2003 document imaging control 控件 做识别 OCR

先确保的是你要装上这个组件Microsoft Office Document Imaging 12.0 Type Library或者Microsoft Office Document Imaging 11.0 Type Library


 

装好后项目里添加引用这个com就可以了

它识别英文和中文的正确率都还不错。要注意的是:如果要识别简体中文,你必须下载安装中文简体OCR引擎,微软官方下载地址:中文简体OCR引擎

以下是识别的核心代码

 MODI.Document md =  new MODI.Document();
            md.Create(Directory.GetCurrentDirectory() + "\\SampleForOCR.tiff");
            md.OCR(MODI.MiLANGUAGES.miLANG_ENGLISH,  truetrue);

            MODI.Image image = (MODI.Image)md.Images[0];
            MODI.Layout layout = image.Layout;

            MODI.Word word;
            StringBuilder sb =  new StringBuilder();
             for ( int i = 0; i < layout.Words.Count;i++ )
             {
                word = (MODI.Word)layout.Words[i];
                sb.Append(word.Text);
            }

sb.ToString就可以得到了
图片转成 tiff
Bitmap bitmap =  new Bitmap(100, 100);
            bitmap.Save("somefilename", ImageFormat.Tiff);
如何进行截屏呢?
拷屏.先把整个屏幕的图像拷过来(其它地方也会用到这个.比如做一个操作动作的外挂等)
Graphics g = Graphics.FromImage(m_WindowDlg.m_objBitmap);
             //  Copy the screen into the bitmap object.
            g.CopyFromScreen(0, 0, 0, 0,  new Size(w, h));

指定区域是利用
另一个窗口来的..把这个窗口的透明属性opacity设为100%这样人家就看不到了.
然后利用 MouseDown 画出我们截屏的范围 和 mouseUP事件.
mouseUP后引发一个完成的事件.并通知座标大小
主要代码

//  Make sure we clicked the mouse button and
            
//    have a starting coordinate.
             if (m_nStartX != -1)
             {
                // Get the graphcis object from this window.
                Graphics g = Graphics.FromHwnd(Handle);

                // Check to see if we need to restore the
                
//   screen from a previous rectangle draw.
                if (m_nLastX != -1)
                {
                    // Create a rectangle with which to clip.
                    
//   Note that we are 3 pixels to the left
                    
//   and 3 pixels to the right so that we
                    
//   can take the width of the line into
                    
//   account.
                    Rectangle rc = new Rectangle(m_nLastX-3, m_nLastY-3,
                        m_nLastWidth+6, m_nLastHeight+6);
                    g.SetClip(rc);
                    g.DrawImage(m_objBitmap, 0, 0);
                    g.ResetClip();
                }


                // Here we set some local variables just
                
//   in case we have to swap the values.
                int nStartX = m_nStartX;
                int nStartY = m_nStartY;

                // Get and record our current mouse position.
                int nEndX = e.X;
                int nEndY = e.Y;

                // Calculate width and height.
                int nWidth = Math.Abs(nStartX - nEndX);
                int nHeight = Math.Abs(nStartY - nEndY);

                // We may need to swap values if user went
                
//   left from the start position.
                if (nEndX < nStartX)
                {
                    int i = nStartX;
                    nStartX = nEndX;
                    nEndX = i;
                }

                // We may need to swap values if user went
                
//   up from the start position.
                if (nEndY < nStartY)
                {
                    int i = nStartY;
                    nStartY = nEndY;
                    nEndY = i;
                }


                // Draw the rectangle.
                g.DrawRectangle(new Pen(Color.Black, 3),
                    nStartX, nStartY, nWidth, nHeight);

                // Record the operation so that we can restore
                
//   when the mouse moves, and also give values
                
//   to the EndCapture method.
                m_nLastX = nStartX;
                m_nLastY = nStartY;
                m_nLastWidth = nWidth;
                m_nLastHeight = nHeight;
g.DrawRectangle(new Pen(Color.Red, 1), 这个改成红色一个像素看起来更舒服,这个函数是鼠标一移动都会被调用的.


转载地址:http://www.cnblogs.com/xuneng/archive/2008/07/31/1257448.html
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值