开源 企业版 高校版 私有云 模力方舟 AI 队友
代码拉取完成,页面将自动刷新
捐赠
捐赠前请先登录
扫描微信二维码支付
取消
支付完成
支付提示
将跳转至支付宝完成支付
确定
取消
2 Star 7 Fork 5

DebugST/ DotNet_Captcha_VerifyReader

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
已有帐号? 立即登录
文件
main
分支 (1)
标签 (1)
main
2.0
main
分支 (1)
标签 (1)
main
2.0
克隆/下载
克隆/下载
提示
下载代码请复制以下命令到终端执行
为确保你提交的代码身份被 Gitee 正确识别,请执行以下命令完成配置
初次使用 SSH 协议进行代码克隆、推送等操作时,需按下述提示完成 SSH 配置
1 生成 RSA 密钥
2 获取 RSA 公钥内容,并配置到 SSH公钥
在 Gitee 上使用 SVN,请访问 使用指南
使用 HTTPS 协议时,命令行会出现如下账号密码验证步骤。基于安全考虑,Gitee 建议 配置并使用私人令牌 替代登录密码进行克隆、推送等操作
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # 私人令牌
main
分支 (1)
标签 (1)
main
2.0
VerifyReader
/
code
/
CodeHelper.cs
VerifyReader
/
code
/
CodeHelper.cs
CodeHelper.cs 19.36 KB
一键复制 编辑 原始数据 按行查看 历史
DebugST 提交于 2021年03月22日 18:18 +08:00 . upload project
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
namespace VerifyReader
{
public class CodeHelper
{
public CodeHelper() { }
/// <summary>
/// 构造函数
/// </summary>
/// <param name="ci">验证码配置信息</param>
public CodeHelper(CodeInfo ci) {
m_ci = ci;
}
private CodeInfo m_ci; //验证码配置信息
private BitmapData m_bmpData; //位图缓存数据
private byte[] m_byColorInfo; //位图RGB值缓存
public void LoadCodeInfo(CodeInfo ci) {
m_ci = ci;
}
public void LoadCodeInfo(string strFileName) {
m_ci = CodeInfo.LoadFromFile(strFileName);
}
/// <summary>
/// 传入验证码图片返回验证码字符
/// </summary>
/// <param name="imgSrc"></param>
/// <returns>验证码字符窜</returns>
public string GetCodeString(Image imgSrc) {
if (m_ci == null) throw new Exception("Please load CodeInfo");
if (m_ci.GetChars().Count == 0) throw new Exception("Can not found font libary");
string strCode = string.Empty;
int nBlackCount = 0, nWhiteCount = 0;
Image img_dark = null;
List<Rectangle> lstChar = null;
if (m_ci.IsAutoSelectRect) m_ci.CodeCount = m_ci.CodeRectangles.Count;
for (int i = 0, len = m_ci.CodeCount == 0 ? 1 : m_ci.BinaryValues.Length; i < len; i++) {
img_dark = this.GetBinaryImage(imgSrc, m_ci.BinaryValues[i], out nBlackCount, out nWhiteCount, m_ci.Express);
if (nBlackCount > nWhiteCount) img_dark = this.ReverseColors(img_dark);
if (m_ci.RectangleCut.Size != img_dark.Size) {
Bitmap bmp = new Bitmap(m_ci.RectangleCut.Width, m_ci.RectangleCut.Height);
using (Graphics g = Graphics.FromImage(bmp)) {
g.DrawImage(img_dark, new Rectangle(Point.Empty, bmp.Size), m_ci.RectangleCut, GraphicsUnit.Pixel);
img_dark = bmp;
}
}
lstChar = this.GetCharRect((Bitmap)img_dark, m_ci.PixelMin, m_ci.PixelMax);
if (lstChar.Count == m_ci.CodeCount) break;
}
if (!m_ci.IsAutoSelectRect)
lstChar = m_ci.CodeRectangles;
foreach (var v in lstChar) {
strCode += this.GetBestSameChar((Bitmap)img_dark, v);
}
return strCode.Replace("-", "");
}
/// <summary>
/// 获取图像的二值化图像(不会操作原图)
/// </summary>
/// <param name="imgSrc">原始图像</param>
/// <param name="byValue">二值化阈值</param>
/// <param name="nBlackCount">返回二值化后黑点个数</param>
/// <param name="nWhiteCount">返回二值化后白点个数</param>
/// <param name="lstExp">条件表达式</param>
/// <returns>二值化后的图像</returns>
public Image GetBinaryImage(Image imgSrc, byte byValue, out int nBlackCount, out int nWhiteCount, List<string> lstExp = null) {
nBlackCount = 0;
nWhiteCount = 0;
Bitmap b = new Bitmap(imgSrc);
Bitmap bmp = b.Clone(new Rectangle(0, 0, imgSrc.Width, imgSrc.Height), PixelFormat.Format24bppRgb);
b.Dispose();
BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, bmp.PixelFormat);
byte[] byColorInfo = new byte[bmp.Height * bmpData.Stride];
Marshal.Copy(bmpData.Scan0, byColorInfo, 0, byColorInfo.Length);
byte byR, byG, byB;
for (int x = 0, xLen = bmp.Width; x < xLen; x++) {
for (int y = 0, yLen = bmp.Height; y < yLen; y++) {
byB = byColorInfo[y * bmpData.Stride + x * 3];
byG = byColorInfo[y * bmpData.Stride + x * 3 + 1];
byR = byColorInfo[y * bmpData.Stride + x * 3 + 2];
byte byV = GetAvg(byR, byG, byB);
if (byR == 0 && byG == 0) {
int a = 0;
a++;
}
if (lstExp != null) {
foreach (var v in lstExp) {
string[] strTemps = v.Split('-');
if (this.ExecExpress(strTemps[1]
.Replace("R", byR.ToString())
.Replace("G", byG.ToString())
.Replace("B", byB.ToString())
.Replace("V", byV.ToString()))) {
byV = strTemps[0] == "W" ? byV = 255 : byV = 0;
break;
}
}
}
if (byV >= byValue) {
byV = 255;
nWhiteCount++;
} else {
byV = 0;
nBlackCount++;
}
byColorInfo[y * bmpData.Stride + x * 3] =
byColorInfo[y * bmpData.Stride + x * 3 + 1] =
byColorInfo[y * bmpData.Stride + x * 3 + 2] = byV;
}
}
Marshal.Copy(byColorInfo, 0, bmpData.Scan0, byColorInfo.Length);
bmp.UnlockBits(bmpData);
return bmp;
}
/// <summary>
/// 对图像进行反射图里(操作原图)
/// </summary>
/// <param name="imgSrc"></param>
/// <returns>反色后图像</returns>
public Image ReverseColors(Image imgSrc) {
Bitmap bmp = (Bitmap)imgSrc;
BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
byte[] byColorInfo = new byte[bmp.Height * bmpData.Stride];
Marshal.Copy(bmpData.Scan0, byColorInfo, 0, byColorInfo.Length);
for (int x = 0, xLen = bmp.Width; x < xLen; x++) {
for (int y = 0, yLen = bmp.Height; y < yLen; y++) {
byColorInfo[y * bmpData.Stride + x * 3] = (byte)(255 - byColorInfo[y * bmpData.Stride + x * 3]);
byColorInfo[y * bmpData.Stride + x * 3 + 1] = (byte)(255 - byColorInfo[y * bmpData.Stride + x * 3 + 1]);
byColorInfo[y * bmpData.Stride + x * 3 + 2] = (byte)(255 - byColorInfo[y * bmpData.Stride + x * 3 + 2]);
}
}
Marshal.Copy(byColorInfo, 0, bmpData.Scan0, byColorInfo.Length);
bmp.UnlockBits(bmpData);
return bmp;
}
/// <summary>
/// 获取验证码字符所在的区域
/// </summary>
/// <param name="imgDark">二值化后的图像</param>
/// <param name="nPixelMin">连续的最小像素个数 小于此数将被忽略</param>
/// <param name="nPixelMax">连续的最大像素个数 大于此数将被忽略</param>
/// <returns></returns>
public List<Rectangle> GetCharRect(Image imgDark, int nPixelMin, int nPixelMax) {
Bitmap bmp = (Bitmap)imgDark;
m_bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
m_byColorInfo = new byte[bmp.Height * m_bmpData.Stride];
Marshal.Copy(m_bmpData.Scan0, m_byColorInfo, 0, m_byColorInfo.Length);
List<Rectangle> lstRect = new List<Rectangle>();
for (int y = 0, leny = bmp.Height; y < leny; y++) {
for (int x = 0, lenx = bmp.Width; x < lenx; x++) {
if (this.GetArgbFormByColor(x, y) == Color.Black.ToArgb()) {
Rectangle rectTemp = this.GetRectFromPoint(nPixelMin, nPixelMax, new Point(x, y), Color.Black, Color.Blue);
if (rectTemp != Rectangle.Empty) lstRect.Add(rectTemp);
}
}
}
Marshal.Copy(m_byColorInfo, 0, m_bmpData.Scan0, m_byColorInfo.Length);
bmp.UnlockBits(m_bmpData);
//将区域按照left属性排序
for (int i = 0; i < lstRect.Count; i++) {
for (int j = 1; j < lstRect.Count - i; j++) {
if (lstRect[j - 1].Left > lstRect[j].Left) {
Rectangle rectTemp = lstRect[j];
lstRect[j] = lstRect[j - 1];
lstRect[j - 1] = rectTemp;
}
}
}
return lstRect;
}
/// <summary>
/// 更具起始坐标点获取该点的联通区域
/// </summary>
/// <param name="nPixelMin">连续的最小像素个数 小于此数将被忽略</param>
/// <param name="nPixelMax">连续的最大像素个数 大于此数将被忽略</param>
/// <param name="ptStart">起始点位置</param>
/// <param name="clrSrc">点的颜色</param>
/// <param name="clrSet">标记颜色</param>
/// <returns>目标点的连通区域</returns>
private Rectangle GetRectFromPoint(int nPixelMin, int nPixelMax, Point ptStart, Color clrSrc, Color clrSet) {
int nCount = 0;
int nArgb = clrSrc.ToArgb();
Rectangle rect = new Rectangle(ptStart.X, ptStart.Y, 0, 0);
List<Point> ptRegList = new List<Point>();
List<Point> lstTemp = new List<Point>();
ptRegList.Add(ptStart);
lstTemp.Add(ptStart);
this.SetColorFormByColorInfo(ptStart.X, ptStart.Y, clrSet);
while (ptRegList.Count != 0) {
Point ptTemp = this.GetNextPoint(ptRegList[ptRegList.Count - 1], nArgb);
//Point ptTemp = ptRegList[ptRegList.Count - 1];
if (ptTemp != Point.Empty) {
ptRegList.Add(ptTemp);
lstTemp.Add(ptTemp);
this.SetColorFormByColorInfo(ptTemp.X, ptTemp.Y, clrSet);
nCount++;
if (ptTemp.X < rect.Left) { rect.Width = rect.Right - ptTemp.X; rect.X = ptTemp.X; }
if (ptTemp.Y < rect.Top) { rect.Height = rect.Bottom - ptTemp.Y; rect.Y = ptTemp.Y; }
if (ptTemp.X > rect.Right) rect.Width = ptTemp.X - rect.Left;
if (ptTemp.Y > rect.Bottom) rect.Height = ptTemp.Y - rect.Top;
} else
ptRegList.RemoveAt(ptRegList.Count - 1);
}
rect.Width += 1; rect.Height += 1;
if (nCount < nPixelMin || nCount > nPixelMax) {
foreach (var v in lstTemp) {
this.SetColorFormByColorInfo(v.X, v.Y, Color.White);
}
return Rectangle.Empty;
}
return rect;
}
/// <summary>
/// 获取下一个连通像素点
/// </summary>
/// <param name="ptStart">当前位置</param>
/// <param name="nArgb">当前像素颜色</param>
/// <returns>下一个点的坐标</returns>
private Point GetNextPoint(Point ptStart, int nArgb) {
if (m_bmpData.Height > ptStart.Y + 1 && this.GetArgbFormByColor(ptStart.X, ptStart.Y + 1) == nArgb)
return new Point(ptStart.X, ptStart.Y + 1);
if (m_bmpData.Width > ptStart.X + 1 && this.GetArgbFormByColor(ptStart.X + 1, ptStart.Y) == nArgb)
return new Point(ptStart.X + 1, ptStart.Y);
if (0 <= ptStart.Y - 1 && this.GetArgbFormByColor(ptStart.X, ptStart.Y - 1) == nArgb)
return new Point(ptStart.X, ptStart.Y - 1);
if (0 <= ptStart.X - 1 && this.GetArgbFormByColor(ptStart.X - 1, ptStart.Y) == nArgb)
return new Point(ptStart.X - 1, ptStart.Y);
if (0 <= ptStart.X - 1 && m_bmpData.Height > ptStart.Y + 1 && this.GetArgbFormByColor(ptStart.X - 1, ptStart.Y + 1) == nArgb)
return new Point(ptStart.X - 1, ptStart.Y + 1);
if (m_bmpData.Width > ptStart.X + 1 && m_bmpData.Height > ptStart.Y + 1 && this.GetArgbFormByColor(ptStart.X + 1, ptStart.Y + 1) == nArgb)
return new Point(ptStart.X + 1, ptStart.Y + 1);
if (m_bmpData.Width > ptStart.X + 1 && 0 <= ptStart.Y - 1 && this.GetArgbFormByColor(ptStart.X + 1, ptStart.Y - 1) == nArgb)
return new Point(ptStart.X + 1, ptStart.Y - 1);
if (0 <= ptStart.X - 1 && 0 <= ptStart.Y - 1 && this.GetArgbFormByColor(ptStart.X - 1, ptStart.Y - 1) == nArgb)
return new Point(ptStart.X - 1, ptStart.Y - 1);
return Point.Empty;
}
/// <summary>
/// 获取r g b平均值
/// </summary>
/// <param name="b">blue</param>
/// <param name="g">green</param>
/// <param name="r">red</param>
/// <returns>平均值</returns>
private byte GetAvg(byte b, byte g, byte r) {
return (byte)((r + g + b) / 3);
}
/// <summary>
/// 从缓存中获取图像指定坐标的ARGB值
/// </summary>
/// <param name="x">x坐标</param>
/// <param name="y">y坐标</param>
/// <returns>ARGB值</returns>
private int GetArgbFormByColor(int x, int y) {
return Color.FromArgb(255,
m_byColorInfo[y * m_bmpData.Stride + x * 3],
m_byColorInfo[y * m_bmpData.Stride + x * 3 + 1],
m_byColorInfo[y * m_bmpData.Stride + x * 3 + 2]).ToArgb();
}
/// <summary>
/// 从缓存设置图像指定坐标的颜色
/// </summary>
/// <param name="x">x坐标</param>
/// <param name="y">y组表</param>
/// <param name="clr">要设置的颜色</param>
private void SetColorFormByColorInfo(int x, int y, Color clr) {
m_byColorInfo[y * m_bmpData.Stride + x * 3] = clr.B;
m_byColorInfo[y * m_bmpData.Stride + x * 3 + 1] = clr.G;
m_byColorInfo[y * m_bmpData.Stride + x * 3 + 2] = clr.R;
}
/// <summary>
/// 返回文字表达式的执行结果
/// </summary>
/// <param name="strExp">表达式字符串</param>
/// <returns>布尔值</returns>
private bool ExecExpress(string strExp) {
List<bool> lst = new List<bool>();
foreach (Match m in Regex.Matches(strExp, @"(\d+)(.)(\d+)")) {
switch (m.Groups[2].Value) {
case "<": lst.Add(int.Parse(m.Groups[1].Value) < int.Parse(m.Groups[3].Value)); break;
case ">": lst.Add(int.Parse(m.Groups[1].Value) > int.Parse(m.Groups[3].Value)); break;
case "=": lst.Add(m.Groups[1].Value == m.Groups[3].Value); break;
}
}
int index = 0;
foreach (Match m in Regex.Matches(strExp, @"&|\|")) {
switch (m.Value) {
case "|": lst[index + 1] |= lst[index]; break;
case "&": lst[index + 1] &= lst[index]; break;
}
index++;
}
return lst[lst.Count - 1];
}
/// <summary>
/// 从样本中获取最相似的图像并返回字符
/// </summary>
/// <param name="imgDarkCodeImage">验证码图像</param>
/// <param name="rect">字符所在的区域</param>
/// <returns>最相识的字符</returns>
private char GetBestSameChar(Image imgDarkCodeImage, Rectangle rect) {
char chCode = '0';
using (Bitmap bmpChar = new Bitmap(rect.Width, rect.Height)) {
using (Graphics g = Graphics.FromImage(bmpChar)) {
g.DrawImage(imgDarkCodeImage, new Rectangle(0, 0, rect.Width, rect.Height), rect, GraphicsUnit.Pixel);
int nSameCount = 0;
foreach (var v in m_ci.GetChars()) {
int nTemp = this.GetBestSameCount(bmpChar, v);
if (nTemp > nSameCount) { nSameCount = nTemp; chCode = v; }
}
}
}
return chCode;
}
/// <summary>
/// 获取验证码字符图在字符ch的模版中最高重叠率的像素
/// </summary>
/// <param name="imgDark">验证码字符图像</param>
/// <param name="ch"></param>
/// <returns>在ch模版中最高重叠的个数</returns>
private int GetBestSameCount(Image imgDark, char ch) {
List<Image> lstBmpModel = m_ci.GetCodeImages(ch);
if (lstBmpModel == null) return 0;
int nSameCount = 0;
for (int j = 0, lenj = lstBmpModel.Count; j < lenj; j++) {
int nTemp = this.CmpImage(imgDark, lstBmpModel[j]);
if (nTemp > nSameCount) {
nSameCount = nTemp;
}
}
return nSameCount;
}
/// <summary>
/// 将imgB以imgA的大小重叠在一起比对
/// </summary>
/// <param name="imgA">目标图片</param>
/// <param name="imgB">模版图片</param>
/// <returns>非白色像素点的重叠个数</returns>
private int CmpImage(Image imgA, Image imgB) {
int nCount = 0;
using (Bitmap bmpTemp = new Bitmap(imgA.Width, imgA.Height)) {
using (Graphics g = Graphics.FromImage(bmpTemp)) {
g.DrawImage(imgB, 0, 0, imgA.Width, imgA.Height);
BitmapData bmpDataA = ((Bitmap)imgA).LockBits(new Rectangle(0, 0, imgA.Width, imgA.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
byte[] byColorInfoA = new byte[imgA.Height * bmpDataA.Stride];
Marshal.Copy(bmpDataA.Scan0, byColorInfoA, 0, byColorInfoA.Length);
BitmapData bmpDataB = bmpTemp.LockBits(new Rectangle(0, 0, bmpTemp.Width, bmpTemp.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
byte[] byColorInfoB = new byte[bmpTemp.Height * bmpDataB.Stride];
Marshal.Copy(bmpDataB.Scan0, byColorInfoB, 0, byColorInfoB.Length);
for (int x = 0, xLen = imgA.Width; x < xLen; x++) {
for (int y = 0, yLen = imgA.Height; y < yLen; y++) {
byte byA = (byte)(GetAvg(
byColorInfoA[y * bmpDataA.Stride + x * 3],
byColorInfoA[y * bmpDataA.Stride + x * 3 + 1],
byColorInfoA[y * bmpDataA.Stride + x * 3 + 2]) != 255 ? 0 : 255);
byte byB = (byte)(GetAvg(
byColorInfoB[y * bmpDataB.Stride + x * 3],
byColorInfoB[y * bmpDataB.Stride + x * 3 + 1],
byColorInfoB[y * bmpDataB.Stride + x * 3 + 2]) != 255 ? 0 : 255);
if (byA == byB) nCount++;
}
}
Marshal.Copy(byColorInfoA, 0, bmpDataA.Scan0, byColorInfoA.Length);
((Bitmap)imgA).UnlockBits(bmpDataA);
Marshal.Copy(byColorInfoB, 0, bmpDataB.Scan0, byColorInfoB.Length);
bmpTemp.UnlockBits(bmpDataB);
}
}
return nCount;
}
}
}
Loading...
举报
举报成功
我们将于2个工作日内通过站内信反馈结果给你!
请认真填写举报原因,尽可能描述详细。
请选择举报类型
取消
发送
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

简介

[验证码识别] 针对简单类型验证码进行识别的工具套件 其中包含验证码配置工具、识别模块、爆破模块 识别模块代码进行开源 仅两个类文件 其他工具均为对这两个类的进行二次封装 主要是提供一些思路
取消

发行版

暂无发行版

贡献者

全部

语言

近期动态

不能加载更多了
编辑仓库简介
简介内容
主页
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C#
1
https://gitee.com/DebugST/VerifyReader.git
git@gitee.com:DebugST/VerifyReader.git
DebugST
VerifyReader
DotNet_Captcha_VerifyReader
main
点此查找更多帮助

搜索帮助

评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册

AltStyle によって変換されたページ (->オリジナル) /