提高防OCR的一种办法

提高防OCR的一种办法

今天因为某件事情,想搞个图片,想防止被OCR。

各种试,但不得不说现在的OCR真的牛啊,变形,加噪,横线,仍然难以阻挡。

后面试了不下百十来种字体,但仍然无效,比如下面的

也试了一种简单办法 ,有点小用,但也没大用

但最后试出一种结果,可以有效的防止OCR(只是目前),拿三家大厂来试一下

腾讯

https://cloud.tencent.com/product/ocr-catalog

百度

https://cloud.baidu.com/product/ocr/general

阿里

https://duguang.aliyun.com/experience?type=universal&subtype=general#intro

这个字体是怎么生成的了,很简单

生成汉字,扭曲,加棱形外框,旋转20度。

不知道哪位民间高手,能够试试?

另外把生成验证码的各种操作C#代码放出来,凑合看,跟本文关系不大

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;

namespace wxEncode

{

public partial class Form1 : Form

{

//private static Color BackColor = Color.White;

private static int Width = 62;

private static int Height = 61;

private Random _random;

//private int _brushNameIndex;

public Form1()

{

InitializeComponent();

}

private void button1_Click(object sender, EventArgs e)

{

string code = "今";

//code = "wwwwsss.coa.domf";

//code = "wwwww.baidu.com";

Width = getstrLen(code);

pictureBox1.Image = GetVCode(code);

Bitmap b= TwistImage(new Bitmap(pictureBox1.Image), true, Convert.ToInt32( numericUpDown1.Value), Convert.ToInt32(numericUpDown2.Value));

Image img = b;

//System.IO.File.Delete(@"d:\x.png");

//b.Save(@"d:\x.png");

pictureBox1.Image = b;

}

public int getstrLen(string code)

{

code = code.Trim();

string d = "`1234567890-=qwertyuiop[]\\asdfghjkl;'zxcvbnm,./~!@#$%^&*()_+QWERTYUIOP{}|ASDFGHJKL:\"ZXCVBNM<>?";

int r = 0;

int e = 0;

int h = 0;

for (int i = 0; i < code.Length; i++)

{

if (d.Contains(code[i]))

{

//r += 15;

e++;

}

else

{

//r += 20;

h++;

}

}

int s1 = 6;

if (e < s1)

r += e * 40;

else

r += e * 28;

if (h < s1)

r += h * 60;

else

r += h * 45;

//if (code.Length<=s1)

// if (e>=2)

// r += (s1+1- e)*10;

return r;

}

public Image GetVCode(string codeStr)

{

_random = new Random();

using (Bitmap img = new Bitmap(Width, Height))

{

// _code = GetRandomCode();

// System.Web.HttpContext.Current.Session["vcode"] = _code;

using (Graphics g = Graphics.FromImage(img))

{

g.Clear(Color.White);//绘画背景颜色

Paint_Text(g, codeStr);// 绘画文字

// g.DrawString(strCode, new Font("微软雅黑", 15), Brushes.Blue, new PointF(5, 2));// 绘画文字

//Paint_TextStain(img);// 绘画噪音点

Paint_line(g);

g.DrawRectangle(Pens.DarkGray, 0, 0, Width - 1, Height - 1);//绘画边框

IntPtr pr = img.GetHbitmap();

Image MyImage = Image.FromHbitmap(pr);

return MyImage;

//using (System.IO.MemoryStream ms = new System.IO.MemoryStream())

//{

// //将图片 保存到内存流中

// img.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);

// //将内存流 里的 数据 转成 byte 数组 返回

// return ms.ToArray();

//}

}

}

}

private void Paint_line(Graphics graph)

{

return;

int nLines = 3;

System.Drawing.Pen pen = new System.Drawing.Pen(GetBrush(), 0.5f);

for (int a = 0; a < nLines; a++)

{

int x1 = _random.Next() % Convert.ToInt32 (Width/4);

int y1 = _random.Next() % Height;

int x2 = _random.Next() % Convert.ToInt32(Width / 4*3);

int y2 = _random.Next() % Height;

graph.DrawLine(pen, x1, y1, x2, y2);

}

}

///

/// 绘画文字

///

///

private void Paint_Text(Graphics g, string code)

{

g.DrawString(code, GetFont(), GetBrush(), 3, 1);

}

///

/// 绘画文字噪音点

///

///

private void Paint_TextStain(Bitmap b)

{

string[] BrushName = new string[] { "OliveDrab",

"ForestGreen",

"DarkCyan",

"LightSlateGray",

"RoyalBlue",

"SlateBlue",

"DarkViolet",

"MediumVioletRed",

"IndianRed",

"Firebrick",

"Chocolate",

"Peru",

" enrod"

};

for (int n = 0; n < Width*Height*0.05; n++)

{

int bi = _random.Next(0, BrushName.Length);

int x = _random.Next(Width);

int y = _random.Next(Height);

b.SetPixel(x, y, Color.FromName(BrushName[bi]));

}

}

///

/// 随机取一个字体

///

///

private Font GetFont()

{

string[] FontItems = new string[]{ "Arial",

"Helvetica",

"Geneva",

"sans-serif",

"Verdana"

};

int fontIndex = _random.Next(0, FontItems.Length);

FontStyle fontStyle = GetFontStyle(_random.Next(0, 2));

Console.WriteLine("font:" + FontItems[fontIndex]);

return new Font(FontItems[4], 25, fontStyle);

}

/**/

/**/

/**/

///

/// 随机取一个笔刷

///

///

private Brush GetBrush()

{

Brush[] BrushItems = new Brush[]{ Brushes.OliveDrab,

Brushes.ForestGreen,

Brushes.DarkCyan,

Brushes.LightSlateGray,

Brushes.RoyalBlue,

Brushes.SlateBlue,

Brushes.DarkViolet,

Brushes.MediumVioletRed,

Brushes.IndianRed,

Brushes.Firebrick,

Brushes.Chocolate,

Brushes.Peru,

Brushes.Goldenrod

};

int brushIndex = _random.Next(0, BrushItems.Length);

Console.WriteLine("brush:" + brushIndex.ToString () +"--" + BrushItems[brushIndex]);

return BrushItems[7];

}

///

/// 绘画背景颜色

///

///

private void Paint_Background(Graphics g)

{

g.Clear(Color.White);

}

/**/

/**/

/**/

///

/// 取一个字体的样式

///

///

///

private FontStyle GetFontStyle(int index)

{

index = 0;

switch (index)

{

case 0:

return FontStyle.Bold;

case 1:

return FontStyle.Italic;

default:

return FontStyle.Regular;

}

}

///

/// 取得一个 4 位的随机码

///

///

public string GetRandomCode()

{

return Guid.NewGuid().ToString().Substring(0, 5);

}

private const double PI = 3.1415926535897932384626433832795;

private const double PI2 = 6.283185307179586476925286766559;

///

/// 正弦曲线Wave扭曲图片

///

/// 图片路径

/// 如果扭曲则选择为True

/// 波形的幅度倍数,越大扭曲的程度越高,一般为3

/// 波形的起始相位,取值区间[0-2*PI)

///

public System.Drawing.Bitmap TwistImage(Bitmap srcBmp, bool bXDir, double dMultValue, double dPhase)

{

System.Drawing.Bitmap destBmp = new Bitmap(srcBmp.Width, srcBmp.Height);

//将位图背景填充为白色

System.Drawing.Graphics graph = System.Drawing.Graphics.FromImage(destBmp);

graph.FillRectangle(new SolidBrush(System.Drawing.Color.White), 0, 0, destBmp.Width, destBmp.Height);

graph.Dispose();

double dBaseAxisLen = bXDir ? (double)destBmp.Height : (double)destBmp.Width;

for (int i = 0; i < destBmp.Width; i++)

{

for (int j = 0; j < destBmp.Height; j++)

{

double dx = 0;

dx = bXDir ? (PI2 * (double)j) / dBaseAxisLen : (PI2 * (double)i) / dBaseAxisLen;

dx += dPhase;

double dy = Math.Sin(dx);

//取得当前点的颜色

int nOldX = 0,nOldY = 0;

nOldX = bXDir ? i + (int)(dy * dMultValue) : i;

nOldY = bXDir ? j : j + (int)(dy * dMultValue);

System.Drawing.Color color = srcBmp.GetPixel(i, j);

if (nOldX >= 0 && nOldX < destBmp.Width

&& nOldY >= 0 && nOldY < destBmp.Height)

{

destBmp.SetPixel(nOldX, nOldY, color);

}

}

}

return destBmp;

}

}

}

View Code

🎈 相关推荐

虚拟机-安装与使用(详细教程)
beat365英国在线体育

虚拟机-安装与使用(详细教程)

📅 07-15 👀 2724
英雄联盟中国赛区为什么叫LPL?
365体育亚洲官方登录

英雄联盟中国赛区为什么叫LPL?

📅 07-16 👀 3889
有对方短号怎么查手机号
beat365网站假的吗

有对方短号怎么查手机号

📅 07-02 👀 4887