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), 这个改成红色一个像素看起来更舒服,这个函数是鼠标一移动都会被调用的.
static extern string craboOCR(string file, int type);
{
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;
}
}
}
{
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 . Wordword inresult ) //遍历识别结果。
{
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 . Wordword inresult ) //遍历识别结果。
{
txt +=word . Text ;
}
}
textBox1 . Text =txt ;
}