这种还算是比较规则的。。
嗯嗯,是的 识别出来他的结果。 就是这种算术类的验证码。
@黄飞飞: 图片先二值化,然后利用相似度对比判断出结果。最后再进行算数运算。
@幻天芒: 你好,能提供一下代码 谢谢。
@黄飞飞: 代码不少,你根据提供的关键字自行解决吧。。
把他们都分别赋给自己声明3个不同的对象 进行用自己的对象 进行比较 就可以了
这是张图片,怎么赋值到对象 你能说明白些吗?谢谢
调用的代码:
System.Drawing.Graphics g = null;
System.Drawing.Bitmap image = null;
System.IO.MemoryStream ms = new System.IO.MemoryStream();
try
{
decimal a = tool.GetRandomNum(1,100);
decimal b = tool.GetRandomNum(1, 100);
string code = string.Format("{0}减去{1}等于?", a, b);
string result = string.Format("{0}", a - b);
if (null == context.Session["voliCode"])
{
context.Session.Add("voliCode", result);
}
else
{
context.Session["voliCode"] = result;
}
tool.CreateValidateCodeImage(ref image, ref g, code, 400, 40, false);
image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
tool.GetUnixTimestamp();
}
catch
{
}
finally
{
image.Dispose();
g.Dispose();
}
context.Response.ClearContent();
context.Response.Headers.Add("Content-disposition","attachment; filename=validate.png");
context.Response.ContentType = "image/Png";
context.Response.BinaryWrite(ms.ToArray());
具体方法所在的类:
public string CreateRandomCharacter(short num)
{
StringBuilder rc = null;
try
{
rc = new StringBuilder();
List<string> list = GetRandomCharArray();
HashSet<int> set = GetRandomNumSets(num, 0, list.Count);
foreach (int idx in set)
{
rc.Append(list[idx]);
}
}
catch (Exception)
{
rc = new StringBuilder();
}
return rc.ToString();
}
public static List<string> GetRandomCharArray()
{
List<string> list = null;
//定义ascii码数组:即0、9,A、Z,a、z的代表的ascii码值
short[] maxLen = new short[] { 48, 57, 65, 90, 97, 122 };
try
{
list = new List<string>();
int j = 0;
while (((maxLen.Length / 2) + 1) >= j)
{
short i = maxLen[j++];
while (maxLen[j] >= i)
{
list.Add(Convert.ToString(Convert.ToChar(i++)));
}
j++;
}
}
catch (Exception)
{
list = null;
}
return list;
}
public string GetRandomChinese(int len)
{
string result = string.Empty;
string c = GetChineseString();
int i = 0;
Random r = new Random(GetUnixTimestamp());
while (len > i++)
{
int idx = r.Next(c.Length);
result += c[idx].ToString();
}
return result;
}
// 现代汉语常用字(共3500个)
public string GetChineseString()
{
var set = new HashSet<char>(GetGB2312String());
set.ExceptWith("皑胺盎敖翱佰稗镑鲍钡苯甭迸毖敝陛卞斌摈炳钵铂箔帛蔡搽诧谗掣郴骋炽踌瞅躇滁" +
"搐椽疵茨蹿瘁淬磋傣殆郸惮迪狄翟滇靛凋迭侗恫犊遁掇剁峨娥厄鄂洱珐藩钒酚汾烽氟涪弗釜腑阜讣噶" +
"嘎赣皋铬庚龚蛊剐圭癸辊骸氦邯郝菏貉阂涸亨弘瑚桓豢磺簧卉烩姬缉汲蓟伎悸笺缄硷槛饯铰桔睫藉疥" +
"靳烬粳痉炯厩咎狙疽咀踞娟撅攫抉浚郡喀咯亢柯侩匡岿奎馈婪阑谰佬镭磊傈涟撂廖霖拎羚陇掳麓潞禄" +
"戮挛孪滦纶嘛谩卯酶镁寐醚幂抿牟氖淖妮霓倪拈啮镍涅哦沤啪琶磐耪呸裴抨砒琵毗痞瞥粕莆埔曝沏祁" +
"讫扦钎仟堑羌蔷橇鞘沁氰邱酋泅龋颧醛炔榷冉壬妊戎茹孺汝阮鳃莎煽汕缮墒韶邵慑砷娠噬仕孰戍舜朔" +
"嗣巳怂擞僳隋绥蓑獭挞酞坍绦锑嚏腆迢眺烃汀酮湍陀哇烷皖韦惟潍渭挝斡钨吾毋戊硒矽嘻烯汐檄襄霄" +
"忻惺墟戌嘘眩绚丫焉阉彦佯疡瑶尧噎耶曳铱颐沂彝矣臆裔诣翌荫寅尹臃痈雍恿铀酉釉盂虞俞渝禹峪驭" +
"垣苑曰郧匝哉札咋詹辗湛漳瘴肇蛰锗甄砧臻帧峙炙痔诌诛瞩拽篆兹淄孜渍鬃邹纂佐柞");
set.UnionWith("叨蜓筝蜻橘匕丐夭叽吆凫阱芙杈岖鸠沐姊卦拗茉昙肴衩玷茴荞荠盹咧昵咪秕胧奕飒炫" +
"祠荸莺桦唠蚣蚪蚓唧秫麸捺匾蚯蛉啰铐铛笙笤偎徙翎庵涮悴裆谒雳跛锉掰牍腌猬愕鹉蒿榄楣嗦跷蜈嗤" +
"馍禀缤榛榕嘁嘀幔箫漩橄嘹蝠蝌蝙鲫憔翩嬉缭薇噩蟥霎踱蹂蟆螃鹦瘾缰檐檩瞭蟋蟀朦臊鳄鳍癞璧簸鬓躏");
return new string(set.ToArray());
}
// 国标一级字(共3755个): 区:16-55, 位:01-94, 55区最后5位为空位
private string GetGB2312String()
{
var list = new List<byte>();
for (var area = 16; area <= 55; area++)
for (int sit = (area == 55) ? 89 : 94, sit2 = 1; sit2 <= sit; sit2++)
{
list.Add((byte)(area + 0xa0));
list.Add((byte)(sit2 + 0xa0));
}
return Encoding.GetEncoding("GB2312").GetString(list.ToArray());
}
public void CreateValidateCodeImage(ref Bitmap img, ref Graphics g, string r, short width, short height, bool isShowBorder)
{
int lineCount = 6;
int dotCount = 6;
int i = 0;
img = new System.Drawing.Bitmap(width, height);
g = Graphics.FromImage(img);
Color white = Color.FromArgb(238, 238, 238);
Color black = Color.Blue;
Color rndColor = Color.White;
System.Drawing.Font f = new System.Drawing.Font("Arial", 10, System.Drawing.FontStyle.Bold);
Brush b = new System.Drawing.SolidBrush(white);
g.Clear(white);
if (isShowBorder)
{
g.FillRectangle(new System.Drawing.SolidBrush(Color.Blue), 0, 0, img.Width, img.Height);
}
HashSet<Color> lineColors = GetRndColors(lineCount, 200, 255, 200, 255, 200, 255);
HashSet<int> lineX1s = GetRandomNumSets(lineCount, 0, width);
HashSet<int> lineY1s = GetRandomNumSets(lineCount, 0, height);
HashSet<int> lineX2s = GetRandomNumSets(lineCount, 0, width);
HashSet<int> lineY2s = GetRandomNumSets(lineCount, 0, height);
//画出6条随机直线
while (lineCount > i)
{
Pen pen = new Pen(lineColors.ElementAt<Color>(i));
g.DrawLine(pen, new Point(lineX1s.ElementAt<int>(i), lineY1s.ElementAt<int>(i)), new Point(lineX2s.ElementAt<int>(i), lineY2s.ElementAt<int>(i)));
i++;
}
HashSet<Color> dotColors = GetRndColors(dotCount, 0, 50, 0, 50, 0, 50);
HashSet<int> dotXs = GetRandomNumSets(dotCount, 0, width);
HashSet<int> dotYs = GetRandomNumSets(dotCount, 0, height);
i = 0;
//画点
while (dotCount > i)
{
Pen pen = new Pen(dotColors.ElementAt<Color>(i));
g.DrawString("*", new System.Drawing.Font("Arial", 6, System.Drawing.FontStyle.Regular), new System.Drawing.SolidBrush(rndColor), dotXs.ElementAt<int>(i), dotYs.ElementAt<int>(i));
i++;
}
HashSet<Color> strColor = GetRndColors(r.Length, 0, 50, 0, 50, 0, 155);
i = 0;
HashSet<int> sizes = GetRandomNumSets(r.Length, 8, 18);
HashSet<int> strYs = GetRandomNumSets(r.Length, 1, height / 2);
while (r.Length > i)
{
int x = (width / r.Length) * i;
int y = strYs.ElementAt<int>(i);//GetRandomNum(1, height / 2);
g.DrawString(Convert.ToString(r[i]), new System.Drawing.Font("Microsoft YaHei", 14, System.Drawing.FontStyle.Regular), new System.Drawing.SolidBrush(strColor.ElementAt<Color>(i)), x, y);
i++;
}
}
/// <summary>
/// 产生随机颜色
/// </summary>
/// <param name="minR">最小Red值0-255</param>
/// <param name="maxR">最大Red值0-255</param>
/// <param name="minG">最小Green值0-255</param>
/// <param name="maxG">最大Green值0-255</param>
/// <param name="minB">最小Blue值0-255</param>
/// <param name="maxB">最大Blue值0-255</param>
/// <returns>随机颜色</returns>
public Color GetRndColor(ushort minR, ushort maxR, ushort minG, ushort maxG, ushort minB, ushort maxB)
{
HashSet<int> rgb = new HashSet<int>();
while (!rgb.Add(GetRandomNum(minR, maxR))) ;
while (!rgb.Add(GetRandomNum(minG, maxG))) ;
while (!rgb.Add(GetRandomNum(minB, maxB))) ;
//产生随机颜色数字越大表示颜色越浅,255表示白色
Color rndColor = Color.FromArgb(rgb.ElementAt<int>(0), rgb.ElementAt<int>(1), rgb.ElementAt<int>(2));
return rndColor;
}
public HashSet<Color> GetRndColors(int num, ushort minR, ushort maxR, ushort minG, ushort maxG, ushort minB, ushort maxB)
{
HashSet<Color> sets = new HashSet<Color>();
int i = 0;
while (num > i++)
{
while (!sets.Add(GetRndColor(minR, maxR, minG, maxG, minB, maxB))) ;
}
return sets;
}
public int GetRandomNum(int min, int max)
{
Random randomNum = new Random(DateTime.Now.Millisecond);
return randomNum.Next(min, max);
}
public HashSet<int> GetRandomNumSets(int num, int min, int max)
{
HashSet<int> set = new HashSet<int>();
int i = 0;
while (num > i++)
{
while (!set.Add(GetRandomNum(min, max))) ;
}
return set;
}
这里还包含其它的方法与处理此验证码无关,我没有去掉,你自己看看吧:)
http://git.oschina.net/bobo2cj/iamge2text
你好! 这发的这个示例好像不能识别我的验证码?
@黄飞飞: 。。