搜索不少关于验证码的东东,经过实验识别程度不是很理想,但也能识别90%数字验证码,对字母验证码不是很好。
1 public partial class GetValidCode : Form 2 { 3 private ValidImg _mValidImg; 4 private Bitmap _testMap; 5 private Bitmap[] arrmap =null; 6 public GetValidCode() 7 { 8 InitializeComponent(); 9 } 10 11 private void GetValidCode_Load(object sender, EventArgs e) 12 { 13 #region 得到验证码图片 14 _mValidImg = new ValidImg(); 15 _testMap = _mValidImg.GetValidImg(); 16 Bitmap map = new Bitmap(_mValidImg.StrPath); 17 pbImg.Image = map; 18 //arrmap = GetSplitPics(_testMap, 1, 5); 19 //DisplaySplitImg(arrmap); 20 #endregion 21 } 22 23 24 private void btnGetContent_Click(object sender, EventArgs e) 25 { 26 if (true) 27 { 28 #region TestOne 29 _testMap = new Bitmap(_mValidImg.StrPath); 30 31 UnCodebase ud = new UnCodebase(_testMap); 32 _testMap = ud.GrayByPixels(); 33 ud.ClearNoise(128, 2); 34 pbImg.Image = _testMap; 35 36 tessnet2.Tesseract ocr = new tessnet2.Tesseract();//声明一个OCR类 37 ocr.SetVariable("tessedit_char_whitelist", "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXZY0123456789"); //设置识别变量,当前只能识别数字。 38 ocr.Init(Application.StartupPath + @"\\tmpe", "eng", true); //应用当前语言包。注,Tessnet2是支持多国语的。语言包下载链接:http://code.google.com/p/tesseract-ocr/downloads/list 39 40 List<tessnet2.Word> result = ocr.DoOCR(_testMap, Rectangle.Empty);//执行识别操作 41 string code = result[0].Text; 42 txtContent.Text += code; 43 44 #endregion 45 } 46 } 47 48 49 /// <summary> 50 /// 得到图片有效区域,使切分图片更加精确 51 /// </summary> 52 /// <param name="_bitmap"></param> 53 /// <returns></returns> 54 private Bitmap CutMap(Bitmap _bitmap) 55 { 56 Rectangle rg = new Rectangle(int.Parse(updX.Value.ToString()), int.Parse(updY.Value.ToString()), _bitmap.Width - int.Parse(updW.Value.ToString()), _bitmap.Height - int.Parse(updH.Value.ToString())); 57 //Rectangle rg = new Rectangle(1, 0, _bitmap.Width - 10, _bitmap.Height ); 58 Bitmap bitmap = _bitmap.Clone(rg, _bitmap.PixelFormat); 59 return bitmap; 60 61 } 62 63 /// <summary> 64 /// 切分图片 65 /// </summary> 66 /// <param name="_bitmap"></param> 67 /// <param name="row"></param> 68 /// <param name="col"></param> 69 /// <returns></returns> 70 private Bitmap[] SplitImg(Bitmap _bitmap, int row, int col) 71 { 72 int singW = _bitmap.Width / row; 73 int singH = _bitmap.Height / col; 74 Bitmap[] arrmap = new Bitmap[row * col]; 75 Rectangle rect; 76 for (int i = 0; i < col; i++) 77 { 78 for (int j = 0; j < row; j++) 79 { 80 rect = new Rectangle(j * singW, i * singH, singW, singH); 81 arrmap[i * row + j] = _bitmap.Clone(rect, _bitmap.PixelFormat); 82 } 83 } 84 85 return arrmap; 86 } 87 88 /// <summary> 89 /// 展示 90 /// </summary> 91 /// <param name="arrmap"></param> 92 private void DisplaySplitImg(Bitmap[] arrmap) 93 { 94 for (int i = 0; i < arrmap.Length; i++) 95 { 96 ControlCollection cc = this.Controls as ControlCollection; 97 foreach (Control item in cc) 98 { 99 if (item is PictureBox) 100 { 101 PictureBox pb = item as PictureBox; 102 int index=i+1; 103 if (pb.Name=="pb"+index) 104 { 105 pb.Image = arrmap[i]; 106 break; 107 } 108 } 109 } 110 111 } 112 } 113 114 private void GetValidCode_FormClosed(object sender, FormClosedEventArgs e) 115 { 116 this.Dispose(); 117 if (File.Exists(_mValidImg.StrPath)) 118 { 119 File.Delete(_mValidImg.StrPath); 120 } 121 } 122 123 }
ValidImg类中定义验证码图片
public class ValidImg { private string _strPath=string.Empty; public string StrPath { get { return _strPath; } set { _strPath = value; } } public ValidImg() { Directory.CreateDirectory(@"tmpImg"); } public Bitmap GetValidImg() { Bitmap _ResultBitMap; using (Bitmap img = new Bitmap(120, 30)) { using (Graphics g = Graphics.FromImage(img)) { string strCode = GetCode(5); Random r = new Random(); g.Clear(Color.White); for (int i = 0; i < 25; i++) { int x1 = r.Next(0, img.Width); int x2 = r.Next(0, img.Width); int y2 = r.Next(0, img.Height); int y1 = r.Next(0, img.Height); g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2); // 随机产生图片的背景噪线 } Font font = new Font("楷体", 18, (System.Drawing.FontStyle.Bold)); LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, img.Width, img.Height), Color.Blue, Color.DarkRed, 3, true); g.DrawString(strCode, font, brush, 5, 2); //绘制随机字符 g.DrawRectangle(new Pen(Color.Silver), 0, 0, img.Width - 1, img.Height - 1); //绘制图片的前景噪点 _ResultBitMap = img; DateTime dt = DateTime.Now; string strImgFileName = dt.ToFileTime().ToString()+ ".jpg"; _strPath = Path.Combine("tmpImg", strImgFileName); _ResultBitMap.Save(_strPath, System.Drawing.Imaging.ImageFormat.Jpeg); } } return _ResultBitMap; } private string GetCode(int num) { string str = "ABCD1EF2GH3IJ4KL5MN6P7QR8ST9UVWXYZ"; Random r = new Random(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < num; i++) { sb.Append(str.Substring(r.Next(0, str.Length), 1)); } return sb.ToString(); } }
public class ValidImg { private string _strPath=string.Empty; public string StrPath { get { return _strPath; } set { _strPath = value; } } public ValidImg() { Directory.CreateDirectory(@"tmpImg"); } public Bitmap GetValidImg() { Bitmap _ResultBitMap; using (Bitmap img = new Bitmap(120, 30)) { using (Graphics g = Graphics.FromImage(img)) { string strCode = GetCode(5); Random r = new Random(); g.Clear(Color.White); for (int i = 0; i < 25; i++) { int x1 = r.Next(0, img.Width); int x2 = r.Next(0, img.Width); int y2 = r.Next(0, img.Height); int y1 = r.Next(0, img.Height); g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2); // 随机产生图片的背景噪线 } Font font = new Font("楷体", 18, (System.Drawing.FontStyle.Bold)); LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, img.Width, img.Height), Color.Blue, Color.DarkRed, 3, true); g.DrawString(strCode, font, brush, 5, 2); //绘制随机字符 g.DrawRectangle(new Pen(Color.Silver), 0, 0, img.Width - 1, img.Height - 1); //绘制图片的前景噪点 _ResultBitMap = img; DateTime dt = DateTime.Now; string strImgFileName = dt.ToFileTime().ToString()+ ".jpg"; _strPath = Path.Combine("tmpImg", strImgFileName); _ResultBitMap.Save(_strPath, System.Drawing.Imaging.ImageFormat.Jpeg); } } return _ResultBitMap; } private string GetCode(int num) { string str = "ABCD1EF2GH3IJ4KL5MN6P7QR8ST9UVWXYZ"; Random r = new Random(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < num; i++) { sb.Append(str.Substring(r.Next(0, str.Length), 1)); } return sb.ToString(); } }
虽然不完美,但也是识别验证码的一个思路,希望不要用在做坏事上。