首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

求网页验证码识别,该怎么处理

2012-01-11 
求网页验证码识别,急!没有如下字符:1,i,o,0,v,n,m,l,7,我头像就是这个验证码的截图,验证码出处如下,C#源码

求网页验证码识别,急!
没有如下字符:1,i,o,0,v,n,m,l,7,我头像就是这个验证码的截图,验证码出处如下,C#源码一千元酬谢,csdn项目交易流程交易!

C# code
http://pay.sdo.com/Order/newvalidatecode1.aspx


[解决办法]
转贴一下思路吧:

我举的例子是比较难于识别的验证码,不讨论不变形、不换字体、不换大小、不旋转的验证码,这里我可能不会写出代码,只是提供我编写的思路,按照这个思路, 我写出的程序,比市场上出售的程序的识别率还要高很多。(有兴趣的可以问我,我不想在这里帮别人宣传,呵呵~~)

首先以数字验证码开始,字母的要比数字麻烦一些,不过搞清楚了数字验证码的识别,字母的也就不难了。

验证码一般都是图片,而且一般都是4位数,处理过程是:先分割为4个部分,然后逐一识别,由于分割比较简单,我这里就不说了,我这里只说如何识别。

我的方法是把需要识别的图片,划分为 5 行,3 列,15个块,为什么要划分为15个块呢?先看图!
○■○ 
■○■
■○■
■○■
○■○

○■○
■■○
○■○
○■○
■■■

■■■
○○■
■■■
■○○
■■■

■■■
○○■
■■■
○○■
■■■
我先举这4个例子吧,其余的大家可以自己画出来。如果做过验证码识别的朋友,肯定很快就明白为什么划分为15个块,其实主要就是因为这样划分更合理,也就更能提高识别率。

我的方法是把需要识别的图片,划分为 5 行,3 列,15个块,然后对每个块进行计算,当每个块里的有效象素超过多少百分比的时候,就标记为 ■,如果 没超过就标记为○,(这里为了显示方便我使用了■,○,你完全可以把它标为1、0),这里要注意一下,这里的百分比根据字体的粗细可以取 67%, 50%,33%,20%,为什么要取这几个数?主要和计算机的浮点数运算有关,选这几个数,运算更快,且不容易出错,否则计算机在进行大量计算时也是会出 错的!当然这里,你完全可以选适合你的验证码图片的百分比!!

如果验证码不变形、不换字体、不换大小、不旋转,我们的识别工作到这一步基本上就结束了,因为已经可以得到比较清晰的块图,对付大多数论坛,就已经足够了。^_^

如果验证码的变形比较大、且有很多字体、大小也不固定、且有旋转,那么我们经过划分、取比率显示后,可能会得到这样一个图:
○■○
○○■
○■○
■○○
■■■
那么这个数字应该是什么数字,我们需要使用排除法!排除所有不可能,在0123456789中,这个图不可能是013456789,他只能是 :2。

写过验证码识别的朋友可能已经明白了!是的,我们需要建立一个类似的数据库,也就是识别库,出现哪些图,他就属于那个数字。
再举一个例子:
○■○
■○○
■■■
■○■
○■○
这是哪个数字呢??是6,没错

我这里我需要再说明一下为什么要取5行,3列,15个块,因为块太多了你 的识别库就会很大,块太少了,就会出现很多分不清楚的块图。

另外你取的百分比也需要注意,不能太大也不能太小。

好了,等做好自己的数据库,这时就可以识别大部分数字了。

最后还有一个问题,就是重复的问题,比如,图片上的数字,明明是 5,可由于它的字体不是常见的字体,且发生了旋转,最后得到这样一个图:
■■■
■○○
■■■
■○■
■■■
在我的数据库里,这个块图,是6,也是就说识别错误,怎么办?

我的解决方法是,在数据库里先把这条数据删除因为这个是错误的。

