/// <summary> /// 获取验证码 /// </summary> /// <param name="url"></param> /// <returns></returns> public static string GetValidateCode(string url, CookieContainer webCookie, out Bitmap bmpImage) { try { //获取验证码 Image HeadImage = HttpHelps.GetPicture(url, webCookie); //处理图片 Bitmap bitmap = new Bitmap(HeadImage); UnCodebase ud = new UnCodebase(bitmap); ud.GrayByPixels(); ud.ClearNoise(128, 2); //识别验证码 tessnet2.Tesseract ocr = new tessnet2.Tesseract(); //声明一个OCR类 ocr.SetVariable("tessedit_char_whitelist", "0123456789"); //设置识别变量,当前只能识别数字。 ocr.Init(Application.StartupPath + @"\\tmpe", "eng", true); //应用当前语言包。 List <tessnet2.Word> result = ocr.DoOCR(ud.bmpobj, Rectangle.Empty); //执行识别操作 string validateCode = result[0].Text; //pictureBox1.Image = ud.bmpobj; bmpImage = ud.bmpobj; return(validateCode); } catch (Exception ex) { function.log("验证码获取错误" + ex.Message); bmpImage = null; return(""); } }
public string GetImageValues(string url) { Bitmap pic = new Bitmap(WebRequest.Create(url).GetResponse().GetResponseStream(), false); UnCodebase codebase = new UnCodebase(pic); codebase.GrayByPixels(); Bitmap[] bitmapArray = codebase.readMap(); int[] numArray = new int[4]; for (int i = 0; i < 4; i++) { numArray[i] = codebase.GetSingleDgGrayValue(bitmapArray[i]); } string[] arr = new string[4]; for (int j = 0; j < 4; j++) { arr[j] = codebase.GetSingleBmpCode(bitmapArray[j], numArray[j]); } return(this.getPicnums(arr)); }
private void btnClearSource_Click(object sender, EventArgs e) { int verfitycodecharcount = Convert.ToInt32(this.tbxVerfityCodeCharCount.Text); UnCodebase uc = new UnCodebase((Bitmap)pbVerfityImageSource.Image); uc.GrayByPixels(); //灰度处理 uc.GetPicValidByValue(128, verfitycodecharcount); //得到有效空间 Bitmap[] pics = uc.GetSplitPics(verfitycodecharcount, 1); //分割 if (pics.Length != verfitycodecharcount) { MessageBox.Show("分割错误,数量不是预期的" + verfitycodecharcount + "个"); //分割错误 return; } else // 重新调整大小 { pics[0] = uc.GetPicValidByValue(pics[0], 128); pics[1] = uc.GetPicValidByValue(pics[1], 128); pics[2] = uc.GetPicValidByValue(pics[2], 128); pics[3] = uc.GetPicValidByValue(pics[3], 128); } int newbitmaphight = 0; int newbitmapwith = 0; int lasty = 0; foreach (Bitmap bmp in pics) { newbitmapwith += bmp.Width; if (bmp.Height > newbitmaphight) { newbitmaphight = bmp.Height; } PictureBox pb = new PictureBox(); pb.Image = (Image)bmp; pb.Location = new Point(0, lasty); pb.Size = new Size(bmp.Width, bmp.Height); this.plClearedImageList.Controls.Add(pb); Label lb = new Label(); lb.Text = uc.GetSingleBmpCode(bmp, 128); lb.Location = new Point(66, lasty); lb.Size = new Size(500, bmp.Height + 5); this.plClearedImageList.Controls.Add(lb); TextBox tb = new TextBox(); tb.Location = new Point(lb.Location.X + lb.Size.Width + 10, lasty); tb.Size = new Size(20, bmp.Height + 5); this.plClearedImageList.Controls.Add(tb); lasty += bmp.Height + 5; } Bitmap newBitmap = new Bitmap(newbitmapwith, newbitmaphight); Graphics g = Graphics.FromImage(newBitmap); g.Clear(System.Drawing.Color.White); int x = 0; foreach (Bitmap newbmp in pics) { g.DrawImage(newbmp, x, newbmp.Height); x += newbmp.Width; } pbVerfityImageClear.Image = (Image)newBitmap; //newBitmap.Dispose(); //g.Dispose(); // if (!textBoxInput.Text.Equals("")) //string result = ""; //char singleChar = ' '; //{ // for (int i = 0; i < 4; i++) // { // string code = GetSingleBmpCode(pics[i], 128); //得到代码串 // for (int arrayIndex = 0; arrayIndex < CodeArray.Length; arrayIndex++) // { // if (CodeArray[arrayIndex].Equals(code)) //相等 // { // if (arrayIndex < 10) // 0..9 // singleChar = (char)(48 + arrayIndex); // else if (arrayIndex < 36) //A..Z // singleChar = (char)(65 + arrayIndex - 10); // else // singleChar = (char)(97 + arrayIndex - 36); // result = result + singleChar; // } // } // } //} //return result; }
public void SetTbcd() { textBox1.Text = verifyText; UnCodebase uc = new UnCodebase(v); Bitmap[] bb = uc.getImg(); pic_11.Image = bb[0]; pic_22.Image = bb[1]; pic_33.Image = bb[2]; pic_44.Image = bb[3]; char[] cha = verifyText.ToCharArray(); tbc_11.Text = "" + cha[0]; tbd_11.Text = uc.getData(bb[0]); tbc_22.Text = "" + cha[1]; tbd_22.Text = uc.getData(bb[1]); tbc_33.Text = "" + cha[2]; tbd_33.Text = uc.getData(bb[2]); tbc_44.Text = "" + cha[3]; tbd_44.Text = uc.getData(bb[3]); label2.Text = "识别完成!"; textBox1.Focus(); }
/// <summary> /// 登录 /// </summary> private void Login() { ////登录页 //_httpHelper.SyncRequest("http://*****:*****@"./tessdata", "eng", EngineMode.Default)) //{ // using (var entity = engine.Process(uncode.bmpobj)) // { // _checkCode = entity.GetText(); // } //} //_checkCode = _checkCode.Trim(); //image.Save("D:\\" + InputText(_checkCode, _checkCode.Length) + "_Code.jpg"); ////登录验证 //_httpHelper.PostData = string.Format("Email={0}&Password={1}&VCode={2}", // _userName, _password, _checkCode); //_httpHelper.SyncRequest("http://*****:*****@"./tessdata", "eng", EngineMode.Default)) //{ // using (var entity = engine.Process(uncode.bmpobj)) // { // _checkCode = entity.GetText(); // } //} //System.Drawing.Image image = (Image)uncode.bmpobj; //image.Save("D:\\" + InputText(_checkCode, _checkCode.Length) + "_Code.jpg"); //_checkCode = _checkCode.Trim(); //_checkCode = _checkCode.Replace(" ", "");//这样替换下就可以了 ////登录验证 //_httpHelper.PostData = string.Format("username={0}&password={1}&captcha={2}", // _userName, _password, _checkCode); //_httpHelper.SyncRequest("https://eastmoney-office.eastmoney.com/bd-cas/login"); //#########################################################################// //登录页 _httpHelper.SyncRequest(" http://wp18011601.eda8888.com/App/Login"); //获取验证码图片 var bitmap = _httpHelper.GetCheckCode("http://wp18011601.eda8888.com/App/Login/GetCodeImage?id=1"); System.Drawing.Image Source_image = (Image)bitmap; var random = new Random(); var name_img = random.Next(0, int.MaxValue); Source_image.Save("D:\\eda8888\\" + name_img + "_Code.jpg"); //Bitmap bitmap = new Bitmap("D:\\617420430_Code.jpg"); //解析验证码1 using (var engine = new TesseractEngine(@"./tessdata", "eng", EngineMode.Default)) { using (var entity = engine.Process(bitmap)) { _checkCode = entity.GetText(); } } _checkCode = InputText(_checkCode, _checkCode.Length); UnCodebase uncode = new UnCodebase(bitmap); if (Contains_a_Z(_checkCode)) { //去图形边框 uncode.ClearPicBorder(1); //灰度转换,逐点方式 uncode.GrayByPixels(); //去掉噪点 //uncode.ClearNoise(50, 1); //图像二值化 uncode.ConvertToBinaryImage(); //扭曲图片校正 uncode.ReSetBitMap(); //解析验证码2 using (var engine = new TesseractEngine(@"./tessdata", "eng", EngineMode.Default)) { using (var entity = engine.Process(uncode.bmpobj)) { _checkCode = entity.GetText(); } } } _checkCode = _checkCode.Trim(); _checkCode = _checkCode.Replace(" ", "");//这样替换下就可以了 _checkCode = InputText(_checkCode, _checkCode.Length); System.Drawing.Image image = (Image)uncode.bmpobj; image.Save("D:\\eda8888\\" + InputText(_checkCode, _checkCode.Length) + "_Code.jpg"); //登录验证 _httpHelper.PostData = string.Format("username={0}&password={1}&code={2}", _userName, _password, _checkCode); _httpHelper.SyncRequest("http://wp18011601.eda8888.com/App/Login"); }