数字图形识别代码
时间:2025-07-09
时间:2025-07-09
数字图形识别代码
using
System;
using
http://www.77cn.com.cn;
using
System.Web;
using
System.Drawing;
using
System.Drawing.Imaging;
namespace CrMonterNet
{
/// <summary>
/// CrMonterNetImg 的摘要说明。
/// </summary>
public class
CrMonterNetImg
{
/// <summary>
/// Cookie集合,使用这个cookie集合去获取验证码
/// </summary>
private CookieCollection Cookie=null
;
/// <summary>
/// 验证码的地址
/// </summary>
private string
ImgUrl="";
/// <summary>
/// 构造函数
/// </summary>
/// <param name="StrUrl">验证码地址</param>
/// <param name="Cookies">cookie集合</param>
public CrMonterNetImg(string
StrUrl, CookieCollection Cookies)
{
this
.ImgUrl = StrUrl;
this
.Cookie = Cookies;
}
private bool _debug=false
;
/// <summary>
/// 设置是否为debug,如果是debug的话,就把当前图片保存到
c: .bmp
/// </summary>
public bool
debug
{
set
{
数字图形识别代码
_debug=value;
}
get
{
return
_debug;
}
}
/// <summary>
/// 识别主函数
/// </summary>
/// <returns></returns>
public string
proc()
{
//图形
Bitmap bmp=null
;
//保存深浅2种颜色
Color[] cc = new
Color[2];
int[] left = new int[5];//左边的点
int[] right = new int[5];//右边的点
int[] top = new int[5];//顶点
int[] bottom = new int[5];//底点
string
str = "";
try
{
int
i=0;
// '抓取图片
while
(i != 4)
{//一直抓到图形中有4个独立的字符
bmp = this
.crImg();
if
(debug)
{
bmp.Save("c:\1.bmp");
}
cc = this.GetDeepColor(bmp);//得到深浅颜色
this.FormatImg(bmp, cc);//格式话图形,去掉背景
Array array1 = this.GetV(bmp);//得到x轴上有黑色点的坐标
i = this.chkImg(array1, ref left, ref right);//分析字符个数
}
//这里分析单个字符的上下左右点,这样就可以确定范围了
this.getTB(bmp, left , right, ref top, ref bottom);
数字图形识别代码
//识别出来
str = this
.crImg(bmp, left, right, top, bottom);
//释放资源
bmp.Dispose();
}
catch
(Exception exception2)
{
//ProjectData.SetProjectError(exception2);
Exception exception1 = exception2;
//ProjectData.ClearProjectError();
}
finally
{//释放资源
if (bmp != null
)
{
bmp.Dispose();
}
}
return
str;
}
#region 其他辅助函数
/// <summary>
/// 加载验证码
/// </summary>
/// <returns>验证码图形</returns>
private
Bitmap crImg()
{
Bitmap bmp=null
;
//响应
HttpWebResponse response=null
;
try
{
//请求
HttpWebRequest request = (HttpWebRequest) WebRequest.Create(this
.ImgUrl);
//这里附加
cookie
request.CookieContainer = new
CookieContainer();
if (this.Cookie != null
)
{
request.CookieContainer.Add(this.Cookie);
数字图形识别代码
}
//得到响应
response = (HttpWebResponse) request.GetResponse();
//从响应流直接创建图片
bmp = new
Bitmap(response.GetResponseStream());
}
catch
{
}
finally
{//清理资源
if (response != null
)
{
response.Close();
}
}
return
bmp;
}
/// <summary>
/// "得到垂直投影",就是得到字符在y轴上有的点
/// </summary>
/// <param name="img">图形</param>
/// <returns>返回这些点所在的array</returns>
private
Array GetV(Bitmap img)
{
string
str = "";
for (int
i = 0; i <= img.Width - 1; i++)
{//在图形的x轴上进行循环
//然后在y轴上找黑点
for (int
h = 0; h <= img.Height - 1; h++)
{
Color c = img.GetPixel(i, h);
if
(c.ToArgb() == Color.Black.ToArgb())
{//如果找到一个黑点的话,就把这个x轴的坐标给记忆下来。
str = str + "," + i.ToString();
break
;
}
}
}
if (str.Length > 0)
数字图形识别代码
{//因为上面采用连接的方式,所以第一个字符可能是“,”,这里去掉
str = str.Substring(1);
}
return str.Split(new char
[] { ',' });
}
/// <summary>
/// 查找单个字符中图形中最上和最下的黑点。
/// </summary>
/// <param name="Im …… 此处隐藏:5323字,全部文档内容请下载后查看。喜欢就下载吧 ……