别人让做一个简单的投票软件(刷投票的)
简单了解了一下,需要攻破一下问题
1、IP 限定的问题
2. 验证码识别的问题
IP限定的问题可以使用代理进行解决
找到一个动态代理的地址: http://www.xici.net.co/
使用CSQUERY 一个类似jquery 的C#html 解析库
代码如下:
string ur = "http://www.xici.net.co/"; CsQuery.CQ cs = CsQuery.CQ.CreateFromUrl(ur); CQ data = cs.Select("#ip_list tr.odd"); string td = data.ToList().First().InnerHTML; CQ ele = CQ.Create(td); CQ tep1 = ele.Select("td:eq(1)"); CQ tep2 = ele.Select("td:eq(2)"); CQ tep3 = ele.Select("td:eq(5)"); string ip = tep1.ToList().First().InnerText; string port = tep2.ToList().First().InnerText; string type = tep3.ToList().First().InnerText;
2. 验证码识别的问题:
tessnet2 代码如下:
private string Ocr(Bitmap image) { var ocr = new Tesseract(); ocr.SetVariable("tessedit_char_whitelist", "0123456789"); // If digit only image = BlackAndWhite(image, 0.8);//黑白处理,这个函数看下边 image = new Bitmap(image, image.Width * 3, image.Height * 3);//放大三倍 string path=Application.StartupPath+@"\tessdata\"; ocr.Init(path, "eng", true); var result = ocr.DoOCR(image, Rectangle.Empty); string tep = ""; foreach (Word word in result) { tep += word.Text; } return tep; }
黑白处理的方法:
public static Bitmap BlackAndWhite(Bitmap bitmap, Double hsb) { if (bitmap == null) { return null; } int width = bitmap.Width; int height = bitmap.Height; try { Bitmap bmpReturn = new Bitmap(width, height, PixelFormat.Format1bppIndexed); BitmapData srcBits = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb); BitmapData targetBits = bmpReturn.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format1bppIndexed); unsafe { byte* pSrcBits = (byte*)srcBits.Scan0.ToPointer(); for (int h = 0; h < height; h++) { byte[] scan = new byte[(width + 7) / 8]; for (int w = 0; w < width; w++) { int r, g, b; r = pSrcBits[2]; g = pSrcBits[1]; b = pSrcBits[0]; if (GetBrightness(r, g, b) >= hsb) scan[w / 8] |= (byte)(0x80 >> (w % 8)); pSrcBits += 3; } Marshal.Copy(scan, 0, (IntPtr)((int)targetBits.Scan0 + targetBits.Stride * h), scan.Length); pSrcBits += srcBits.Stride - width * 3; } bmpReturn.UnlockBits(targetBits); bitmap.UnlockBits(srcBits); return bmpReturn; } } catch { return null; } } private static float GetBrightness(int r, int g, int b) { float fR = ((float)r) / 255f; float fG = ((float)g) / 255f; float fB = ((float)b) / 255f; float fMax = fR; float fMin = fR; fMax = (fG > fMax) ? fG : fMax; fMax = (fB > fMax) ? fB : fMax; fMin = (fG < fMax) ? fG : fMax; fMin = (fB < fMax) ? fB : fMax; return ((fMax + fMin) / 2f); }
以上两个已经解决基本上就问题不大了,
另外对于图片的处理也可以使用ImageMagick C# 对应的版本为:Magick.NET