asp.net的OCR識別

 
組件---Microsoft Office Document Imaging;這樣就方便的做OCR識別了。
首先用office安裝盤安裝這個組件,默認安裝office的時候是不會安裝的,只要添加這個組件功能就好了。
然後新建的項目裏添加組件Microsoft Office Document Imaging 12.0 Type Library(office07)或者Microsoft Office Document Imaging 11.0 Type Library(office03)
MODI.Document md = new MODI.Document();

            md.Create(Directory.GetCurrentDirectory() + "\\SampleForOCR.tiff");

            md.OCR(MODI.MiLANGUAGES.miLANG_ENGLISH, true, true);

            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), 這個改成紅色一個像素看起來更舒服,這個函數是鼠標一移動都會被調用的.

 
 
核心代碼如下:
MODI.Document doc = new MODI.Document();
doc.Create("picPath");
 MODI.Image image; MODI.Layout layout;
 string ret = null;
 //doc.OCR(MODI.MiLANGUAGES.miLANG_ENGLISH, true, true);識別英文
doc.OCR(MODI.MiLANGUAGES.miLANG_CHINESE_SIMPLIFIED, true, true);//識別中文
for (int i = 0; i < doc.Images.Count; i++)
 { image = (MODI.Image)doc.Images[i]; layout = image.Layout; ret += layout.Text; }
 return ret;
它識別英文和中文的正確率都還不錯。要注意的是:如果要識別簡體中文,你必須下載安裝中文簡體OCR引擎,微軟官方下載地址:中文簡體OCR引擎
 
 
下載C#的OCR識別模塊
數字識別率我碰到的幾乎是100%
[DllImport("AspriseOCR.dll")]
  static extern string craboOCR(string file, int type);
private void GetVeryfyCode()
  {
if(File.Exists(_imgPath))//ok now?
   {
    try
    {
    this.picbVeryfyCode.Image=System.Drawing.Bitmap.FromFile(_imgPath);
     _veryfyCode=craboOCR(_imgPath,-1);   //將返回string,並以"\r\n"結尾!!
     _veryfyCode=_veryfyCode.Substring(0,4);
     this.txtVeryfyCode.Text=_veryfyCode;
    }
    catch(Exception e)
    {
     this.lblResult.Text+=e.Message;
    }
   }
}
 
第三種方法
程序使用google的Tesseract進行圖片識別,其.NET版本地址爲:http://www.pixel-technology.com/freeware/tessnet2/
privatevoidbtnGo_Click(objectsender, EventArgs e)
{
    tessnet2.Tesseractocr=newtessnet2.Tesseract();//聲明一個OCR類
   stringdefaultCharList="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-";
   if(!string.IsNullOrEmpty(txtCharList.Text))
   {
        defaultCharList=txtCharList.Text;
   }
    ocr.SetVariable("tessedit_char_whitelist", defaultCharList);//設置識別變量,當前只能識別數字及英文字符。
    ocr.Init(Application.StartupPath+@"\tessdata","eng",false);//應用當前語言包。注,Tessnet2是支持多國語的。語言包下載鏈接:http://code.google.com/p/tesseract-ocr/downloads/list

    WebClient wc=newWebClient();
   byte[]oimg=wc.DownloadData(txtImageUrl.Text);// 自己修改成要識別的地址吧
    Bitmap bp=newBitmap(newMemoryStream(oimg),true);//識別圖像
    Bitmap bp2=newBitmap(newMemoryStream(oimg),true);
    picBox1.Image=bp2;
   //bp = ImageProcess.RemoveGreen(bp);
   //bp = ImageProcess.ToBW(bp);

    picBox2.Image=bp;

    List<tessnet2.Word>result=newList<tessnet2.Word>();
   stringtxt="";

   try
   {
        result=ocr.DoOCR(bp, Rectangle.Empty);//執行識別操作
       foreach(tessnet2.Wordwordinresult)//遍歷識別結果。
       {
            txt+=word.Text;
       }
   }
   catch(Exception ex)
   {

   }


   if(string.IsNullOrEmpty(txt)||txt=="~")
   {
       #region 如圖片太小,無法識別,則先放大
       intw=bp.Width>=100?bp.Width:100;
       inth=bp.Height>=30?bp.Height:30;

        Bitmap resizedBmp=newBitmap(w, h);
        Graphics g=Graphics.FromImage(resizedBmp);
        g.DrawImage(bp,newRectangle(0,0, w, h),newRectangle(0,0, bp.Width, bp.Height), GraphicsUnit.Pixel);
        bp=resizedBmp;
       #endregion

        picBox2.Image=bp;

        result=ocr.DoOCR(bp, Rectangle.Empty);//執行識別操作
        txt="";
       foreach(tessnet2.Wordwordinresult)//遍歷識別結果。
       {
            txt+=word.Text;
       }
   }

    textBox1.Text=txt;
}
發佈了7 篇原創文章 · 獲贊 4 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章