一、导入dll库
二、新建一个C#脚本文件
VerificationCode.cs
using System;
using System.Drawing;
using System.Security.Cryptography;
using System.Drawing.Imaging;
using Random = System.Random;
using UnityEngine;
using System.IO;
/// <summary>
/// 用于创建验证码图片
/// 暂时仅支持4位数 如有其它需求 需要进一步修改
/// </summary>
public class VerificationCode {
private string text = "";
private Bitmap image;
private int letterWidth = 30; //单个字体的宽度范围
private int letterHeight = 60; //单个字体的高度范围
private static byte[] randb = new byte[4];
private static RNGCryptoServiceProvider rand = new RNGCryptoServiceProvider();
/// <summary>
/// 字体样式 信息
/// </summary>
private System.Drawing.Font[] fonts = {
new System.Drawing.Font(new FontFamily("Times New Roman"),30 +Next(1),System.Drawing.FontStyle.Regular),
new System.Drawing.Font(new FontFamily("Georgia"), 25 + Next(1),System.Drawing.FontStyle.Regular),
new System.Drawing.Font(new FontFamily("Arial"), 35 + Next(1),System.Drawing.FontStyle.Regular),
new System.Drawing.Font(new FontFamily("Comic Sans MS"), 28 + Next(1),System.Drawing.FontStyle.Regular)
};
/// <summary>
/// 验证码
/// </summary>
public string Text
{
get { return this.text; }
}
/// <summary>
/// 验证码图片
/// </summary>
public System.Drawing.Bitmap Image
{
get { return this.image; }
}
public VerificationCode(int imgWidth, int imgHeight, int length)
{
Number(length, false);
CreateImage(imgWidth, imgHeight);
}
/// <summary>
/// 生成随机数字
/// </summary>
/// <param name="Length">生成长度</param>
/// <param name="Sleep">是否要在生成前将当前线程阻止以避免重复</param>
private string Number(int Length, bool Sleep)
{
if (Sleep) System.Threading.Thread.Sleep(3);
string result = "";
System.Random random = new System.Random();
for (int i = 0; i < Length; i++)
{
result += random.Next(10).ToString();
}
this.text = result;
//Debug.Log("result: " + result);
return result;
}
/// <summary>
/// 获得下一个随机数
/// </summary>
/// <param name="max">最大值</param>
private static int Next(int max)
{
rand.GetBytes(randb);
int value = BitConverter.ToInt32(randb, 0);
value = value % (max + 1);
if (value < 0) value = -value;
return value;
}
/// <summary>
/// 获得下一个随机数
/// </summary>
/// <param name="min">最小值</param>
/// <param name="max">最大值</param>
private static int Next(int min, int max)
{
int value = Next(max - min) + min;
return value;
}
/// <summary>
/// 绘制验证码
/// </summary>
private void CreateImage(int imgWidth, int imgHeight)
{
image = new Bitmap(imgWidth, imgHeight);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);
//画白色背景
g.Clear(System.Drawing.Color.White);
//画三条 扰乱视野的线
for (int i = 0; i < 2; i++)
{
int x1 = Next(imgWidth / 2);
int x2 = Next(imgWidth/2, imgWidth - 1);
int y1 = Next(imgHeight / 2);
int y2 = Next(imgHeight / 2, imgHeight - 1);
g.DrawLine(new Pen(GetRandomColor()), x1, y1, x2, y2);
}
int _x = 0, _y = -30;
for (int int_index = 0; int_index < this.text.Length; int_index++)
{
//随机字符的左边位置
_x += Next(10, 30); //x坐标 累加
_y = Next(5, 10);//y坐标 在一个范围内 随机
string str_char = this.text.Substring(int_index, 1);
str_char = Next(1) == 1 ? str_char.ToLower() : str_char.ToUpper();
Brush newBrush = new SolidBrush(GetRandomColor());
Point thePos = new Point(_x, _y);
Debug.Log("index: "+int_index+" x: "+_x+" y: "+_y);
g.DrawString(str_char, fonts[Next(fonts.Length - 1)], newBrush, thePos);
}
for (int i = 0; i < 10; i++)
{
int x = Next(image.Width - 1);
int y = Next(image.Height - 1);
image.SetPixel(x, y, System.Drawing.Color.FromArgb(Next(0, 255), Next(0, 255), Next(0, 255)));
}
//image = TwistImage(image, true, Next(1, 3), Next(4, 6));
//g.DrawRectangle(new Pen(System.Drawing.Color.LightGray, 5), 0, 0, 150 - 1, (letterHeight - 1));
}
/// <summary>
/// 字体随机颜色
/// </summary>
public System.Drawing.Color GetRandomColor()
{
System.Random RandomNum_First = new System.Random((int)DateTime.Now.Ticks);
System.Threading.Thread.Sleep(RandomNum_First.Next(50));
System.Random RandomNum_Sencond = new System.Random((int)DateTime.Now.Ticks);
int int_Red = RandomNum_First.Next(180);
int int_Green = RandomNum_Sencond.Next(180);
int int_Blue = (int_Red + int_Green > 300) ? 0 : 400 - int_Red - int_Green;
int_Blue = (int_Blue > 255) ? 255 : int_Blue;
return System.Drawing.Color.FromArgb(int_Red, int_Green, int_Blue);
}
/// <summary>
/// 将Image 转为 Texture
/// </summary>
/// <param name="im"></param>
/// <returns></returns>
public static Texture2D Image2Texture(System.Drawing.Image im)
{
if (im == null)
{
return new Texture2D(4, 4);
}
//Memory stream to store the bitmap data.
MemoryStream ms = new MemoryStream();
//Save to that memory stream.
im.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
//Go to the beginning of the memory stream.
ms.Seek(0, SeekOrigin.Begin);
//make a new Texture2D
Texture2D tex = new Texture2D(im.Width, im.Height);
tex.LoadImage(ms.ToArray());
//Close the stream.
ms.Close();
im.Dispose();
ms = null;
//
return tex;
}
/*产生验证码*/
public string CreateCode(int codeLength)
{
string so = "1,2,3,4,5,6,7,8,9,0,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
string[] strArr = so.Split(',');
string code = "";
Random rand = new Random();
for (int i = 0; i < codeLength; i++)
{
code += strArr[rand.Next(0, strArr.Length)];
}
return code;
}
}
三、调用
/// <summary>
/// 显示图片验证码
/// </summary>
/// <param name="img">显示验证码的image组件</param>
/// <param name="imgWidth">图片宽度,150</param>
/// <param name="imgHeight">图片高度, 60</param>
/// <param name="length">字符长度,一般为4</param>
/// <returns>验证码字符串</returns>
private string ShowVerificationCode(Image img, int imgWidth, int imgHeight, int length)
{
VerificationCode vCode = new VerificationCode(imgWidth, imgHeight, length);
Texture2D texture = VerificationCode.Image2Texture(vCode.Image);
img.sprite = Sprite.Create(texture, new Rect(0, 0, imgWidth, imgHeight), new Vector2(0.5f, 0.5f), 125);
return vCode.Text;
}
四、运行结果
五、如果你发现图片大小和你想要的不一样,请继续看
1.通过构造函数传图片的宽,高,验证码的长度;
构造函数2.我使用的宽高比为150*60,字符长度为4
如果不适合你要的size,你可能需要修改以下几个地方: 字符长宽&字体大小 字符的起始坐标 生成制定长度随机数字: 用于生成数字验证码 生成指定长度,数字字母随机用于生成数字字母验证码 Image转Texture2D代码:
Bitmap2Texture2D
网友评论