遇到这种情况,就需要进行二次处理,我的方法是:降低百分比,这时就得到了:
■■○
■○○
■■○
○○■
■■○
OK,经过降低百分比,图片就由“6”又变为“5”了,呵呵~~~由于降低了百分比,我们需要再建立一个识别库的来存储这些数据。
[解决办法]
仔细看了下,确实不简单。工作量大,算法多……
[解决办法]
早期我对验证码识别做过一些研究 你这个比较有难度 也不能说很难
你看看 http://hi.baidu.com/codeing 很早就写的 虽然很烂 但给我以后在验证码识别提供了不少思路
并且我也写出过类似楼主要求的验证码识别 识别率在90% 以上
[解决办法]
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Untitled Page</title> <script>function show(im) { im.src="Default2.aspx?"+new Date;//注意这里的new Date一定要加上,否则只会生成一次验证码 }</script> </head> <body> <form id="form1" runat="server"> <div> <img src="Default2.aspx" onclick ="show(this)" /> </div> </form> </body> </html> <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

<title>Untitled Page</title>



<script>function show(im)

{

im.src="Default2.aspx?"+new Date;//注意这里的new Date一定要加上,否则只会生成一次验证码

}</script>

</head>

<body>

<form id="form1" runat="server">

<div>

<img src="Default2.aspx" onclick ="show(this)" />

</div>

</form>

</body>

</html>
Default2.cs代码 主要用来生成验证图片
view plaincopy to clipboardprint?
<FONT color=#000000>using System;
  
using System.Data;
  
using System.Configuration;
  
using System.Collections;
  
using System.Web;
  
using System.Web.Security;
  
using System.Web.UI;
  
using System.Web.UI.WebControls;
  
using System.Web.UI.WebControls.WebParts;
  
using System.Web.UI.HtmlControls;
  
using System.Drawing;
  
using System.Drawing.Drawing2D;
  
  
  
public partial class Default2 : System.Web.UI.Page
  
{
  
protected void Page_Load(object sender, EventArgs e)
  
{
  
CreateCheckCodeImage(GenerateCheckCode());
  
}
  
/// <summary>
  
/// 生成随机校验码字符串
  
/// </summary>
  
/// <returns>生成的随机校验码字符串</returns>
  
public string GenerateCheckCode()
  
{
  
int number;
  
string strCode = string.Empty;
  
//随机数种子
  
Random radom = new Random();
  
for (int i = 0; i < 4; i++)
  
{
  
//随机的整数
  
number = radom.Next();
  
//字符从0-9,A-Z中随机产生,对应的ASCII码分别为48-57,65,90
  
number = number % 36;
  
if (number < 10)
  
{
  
number += 48;
  
}
  
else
  
{
  
number += 55;
  
}
  
strCode += ((char)number).ToString();
  
}
  
//在Cookie中保存校验码或Session中
  
Response.Cookies.Add(new HttpCookie("CheckCode", strCode));
  
return strCode;
  
}
  
/// <summary>
  
/// 根据校验码输出图片
  
/// </summary>
  
/// <param name="checkCode">产生的随机校验码</param>
  
public void CreateCheckCodeImage(string checkCode)
  
{
  
//若校验码为空,则直接返回
  
if (checkCode == null || checkCode.Trim() == string.Empty)
  
{
  
return;
  
}
  
//根据校验码的长度确定输出图片的长度
  
System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling(decimal.Parse((checkCode.Length * 15)+"")), 20);
  


//创建GRAPHICS对象
  
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);
  
try
  
{
  
//生成随机数种子
  
Random random = new Random();
  
//清空图片背景色
  
g.Clear(System.Drawing.Color.White);
  
//画图片的背景噪音线10条
  
for (int i = 0; i < 10; i++)
  
{
  
//噪音线起点坐标(x1,y1),终点坐标(x2,y2)
  
int x1 = random.Next(image.Width);
  
int x2 = random.Next(image.Width);
  
int y1 = random.Next(image.Height);
  
int y2 = random.Next(image.Height);
  
//用银色画出噪音线
  
g.DrawLine(new System.Drawing.Pen(System.Drawing.Color.Silver), x1, y1, x2, y2);
  
}
  
//输出图片中校验码的字体:12号Arial,粗斜体
  
System.Drawing.Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
  
//线形渐变画刷
  
System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new System.Drawing.Rectangle(0, 0, image.Width, image.Height), System.Drawing.Color.Blue, System.Drawing.Color.Purple, 1.2f, true);
  
g.DrawString(checkCode, font, brush, 2, 3);
  
//画图片的前景噪音点50个
  
for (int i = 0; i < 50; i++)
  
{
  
int x = random.Next(image.Width);
  
int y = random.Next(image.Height);
  
image.SetPixel(x, y, System.Drawing.Color.FromArgb(random.Next()));
  
}
  
//画图片的边框线
  
g.DrawRectangle(new System.Drawing.Pen(System.Drawing.Color.SaddleBrown), 0, 0, image.Width - 1, image.Height - 1);
  
//创建内存流用于输出图片
  
using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
  
{
  
//图片格式指定为PNG
  
image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
  
//清除缓冲区流中的所有输出
  
Response.ClearContent();
  
//输出流的HTTPMIME类型设置为"image/Png"
  
Response.ContentType = "image/Png";
  
//输出图片的二进制流
  
Response.BinaryWrite(ms.ToArray());
  
}
  
}
  
finally
  
