苹果手机投票刷票软件代码是怎么写的?
现在手机对于大家来说是生活当中必不可或缺的,不论走到哪里手机必须是带在身上的,手机可以为我们解决很多事情,打电话,付钱,打车,等等都可以,那么手机如此普及的情况之下,刷票自然也可以通过手机来操作了,这也就是我今天要说的苹果手机投票刷票软件代码怎么写的原因,现在很多人都想要刷票,那么如果可以通过手机来进行刷票岂不是更完美,那么下面大家来看苹果手机投票刷票软件的代码是怎么写的吧
思路:
通过HTTpWebRequest来获取投票主页面cookie,并且通过这个cookie来访问验证码页面得到验证码,得到验证码 通过灰度化,去噪,取有效区域,最后二值化常规步骤,最后和预存的字符匹配,得到验证码,然后蒋验证码 cookie一起发到投票处理页面,可以通过IE跟踪找到,不在具体描述,最后HTTPWEBReaponse中把返回的cookie设置成过期即可。我这个网址验证码做的比较简单,这么做可以搞定,当然比较复杂的验证码最好还是通过openCV来处理。好了,不说了,直接贴代码。
心得:复习了下winform开发中多线程对控件的访问,采用委托来解决的,好久不用,生疏了好多。。。。。。
复习了session和cookie在web开发中的使用
复习了简单的验证码识别。负责的由于时间关系,米升入下去,以后有机会在研究研究
核心代码
private delegate void FlushClient();//代理
private CookieContainer cc;
private CookieContainer cc_Req;
private void ThreadFunction()
{
if (this.textBox1.InvokeRequired || this.textBox2.InvokeRequired || this.textBox3.InvokeRequired || label2.InvokeRequired)//等待异步
{
FlushClient fc = new FlushClient(ThreadFunction);
this.Invoke(fc);//通过代理调用刷新方法
}
else
{
label2.Text = FlushState;
label10.Text = checkMessage;
textBox3.Text = FlushResult;
textBox1.Text = n_success.ToString();//从返回的内容里找出票数
textBox2.Text = n_fail.ToString();
}
}
private void InitUserCookie()
{
cc = new CookieContainer();
string url = "http://www.toutoupiao.com/showvote-6811.html";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.CookieContainer = cc;
HttpWebResponse response = (HttpWebResponse)req.GetResponse();
cc.Add(response.Cookies);
cc_Req = req.CookieContainer;
}
private Image GetCheckImage()
{
HttpWebRequest reqCheck = (HttpWebRequest)WebRequest.Create(checkURL);
reqCheck.CookieContainer = cc_Req;
reqCheck.Timeout = 5000;
HttpWebResponse repCheck = (HttpWebResponse)reqCheck.GetResponse();
System.Drawing.Image img;
img = new System.Drawing.Bitmap(repCheck.GetResponseStream());
return img;
}
private string GetCheckMessage()
{
Image numPic = GetCheckImage(); // 得到验证码图片
pictureBox1.Image = numPic;
unCodeAiYing UnCheckobj = new unCodeAiYing((Bitmap)numPic);
string strNum = UnCheckobj.getPicnum(); //识别图片
return strNum;
}
private HttpWebRequest CreatHttp()
{
rnd = new Random();
checkMessage = GetCheckMessage();
string rndstr = rnd.NextDouble().ToString();//这是模仿QueryString里那个随机数的,没有估计也不影响结果
string url = "http://www.toutoupiao.com/brushvote.asp?action=ajax_sendvote_more&vote_id=6811&item_id=25829&check_code=" + checkMessage + "&_=" + rndstr;
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.CookieContainer = cc;
req.Method = "GET";
req.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36";
//req.UserAgent = "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)";
req.Timeout = m;
req.Referer = "http://www.toutoupiao.com/showvote-6811.html";
req.KeepAlive = true;
req.Headers.Add("x-requested-with", "XMLHttpRequest");
return req;
}
private void Go()
{
HttpWebRequest req = CreatHttp();
try
{
var res = req.GetResponse() as HttpWebResponse;
Stream st = res.GetResponseStream();//读取Response
StreamReader sr = new StreamReader(st);
string result = sr.ReadToEnd().ToString();
if (res.Cookies.Count != 0)
res.Cookies["6811"].Expires = DateTime.Now.AddDays(-2);
if (result == error_str)
{
n_fail++;
}
if (result.Contains("成功"))
{
n_success++;
}
FlushResult = result + rnd.Next(1000).ToString();
FlushState = "刷票进行中...";
}
catch (WebException e)
{
FlushState = e.ToString();
n_fail++;
}
catch (System.ObjectDisposedException e)
{
FlushState = e.ToString();
n_fail++;
}
finally
{
ThreadFunction();
}
}
unCodeAiYing.cs代码
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using Ocr;
namespace BallotAiying2
{
class unCodeAiYing : UnCodebase
{
//字符表 顺序为0..9,A..Z,a..z
// string[] CodeArray = new string[] {
//"001100010010100001100001100001100001100001100001010010001100",
//"00100011001010000100001000010000100001000010011111",
//"011110100001100001000001000110001000010000100000100000111111",
//"011110100001100001000001001110000001000001100001100001011110",
//"000010000110001010010010100010100010111111000010000010000010",
//"111111100000100000100000111110000001000001000001100001011110",
//"001110010000100000100000111110100001100001100001100001011110",
//"111111000001000001000010000010000010000100000100000100000100",
//"011110100001100001100001011110100001100001100001100001011110",
//"011110100001100001100001011111000001000001000001000010011100",
//"001100010010100001100001100001111111100001100001100001100001",
//"111110100001100001100001111110100001100001100001100001111110",
//"011110100001100000100000100000100000100000100000100001011110",
//"111100100010100001100001100001100001100001100001100010111100",
//"111111100000100000100000111110100000100000100000100000111111",
//"111111100000100000100000111110100000100000100000100000100000",
//"011110100001100000100000100000100111100001100001100011011101",
//"100001100001100001100001111111100001100001100001100001100001",
//"11111001000010000100001000010000100001000010011111",
//"000111000010000010000010000010000010000010100010100010011100",
//"100001100010100100101000110000110000101000100100100010100001",
//"100000100000100000100000100000100000100000100000100000111111",
//"1000001110001111000111010101101010110010011001001100000110000011000001",
//"100001110001110001101001101001100101100101100011100011100001",
//"011110100001100001100001100001100001100001100001100001011110",
//"111110100001100001100001111110100000100000100000100000100000",
//"01111001000010100001010000101000010100001010000101011010110011001111000000011",
//"111110100001100001100001111110100100100010100010100001100001",
//"011110100001100001100000011000000110000001100001100001011110",
//"1111111000100000010000001000000100000010000001000000100000010000001000",
//"100001100001100001100001100001100001100001100001100001011110",
//"1000001100000110000010100010010001001000100010100001010000010000001000",
//"1000001100000110000011001001100100110010011001001101010110101010100010",
//"100001100001010010010010001100001100010010010010100001100001",
//"1000001100000101000100100010001010000010000001000000100000010000001000",
//"111111000001000001000010000100001000010000100000100000111111",
//"011110100001000111011001100001100011011101",
//"100000100000100000101110110001100001100001100001110001101110",
//"011110100001100000100000100000100001011110",
//"000001000001000001011101100011100001100001100001100011011101",
//"011110100001100001111111100000100000011110",
//"001110010001010000010000111110010000010000010000010000010000",
//"000001011101100010100010100010011100010000011110100001100001011110",
//"100000100000100000101110110001100001100001100001100001100001",
//"00100001000000001100001000010000100001000010011111",
//"00001000010000000011000010000100001000010000100001000011001001100",
//"100000100000100000100010100100101000111000100100100010100001",
//"01100001000010000100001000010000100001000010011111",
//"1110110100100110010011001001100100110010011001001",
//"101110110001100001100001100001100001100001",
//"011110100001100001100001100001100001011110",
//"101110110001100001100001100001110001101110100000100000100000",
//"011101100011100001100001100001100011011101000001000001000001",
//"101110110001100000100000100000100000100000",
//"011110100001100000011110000001100001011110",
//"001000001000111110001000001000001000001000001000001001000110",
//"100001100001100001100001100001100011011101",
//"100001100001100001010010010010001100001100",
//"1000001100100110010011001001100100110101010100010",
//"100001100001010010001100010010100001100001",
//"100001100001100001100001100001010011001101000001000010011100",
//"111111000010000100001000010000100000111111"
// };
string[] CodeArray = new string[] {
"011110100001100011100101100101101001101001110001100001011110", //0...
"001111001001001001001001001001", //1...
"011110100001100001000001000010000100001000010000100000111111", //2...
"011110100001100001000001001110000001000001100001100001011110", //3...
"000010000110001010001010010010100010111111000010000010000010", //4...
"111111100000100000111110100001000001000001100001100001011110", //5...
"011110100001100001100000101110110001100001100001100001011110", //6...
"111111000001000010000010000100000100000100001000001000010000", //7...
"011110100001100001100001011110100001100001100001100001011110", //8...
"011110100001100001100001100011011101000001100001100001011110",//9........
"001100010010100001100001100001111111100001100001100001100001",//A
"111110100001100001100001111110100001100001100001100001111110",//B
"011110100001100000100000100000100000100000100000100001011110",//C
"111100100010100001100001100001100001100001100001100010111100",//D
"111111100000100000100000111110100000100000100000100000111111",//E
"111111100000100000100000111110100000100000100000100000100000",//F
"011110100001100000100000100000100111100001100001100011011101",//G
"100001100001100001100001111111100001100001100001100001100001",//H
"11111001000010000100001000010000100001000010011111",//I
"000111000010000010000010000010000010000010100010100010011100",//J
"100001100010100100101000110000110000101000100100100010100001",//K
"10000100001000010000100001000010000100001000011111",//L..........
"1000001110001111000111010101101010110010011001001100000110000011000001",//M
"100001110001110001101001101001100101100101100011100011100001",//N
"011110100001100001100001100001100001100001100001100001011110",//O
"111110100001100001100001111110100000100000100000100000100000",//P
"01111001000010100001010000101000010100001010000101011010110011001111000000011",//Q
"111110100001100001100001111110100100100010100010100001100001",//R
"011110100001100001100000011000000110000001100001100001011110",//S
"1111111000100000010000001000000100000010000001000000100000010000001000",//T
"100001100001100001100001100001100001100001100001100001011110",//U
"1000001100000110000010100010010001001000100010100001010000010000001000",//V
"1000001100000110000011001001100100110010011001001101010110101010100010",//W
"100001100001010010010010001100001100010010010010100001100001",//X
"1000001100000101000100100010001010000010000001000000100000010000001000",//Y
"111111000001000001000010000100001000010000100000100000111111",//Z
"0111100100001001111101000010100001010000100111101",//a.................
"100000100000100000101110110001100001100001100001110001101110",//b
"011110100001100000100000100000100001011110",//c
"000001000001000001011101100011100001100001100001100011011101",//d....
"011110100001100001111111100000100001011110",//e.........
"0011010001001110010001000100010001000100",//f..........
"011101100011100001100001100001100011011101000001000001011110",//g.......
"100000100000100000101110110001100001100001100001100001100001",//h
"1001111111",//i............
"01000001010101010101010110",//j..........
"100000100000100000100010100100101000111000100100100010100001",//k............
"01100001000010000100001000010000100001000010011111",//l
"1010011110110110010011001001100100110010011001001",//m..........
"101110110001100001100001100001100001100001",//n
"011110100001100001100001100001100001011110",//o............
"101110110001100001100001100001110001101110100000100000100000",//p...
"011101100011100001100001100001100011011101000001000001000001",//q...
"1011110010001000100010001000",//r...
"01110100011000001110000011000101110",//s...
"010010111010010010010010001",//t...
"100001100001100001100001100001100011011101",//u...
"1000001100000101000100100010001010000101000001000",//v...
"100010001100010001010101010010101010010101010001000100001000100",//w...
"100001010010001100001100001100010010100001",//x...
"1000001100000101000100100010001010000101000001000000100000100001100000",//y...
"111111000010000100001000010000100000111111"//z
};
public unCodeAiYing(Bitmap pic)
: base(pic)
{
}
public string getPicnum()
{
GrayByPixels(); //灰度处理
//GetPicValidByValue(128, 6); //得到有效空间
Bitmap[] pics = GetSplitPics(6, 1); //分割
if (pics.Length != 6)
{
return "NULL"; //分割错误
}
else // 重新调整大小
{
pics[0] = GetPicValidByValue(pics[0], 128);
pics[1] = GetPicValidByValue(pics[1], 128);
pics[2] = GetPicValidByValue(pics[2], 128);
pics[3] = GetPicValidByValue(pics[3], 128);
pics[4] = GetPicValidByValue(pics[4], 128);
pics[5] = GetPicValidByValue(pics[5], 128);
}
OCRHelp.Binarizate(pics[0]);
OCRHelp.Binarizate(pics[1]);
OCRHelp.Binarizate(pics[2]);
OCRHelp.Binarizate(pics[3]);
OCRHelp.Binarizate(pics[4]);
OCRHelp.Binarizate(pics[5]);
// if (!textBoxInput.Text.Equals(""))
string result = "";
char singleChar = ;
{
for (int i = 0; i < 6; 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;
}
}
}
UnCodeBase.cs代码
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
namespace BallotAiying2
{
class UnCodebase
{
public Bitmap bmpobj;
public UnCodebase(Bitmap pic)
{
// if (pic.PixelFormat == PixelFormat.Format8bppIndexed)
bmpobj = new Bitmap(pic); //转换为Format32bppRgb
}
/// <summary>
/// 根据RGB,计算灰度值
/// </summary>
/// <param name="posClr">Color值</param>
/// <returns>灰度值,整型</returns>
private int GetGrayNumColor(System.Drawing.Color posClr)
{
return (posClr.R * 19595 + posClr.G * 38469 + posClr.B * 7472) >> 16;
}
/// <summary>
/// 灰度转换,逐点方式
/// </summary>
public void GrayByPixels()
{
for (int i = 0; i < bmpobj.Height; i++)
{
for (int j = 0; j < bmpobj.Width; j++)
{
int tmpValue = GetGrayNumColor(bmpobj.GetPixel(j, i));
bmpobj.SetPixel(j, i, Color.FromArgb(tmpValue, tmpValue, tmpValue));
}
}
}
/// <summary>
/// 去图形边框
/// </summary>
/// <param name="borderWidth"></param>
public void ClearPicBorder(int borderWidth)
{
for (int i = 0; i < bmpobj.Height; i++)
{
for (int j = 0; j < bmpobj.Width; j++)
{
if (i < borderWidth || j < borderWidth || j > bmpobj.Width - 1 - borderWidth || i > bmpobj.Height - 1 - borderWidth)
bmpobj.SetPixel(j, i, Color.FromArgb(255, 255, 255));
}
}
}
/// <summary>
/// 灰度转换,逐行方式
/// </summary>
public void GrayByLine()
{
Rectangle rec = new Rectangle(0, 0, bmpobj.Width, bmpobj.Height);
BitmapData bmpData = bmpobj.LockBits(rec, ImageLockMode.ReadWrite, bmpobj.PixelFormat);// PixelFormat.Format32bppPArgb);
// bmpData.PixelFormat = PixelFormat.Format24bppRgb;
IntPtr scan0 = bmpData.Scan0;
int len = bmpobj.Width * bmpobj.Height;
int[] pixels = new int[len];
Marshal.Copy(scan0, pixels, 0, len);
//对图片进行处理
int GrayValue = 0;
for (int i = 0; i < len; i++)
{
GrayValue = GetGrayNumColor(Color.FromArgb(pixels[i]));
pixels[i] = (byte)(Color.FromArgb(GrayValue, GrayValue, GrayValue)).ToArgb(); //Color转byte
}
bmpobj.UnlockBits(bmpData);
////输出
//GCHandle gch = GCHandle.Alloc(pixels, GCHandleType.Pinned);
//bmpOutput = new Bitmap(bmpobj.Width, bmpobj.Height, bmpData.Stride, bmpData.PixelFormat, gch.AddrOfPinnedObject());
//gch.Free();
}
/// <summary>
/// 得到有效图形并调整为可平均分割的大小
/// </summary>
/// <param name="dgGrayValue">灰度背景分界值</param>
/// <param name="CharsCount">有效字符数</param>
/// <returns></returns>
public void GetPicValidByValue(int dgGrayValue, int CharsCount)
{
int posx1 = bmpobj.Width; int posy1 = bmpobj.Height;
int posx2 = 0; int posy2 = 0;
for (int i = 0; i < bmpobj.Height; i++) //找有效区
{
for (int j = 0; j < bmpobj.Width; j++)
{
int pixelValue = bmpobj.GetPixel(j, i).R;
if (pixelValue < dgGrayValue) //根据灰度值
{
if (posx1 > j) posx1 = j;
if (posy1 > i) posy1 = i;
if (posx2 < j) posx2 = j;
if (posy2 < i) posy2 = i;
};
};
};
// 确保能整除
int Span = CharsCount - (posx2 - posx1 + 1) % CharsCount; //可整除的差额数
if (Span < CharsCount)
{
int leftSpan = Span / 2; //分配到左边的空列 ,如span为单数,则右边比左边大1
if (posx1 > leftSpan)
posx1 = posx1 - leftSpan;
if (posx2 + Span - leftSpan < bmpobj.Width)
posx2 = posx2 + Span - leftSpan;
}
//复制新图
Rectangle cloneRect = new Rectangle(posx1, posy1, posx2 - posx1 + 1, posy2 - posy1 + 1);
bmpobj = bmpobj.Clone(cloneRect, bmpobj.PixelFormat);
}
/// <summary>
/// 得到有效图形,图形为类变量
/// </summary>
/// <param name="dgGrayValue">灰度背景分界值</param>
/// <param name="CharsCount">有效字符数</param>
/// <returns></returns>
public void GetPicValidByValue(int dgGrayValue)
{
int posx1 = bmpobj.Width; int posy1 = bmpobj.Height;
int posx2 = 0; int posy2 = 0;
for (int i = 0; i < bmpobj.Height; i++) //找有效区
{
for (int j = 0; j < bmpobj.Width; j++)
{
int pixelValue = bmpobj.GetPixel(j, i).R;
if (pixelValue < dgGrayValue) //根据灰度值
{
if (posx1 > j) posx1 = j;
if (posy1 > i) posy1 = i;
if (posx2 < j) posx2 = j;
if (posy2 < i) posy2 = i;
};
};
};
//复制新图
Rectangle cloneRect = new Rectangle(posx1, posy1, posx2 - posx1 + 1, posy2 - posy1 + 1);
bmpobj = bmpobj.Clone(cloneRect, bmpobj.PixelFormat);
}
/// <summary>
/// 得到有效图形,图形由外面传入
/// </summary>
/// <param name="dgGrayValue">灰度背景分界值</param>
/// <param name="CharsCount">有效字符数</param>
/// <returns></returns>
public Bitmap GetPicValidByValue(Bitmap singlepic, int dgGrayValue)
{
int posx1 = singlepic.Width; int posy1 = singlepic.Height;
int posx2 = 0; int posy2 = 0;
for (int i = 0; i < singlepic.Height; i++) //找有效区
{
for (int j = 0; j < singlepic.Width; j++)
{
int pixelValue = singlepic.GetPixel(j, i).R;
if (pixelValue < dgGrayValue) //根据灰度值
{
if (posx1 > j) posx1 = j;
if (posy1 > i) posy1 = i;
if (posx2 < j) posx2 = j;
if (posy2 < i) posy2 = i;
};
};
};
//复制新图
Rectangle cloneRect = new Rectangle(posx1, posy1, posx2 - posx1 + 1, posy2 - posy1 + 1);
return singlepic.Clone(cloneRect, singlepic.PixelFormat);
}
/// <summary>
/// 平均分割图片
/// </summary>
/// <param name="RowNum">水平上分割数</param>
/// <param name="ColNum">垂直上分割数</param>
/// <returns>分割好的图片数组</returns>
public Bitmap [] GetSplitPics(int RowNum,int ColNum)
{
if (RowNum == 0 || ColNum == 0)
return null;
int singW = bmpobj.Width / RowNum;
int singH = bmpobj.Height / ColNum;
Bitmap [] PicArray=new Bitmap[RowNum*ColNum];
Rectangle cloneRect;
for (int i = 0; i < ColNum; i++) //找有效区
{
for (int j = 0; j < RowNum; j++)
{
cloneRect = new Rectangle(j*singW, i*singH, singW , singH);
PicArray[i*RowNum+j]=bmpobj.Clone(cloneRect, bmpobj.PixelFormat);//复制小块图
}
}
return PicArray;
}
/// <summary>
/// 返回灰度图片的点阵描述字串,1表示灰点,0表示背景
/// </summary>
/// <param name="singlepic">灰度图</param>
/// <param name="dgGrayValue">背前景灰色界限</param>
/// <returns></returns>
public string GetSingleBmpCode(Bitmap singlepic, int dgGrayValue)
{
Color piexl;
string code = "";
for (int posy = 0; posy < singlepic.Height; posy++)
for (int posx = 0; posx < singlepic.Width; posx++)
{
piexl = singlepic.GetPixel(posx, posy);
if (piexl.R < dgGrayValue) // Color.Black )
code = code + "1";
else
code = code + "0";
}
return code;
}
}
}
OCRHelp.cs代码
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
namespace Ocr
{
class OCRHelp
{
public static void Binarizate(Bitmap map)
{
//int tv = ComputeThresholdValue(map);
int tv = 125;
int x = map.Width;
int y = map.Height;
for (int i = 0; i < x; i++)
{
for (int j = 0; j < y; j++)
{
if (map.GetPixel(i, j).G >= tv)
{
map.SetPixel(i, j, Color.FromArgb(0xff, 0xff, 0xff));
}
else
{
map.SetPixel(i, j, Color.FromArgb(0, 0, 0));
}
}
}
}
private static int ComputeThresholdValue(Bitmap img)
{
int i;
int k;
double csum;
int thresholdValue = 1;
int[] ihist = new int[0x100];
for (i = 0; i < 0x100; i++)
{
ihist[i] = 0;
}
int gmin = 0xff;
int gmax = 0;
for (i = 1; i < (img.Width - 1); i++)
{
for (int j = 1; j < (img.Height - 1); j++)
{
int cn = img.GetPixel(i, j).R;
ihist[cn]++;
if (cn > gmax)
{
gmax = cn;
}
if (cn < gmin)
{
gmin = cn;
}
}
}
double sum = csum = 0.0;
int n = 0;
for (k = 0; k <= 0xff; k++)
{
sum += k * ihist[k];
n += ihist[k];
}
if (n == 0)
{
return 60;
}
double fmax = -1.0;
int n1 = 0;
for (k = 0; k < 0xff; k++)
{
n1 += ihist[k];
if (n1 != 0)
{
int n2 = n - n1;
if (n2 == 0)
{
return thresholdValue;
}
csum += k * ihist[k];
double m1 = csum / ((double) n1);
double m2 = (sum - csum) / ((double) n2);
double sb = ((n1 * n2) * (m1 - m2)) * (m1 - m2);
if (sb > fmax)
{
fmax = sb;
thresholdValue = k;
}
}
}
return thresholdValue;
}
}
}
版权申明:本篇文章属于原创,转载请注明出自微信投票网。原文地址: http://www.aivote.com/12983.html