实例介绍
【实例截图】
【核心代码】
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.IO.Compression;
namespace AuctionLicenseSH
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//this.buttonOcrTip.PerformClick();
this.buttonOcrTarget.PerformClick();
}
private void buttonOcrTip_Click(object sender, EventArgs e)
{
Cracker cracker;
int indexCount = 0;
int indexSuccess = 0;
int indexFail = 0;
string outMessage = "";
DirectoryInfo dir;
//识别语义
indexCount = 0;
indexSuccess = 0;
indexFail = 0;
cracker = new Cracker(".//template_tip");
cracker.OcrRate = 0.8;
dir = new DirectoryInfo(".//tip");
foreach (FileInfo f in dir.GetFiles())
{
DateTime start = DateTime.Now;
string fileFullPath = f.FullName;
Bitmap img = new Bitmap(fileFullPath);
var result = cracker.Read(img);
string fileName = f.FullName.Substring(f.FullName.LastIndexOf('\\') 1);
string fileNameWithoutPostFix = fileName.Substring(0, fileName.LastIndexOf('.')); //去除后缀
string SuccessOrFail;
if (result.Length >0 && result.Substring(0, 1) == fileNameWithoutPostFix.Substring(0, 1)) //只需比第一个字符
{
indexSuccess ;
SuccessOrFail = "成功" indexSuccess.ToString();
}
else
{
indexFail ;
SuccessOrFail = "失败" indexFail.ToString();
}
DateTime stop = DateTime.Now;
TimeSpan time = stop.Subtract(start);
int iEclapse = time.Seconds * 1000 time.Milliseconds;
indexCount ;
outMessage = indexCount.ToString() "," SuccessOrFail "," fileName "," result.ToString()
"," iEclapse.ToString() "毫秒" Environment.NewLine;
img.Dispose();
}
this.textBox1.Text = outMessage;
}
private void buttonOcrTarget_Click(object sender, EventArgs e)
{
Cracker cracker;
cracker = new Cracker(".//template_target", Cracker.enumOcrType.OcrTarget);
cracker.OcrRate = 0.5;
int indexCount = 0;
int indexSuccess = 0;
int indexFail = 0;
string outMessage = "";
DirectoryInfo dir;
//识别数字
dir = new DirectoryInfo(".//target");
foreach (FileInfo f in dir.GetFiles())
{
DateTime start = DateTime.Now;
string fileFullPath = f.FullName;
Bitmap img = new Bitmap(fileFullPath);
var result = cracker.Read(img);
string fileName = f.FullName.Substring(f.FullName.LastIndexOf('\\') 1);
string fileNameWithoutPostFix = fileName.Substring(0, fileName.LastIndexOf('.')); //去除后缀
string SuccessOrFail;
if (result == fileNameWithoutPostFix)
{
indexSuccess ;
SuccessOrFail = "成功" indexSuccess.ToString();
}
else
{
indexFail ;
SuccessOrFail = "失败" indexFail.ToString();
}
DateTime stop = DateTime.Now;
TimeSpan time = stop.Subtract(start);
int iEclapse = time.Seconds * 1000 time.Milliseconds;
indexCount ;
outMessage = indexCount.ToString() "," SuccessOrFail "," fileName "," result.ToString()
"," iEclapse.ToString() "毫秒" Environment.NewLine;
img.Dispose();
}
this.textBox1.Text = outMessage;
}
}
public class Cracker
{
public enum enumOcrType
{
/// <summary>
/// 识别提示
/// </summary>
OcrTip,
/// <summary>
/// 识别数字
/// </summary>
OcrTarget,
}
public class CharInfo
{
public char Char { get; private set; }
public bool[,] Table { get; private set; }
public CharInfo(char ch, bool[,] table)
{
Char = ch;
Table = table;
}
}
private class MatchedChar
{
public int X { get; set; }
public int Y { get; set; }
public char Char { get; set; }
public double Rate { get; set; }
}
public List<CharInfo> words_ = new List<CharInfo>();
/// <summary>
/// 识别率
/// </summary>
private double _OcrRate;
public double OcrRate
{
get { return _OcrRate; }
set { _OcrRate = value; }
}
private enumOcrType _OcrType;
public AuctionLicenseSH.Cracker.enumOcrType OcrType
{
get { return _OcrType; }
set { _OcrType = value; }
}
public Cracker(string templatePath, enumOcrType OcrType = enumOcrType.OcrTip)
{
_OcrType = OcrType;
_OcrRate = 0.5;
var bytes = new byte[] {
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0xc5, 0x58, 0xd9, 0x92, 0x13, 0x31,
0x0c, 0x94, 0x9e, 0x93, 0x0c, 0x61, 0x97, 0x2f, 0xe1, 0x58, 0xe0, 0x91, 0x9b, 0x82, 0x62, 0x0b,
0x58, 0xee, 0xff, 0xff, 0x10, 0xd8, 0xcc, 0xc8, 0xea, 0x96, 0x6c, 0x8f, 0x13, 0x48, 0xe1, 0xaa,
0x4d, 0x46, 0x96, 0x6d, 0xb5, 0x8e, 0x96, 0x67, 0x73, 0x7f, 0x3b, 0x09, 0x0e, 0x25, 0x41, 0x49,
0xa3, 0xae, 0xd7, 0x5b, 0xa9, 0xa8, 0xd5, 0xb4, 0x76, 0x02, 0x6a, 0x5c, 0x52, 0x94, 0x54, 0xed,
0x18, 0x5a, 0x7f, 0x18, 0x00, 0x00, 0x84, 0x07, 0x1b, 0x80, 0x4a, 0x9a, 0x08, 0x35, 0xb8, 0x81,
0x50, 0xe7, 0xad, 0xbe, 0xc4, 0x8e, 0xb1, 0x4f, 0x2d, 0x5f, 0xba, 0x80, 0xbb, 0xfd, 0x9a, 0xad,
0x19, 0x36, 0xe5, 0xad, 0x87, 0xf1, 0x10, 0xc0, 0x8d, 0xc6, 0x50, 0x40, 0x52, 0xf8, 0xb3, 0x98,
0x2c, 0xd6, 0xec, 0x59, 0xe7, 0x0d, 0x3e, 0x0f, 0x93, 0x3e, 0x1d, 0x02, 0x7a, 0x18, 0x8f, 0xb6,
0xc7, 0x46, 0x4e, 0x01, 0xa3, 0x96, 0xdc, 0x3a, 0x20, 0x77, 0xbf, 0x2c, 0x24, 0xe4, 0x80, 0xa9,
0x20, 0x14, 0xe5, 0x2d, 0xb5, 0x68, 0xc9, 0x55, 0x89, 0x23, 0x96, 0x82, 0xaa, 0xba, 0x58, 0xa6,
0x03, 0x38, 0x71, 0x4b, 0x29, 0xd2, 0x47, 0x80, 0xe3, 0x84, 0x91, 0xf4, 0x78, 0x43, 0x64, 0x41,
0x7b, 0x73, 0x99, 0x80, 0x42, 0x48, 0x00, 0xde, 0x00, 0x12, 0x88, 0x80, 0xdb, 0x51, 0x4a, 0x49,
0x84, 0x43, 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, 0xc4, 0x50,
0x13, 0x0e, 0x33, 0x5d, 0x67, 0x7d, 0x01, 0x1f, 0xdb, 0xc0, 0x7f, 0xed, 0x87, 0x7f, 0xbc, 0x0f,
0x75, 0xe0, 0xa5, 0xba, 0xc0, 0x84, 0x3d, 0x24, 0x04, 0xe0, 0xf1, 0x16, 0x41, 0x3b, 0x74, 0xd2,
0x52, 0xc5, 0xf8, 0x7c, 0x12, 0xfb, 0xe4, 0x37, 0x5b, 0xfb, 0x57, 0x11, 0xa1, 0x18, 0x00, 0x00,
};
using (var stream = new MemoryStream(bytes))
using (var gzip = new GZipStream(stream, CompressionMode.Decompress))
using (var reader = new BinaryReader(gzip))
{
while (true)
{
char ch = reader.ReadChar();
if (ch == '\0')
break;
int width = reader.ReadByte();
int height = reader.ReadByte();
bool[,] map = new bool[width, height];
for (int i = 0; i < width; i )
{
for (int j = 0; j < height; j )
{
map[i, j] = reader.ReadBoolean();
}
}
//words_.Add(new CharInfo(ch, map));
}
}
ReadTemplate(templatePath);
}
/// <summary>
/// 读取字符的模板
/// </summary>
/// <param name="fileName"></param>
public void ReadTemplate(string folderPath)
{
DirectoryInfo dir = new DirectoryInfo(folderPath);
foreach (FileInfo f in dir.GetFiles())
{
Bitmap img = new Bitmap(f.FullName);
var table = ToTable(img);
int width = img.Width;
int height = img.Height;
string fileName = f.FullName.Substring(f.FullName.LastIndexOf('\\') 1);
char ch = fileName.ToCharArray()[0];
words_.Add(new CharInfo(ch, table));
img.Dispose();
}
}
public string Read(Bitmap bmp)
{
var result = string.Empty;
var width = bmp.Width;
var height = bmp.Height;
var table = ToTable(bmp);
var next = SearchNext(table, -1);
while (next < width - 7)
{
var matched = Match(table, next);
//完成了一次最佳匹配,可以把匹配位置向右推进一大步,若找不到合适的最佳匹配就向右推进一小步。
if (matched.Rate >= _OcrRate)
{
result = matched.Char;
next = matched.X 14;
//目前,提示语句只需识别第一个字符
if (_OcrType == enumOcrType.OcrTip)
{
return result;
}
}
else
{
next = 1;
}
}
return result;
}
/// <summary>
/// Bitmap做2值化
/// </summary>
/// <param name="bmp"></param>
/// <returns></returns>
private bool[,] ToTable(Bitmap bmp)
{
var table = new bool[bmp.Width, bmp.Height];
for (int i = 0; i < bmp.Width; i )
{
for (int j = 0; j < bmp.Height; j )
{
var color = bmp.GetPixel(i, j);
table[i, j] = (color.R color.G color.B < 600);
}
}
return table;
}
/// <summary>
/// 逐列扫描,找到有值的第一列
/// </summary>
/// <param name="table"></param>
/// <param name="start"></param>
/// <returns></returns>
private int SearchNext(bool[,] table, int start)
{
var width = table.GetLength(0);
var height = table.GetLength(1);
for (start ; start < width; start )
for (int j = 0; j < height; j )
if (table[start, j])
return start;
return start;
}
/// <summary>
/// 遍历匹配字库,取匹配率最高的
/// </summary>
/// <param name="source"></param>
/// <param name="start"></param>
/// <returns></returns>
private MatchedChar Match(bool[,] source, int start)
{
MatchedChar best = null;
foreach (var info in words_)
{
var matched = ScopeMatch(source, info.Table, start);
matched.Char = info.Char;
if (best == null || matched.Rate > best.Rate)
best = matched;
}
return best;
}
/// <summary>
/// 扫描缓冲范围内的所有点,作为起始点匹配点,取匹配率最高的
/// </summary>
/// <param name="source"></param>
/// <param name="target"></param>
/// <param name="start"></param>
/// <returns></returns>
private MatchedChar ScopeMatch(bool[,] source, bool[,] target, int start)
{
int targetWidth = target.GetLength(0);
int targetHeight = target.GetLength(1);
int sourceWidth = source.GetLength(0);
int sourceHeight = source.GetLength(1);
double max = 0;
var matched = new MatchedChar();
for (int i = -2; i < 6; i )
{
for (int j = -3; j < sourceHeight - targetHeight 5; j )
{
double rate = FixedMatch(source, target, i start, j);
if (rate > max)
{
max = rate;
matched.X = i start;
matched.Y = j;
matched.Rate = rate;
}
}
}
return matched;
}
/// <summary>
/// 计算,以x0、y0作为原点,匹配的比率
/// </summary>
/// <param name="source"></param>
/// <param name="target"></param>
/// <param name="x0"></param>
/// <param name="y0"></param>
/// <returns></returns>
private double FixedMatch(bool[,] source, bool[,] target, int x0, int y0)
{
double total = 0;
double count = 0;
int targetWidth = target.GetLength(0);
int targetHeight = target.GetLength(1);
int sourceWidth = source.GetLength(0);
int sourceHeight = source.GetLength(1);
int x, y;
//比对每一个像素
for (int i = 0; i < targetWidth; i )
{
x = i x0;
if (x < 0 || x >= sourceWidth)
{
continue; //超出范围,则退出
}
for (int j = 0; j < targetHeight; j )
{
y = j y0;
if (y < 0 || y >= sourceHeight)
{
continue; //超出范围,则退出
}
//目标像素有值时,直接比对
if (target[i, j])
{
total ; //总值
if (source[x, y])
{
count ; //比对上了,加1分
}
else
{
count--; //减1分
}
}
//目标像素无值时,如果源像素有值,也要减分,但只减半
else if (source[x, y])
{
count -= 0.55;
}
}
}
return count / total;
}
/// <summary>
/// 测试压缩解压缩
/// </summary>
public void testCompressDecompress()
{
CharInfo charinfo = words_[0];
char ch = charinfo.Char;
int width = 12;
int height = 14;
bool[,] map = charinfo.Table;
//测试压缩出第一个字符的模板
byte[] buffer = new byte[171];
buffer[0] = (byte)ch;
buffer[1] = (byte)width;
buffer[2] = (byte)height;
int index = 2;
for (int i = 0; i < width; i )
{
for (int j = 0; j < height; j )
{
index ;
if (map[i, j])
{
buffer[index] = 0x01;
}
else
{
buffer[index] = 0x00;
}
}
}
//using (var stream2 = new MemoryStream(buffer))
//using (var gzip2 = new GZipStream(stream2, CompressionMode.Compress))
//压缩
FileStream fstream = new FileStream("compress.txt", FileMode.Create, FileAccess.Write);
GZipStream gstream = new GZipStream(fstream, CompressionMode.Compress);
BinaryWriter swriter = new BinaryWriter(gstream);
swriter.Write(buffer);
swriter.Close();
gstream.Close();
fstream.Close();
//解压
FileStream fstream2 = new FileStream("compress.txt", FileMode.Open, FileAccess.Read);
GZipStream gstream2 = new GZipStream(fstream2, CompressionMode.Decompress);
BinaryReader reader2 = new BinaryReader(gstream2);
byte[] bufferDecompress = new byte[171];
for (int i = 0; i < 171; i )
{
bufferDecompress[i] = (byte)reader2.ReadByte();
}
reader2.Close();
gstream2.Close();
fstream2.Close();
}
}
}
标签: 验证码识别
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论