{
  
//释放 Bitmap 对象和 Graphics 对象
  
g.Dispose();
  
image.Dispose();
  
}
  
}
  
}</FONT>

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/zhlu880516/archive/2008/07/12/2642473.aspx
[解决办法]
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;


using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using System.IO;

namespace Shop
{
/// <summary>
/// Summary description for ValidateCode.
/// </summary>
public class ValidateCode : System.Web.UI.Page
{
/// <summary>
/// Validation Code generated fromt these charaters.
/// Note: l,L 1(number), o, O, 0(number) are removed
/// </summary>
private const string strValidateCodeBound = "abcdefghijkmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ23456789";
private static string[] Fonts = new string[] { "Helvetica",
"Geneva", 
"sans-serif",
"Verdana",
"Times New Roman",
"Courier New",
"Arial"
};


#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

/// <summary>
/// event handler of page load
/// </summary>
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
string str_ValidateCode = GetRandomString(6);

Session["ValidateCode"] = str_ValidateCode;
CreateImage(str_ValidateCode);
}

}

/// <summary>
/// Generate random string
/// </summary>
private string GetRandomString(int int_NumberLength)
{
string valString = string.Empty;
Random theRandomNumber = new Random((int)DateTime.Now.Ticks);

for (int int_index = 0; int_index < int_NumberLength; int_index++)
valString += strValidateCodeBound[theRandomNumber.Next(strValidateCodeBound.Length - 1)].ToString();

return valString;
}

/// <summary>
/// Generate random Color
/// </summary>
private Color GetRandomColor()
{
Random RandomNum_First = new Random((int)DateTime.Now.Ticks);

System.Threading.Thread.Sleep(RandomNum_First.Next(50));
Random RandomNum_Sencond = new Random((int)DateTime.Now.Ticks);


int int_Red = RandomNum_First.Next(256);
int int_Green = RandomNum_Sencond.Next(256);
int int_Blue = (int_Red + int_Green > 400) ? 0 : 400 - int_Red - int_Green;
int_Blue = (int_Blue > 255) ? 255 : int_Blue;

return Color.FromArgb(int_Red, int_Green, int_Blue);
}

/// <summary>
/// Create Validation Code Image
/// </summary>
private void CreateImage(string str_ValidateCode)
{


int int_ImageWidth = str_ValidateCode.Length * 22;
Random newRandom = new Random();

Bitmap theBitmap = new Bitmap(int_ImageWidth + 6 , 38);
Graphics theGraphics = Graphics.FromImage(theBitmap);

theGraphics.Clear(Color.White);

drawLine(theGraphics, theBitmap, newRandom);
theGraphics.DrawRectangle(new Pen(Color.LightGray, 1), 0, 0, theBitmap.Width - 1, theBitmap.Height -1 );

for (int int_index = 0; int_index < str_ValidateCode.Length; int_index++)
{
Matrix X = new Matrix();
X.Shear((float)newRandom.Next(0,300)/1000 - 0.25f, (float)newRandom.Next(0,100)/1000 - 0.05f);
theGraphics.Transform = X;
string str_char = str_ValidateCode.Substring(int_index, 1);
System.Drawing.Drawing2D.LinearGradientBrush newBrush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, theBitmap.Width, theBitmap.Height), Color.Blue, Color.DarkRed, 1.2f, true); 
Point thePos = new Point(int_index * 21 + 1 + newRandom.Next(3), 1 + newRandom.Next(13));

Font theFont = new Font(Fonts[newRandom.Next(Fonts.Length -1)], newRandom.Next(14,18), FontStyle.Bold);

theGraphics.DrawString(str_char, theFont, newBrush, thePos);
}

drawPoint(theBitmap, newRandom);

MemoryStream ms = new MemoryStream();
theBitmap.Save(ms, ImageFormat.Png);

Response.ClearContent(); 
Response.ContentType = "image/Png";
Response.BinaryWrite(ms.ToArray());
theGraphics.Dispose();
theBitmap.Dispose();
Response.End();
}

/// <summary>
/// Draw Line for noise
/// </summary>
private void drawLine(Graphics gfc,Bitmap img, Random ran)
{
for (int i = 0; i < 10; i++)
{
int x1 = ran.Next(img.Width);
int y1 = ran.Next(img.Height);
int x2 = ran.Next(img.Width);
int y2 = ran.Next(img.Height);
gfc.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
}
}

/// <summary>
/// Draw Point for noise
/// </summary>
private void drawPoint(Bitmap img, Random ran)
{
for (int i = 0; i < 30; i++)
{
int x = ran.Next(img.Width);
int y = ran.Next(img.Height);
img.SetPixel(x,y,Color.FromArgb(ran.Next()));
}

}

}
}

热点排行