您的位置:首页 > 其它

简单验证码的识别

2007-01-16 13:51 302 查看
详细代码下载:http://www.ssxz.com/iuhxq/index.html
版权所有,欢迎转载,转载请标明出处并保持文章完整。

作者:小灰
QQ:4111852
http://www.ssxz.com

using System;
using System.Drawing;
using System.IO;
using System.Collections;
using System.Net;
using System.Text;
using System.Threading;
using System.Diagnostics;

namespace qq
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class Class1
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
Bitmap bmp = new Bitmap(Directory.GetCurrentDirectory() + "/temp/20070115092546.bmp");
string result = OCR(bmp);
Console.WriteLine(result);
Console.WriteLine("欢迎访问 搜视小站 http://ssxz.com");
Process.Start("http://ssxz.com");
Console.Read();
}

/// <summary>
/// 识别位图
/// </summary>
/// <param name="bmp"></param>
/// <returns></returns>
public static string OCR(Bitmap bmp)
{
ArrayList swatch = new ArrayList();
string[] files = Directory.GetFiles(Directory.GetCurrentDirectory() + "/swatch/");
foreach (string file in files)
{
Bitmap tmpBmp = new Bitmap(file);
Color[,] item = new Color[tmpBmp.Width, tmpBmp.Height];
for (int i = 0; i < tmpBmp.Width; i++)
{
for (int j = 0; j < tmpBmp.Height; j++)
{
item[i,j] = tmpBmp.GetPixel(i,j);
}
}

swatch.Add(new object[]{item, tmpBmp.Width, tmpBmp.Height, new FileInfo(file).Name.Substring(0, 1)});
}

int width = bmp.Width;
int height = bmp.Height;


Color bg = bmp.GetPixel(0, 0);
Color[,] arr = new Color[width,height];

for (int i = 0; i < width; i++)
{
for (int j = 0; j < height; j++)
{
arr[i,j] = bmp.GetPixel(i, j);
}
}

//反转
arr = Overturn(arr, width, height);

//分割
ArrayList arrl = Split(arr, width, height);

//取有效区域
ArrayList arr1 = new ArrayList();
for (int i = 0; i < arrl.Count; i++)
{
Color[,] tmp = arrl[i] as Color[,];
int newWidth = tmp.Length/height;
int newHeight = height;
tmp = GetArea(tmp, ref newWidth, ref newHeight);
arr1.Add(new object[]{tmp, newWidth, newHeight});
}

//比较
ArrayList result = new ArrayList();
for (int i = 0; i < arr1.Count; i++)
{
object[] os = arr1[i] as object[];
for (int j = 0; j < swatch.Count; j++)
{
object[] os1 = swatch[j] as object[];

Color[,] a = os[0] as Color[,];
Color[,] b = os1[0] as Color[,];

if (Comp(a, int.Parse(os[1].ToString()), int.Parse(os[2].ToString())
, b, int.Parse(os1[1].ToString()), int.Parse(os1[2].ToString())))
result.Add(os1[3]);
}
}

StringBuilder sb = new StringBuilder();
for (int i = 0; i < result.Count; i++)
{
sb.Append(result[i]);
}
return sb.ToString();
}

public static bool Comp(Color[,] arr, int width, int height, Color[,] swatch, int width1, int height1)
{
if (width != width1 || height != height1)
return false;

for (int i = 0; i < width; i++)
{
for (int j = 0; j < height; j++)
{
if (!arr[i,j].Equals(swatch[i,j]))
{
return false;
}
}
}

return true;
}

public static string GetPage(string url)
{
byte[] bs = new WebClient().DownloadData(url);
return Encoding.Default.GetString(bs, 0, bs.Length);
}

#region 辅助方法
/// <summary>
/// 翻转
/// </summary>
/// <param name="arr"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <returns></returns>
public static Color[,] Overturn(Color[,] arr, int width, int height)
{
Color bg = arr[0,0];
Color[,] result = new Color[width, height];
for (int i = 0; i < width; i++)
{
for (int j = 0; j < height; j++)
{
result[i,j] = Color.FromArgb(arr[i,j].R^bg.R, arr[i,j].G^bg.G, arr[i,j].B^bg.B);
}
}
return result;
}

/// <summary>
/// 分割
/// </summary>
/// <param name="arr"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <returns></returns>
public static ArrayList Split(Color[,] arr, int width, int height)
{
ArrayList result = new ArrayList();
Color bg = arr[0,0];

int start = 0;

bool find = false;
for (int i = start; i < width; i++)
{
int j = 0;
for (; j < height; j++)
{
if (!arr[i,j].Equals(bg))
{
find = true;
break;
}
}

if (find && j==height)
{

int newWidth = i - start;

Color[,] item = new Color[newWidth, height];
for (int k = 0; k < newWidth; k++)
{
for (int m = 0; m < height; m++)
{
item[k,m] = arr[k+start, m];
}
}

result.Add(item);

start = i+1;
find = false;
}
}

return result;
}


/// <summary>
/// 获取有效区域
/// </summary>
/// <param name="arr"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <returns></returns>
public static Color[,] GetArea(Color[,] arr, ref int width, ref int height)
{
Color bg = Color.FromArgb(0, 0, 0);
int x = 0;
int y = 0;

int x1 = width;
int y1 = height;

bool find = false;

for (int i = 0; i < width && !find; i++)
{
for (int j = 0; j < height; j++)
{
if (!arr[i,j].Equals(bg))
{
x = i;
find = true;
break;
}
}

}

find = false;
for (int i = 0; i < height && !find; i++)
{
for (int j = 0; j < width; j++)
{
if (!arr[j,i].Equals(bg))
{
y = i;
find = true;
break;
}
}
}

find = false;
for (int i = width-1; i >=0 && !find; i--)
{
for (int j = height-1; j >= 0; j--)
{
if (!arr[i,j].Equals(bg))
{
x1 = i;
find = true;
break;
}
}
}

find = false;
for (int i = height-1; i >=0 && !find; i--)
{
for (int j = width-1; j >= 0; j--)
{
if (!arr[j,i].Equals(bg))
{
y1 = i;
find = true;
break;
}
}
}

if (x1>x && y1>y)
{
width = x1-x+1;
height = y1-y+2;
Color[,] result = new Color[width,height];
for (int i = 0; i < width; i++)
{
for (int j = 0; j < height; j++)
{
result[i,j] = arr[x+i, y+j];
}
}

return result;
}



return null;

}

#endregion

/// <summary>
/// 生成样本
/// </summary>
public static void CreateSwatch()
{
string html = GetPage("http://www.ssxz.com");
int find = html.IndexOf("CreateCodeImg.php?var=");
string img = "";
if (find>0)
{
int end = html.IndexOf("/"", find, 100);
img = html.Substring(find, end-find);
}

img = "http://www.ssxz.com/" + img;
new WebClient().DownloadFile(img, Directory.GetCurrentDirectory() + "/" + new Random().Next(1000, 9999).ToString() + ".bmp");
}

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: