//
namespace SafeCampus.Core.Utils;
///
/// 生成验证码功能
///
public static class CaptchaUtil
{
///
/// 获取验证码
///
/// 验证码数
/// 长度
/// 高度
/// 字体大小
/// 类型 0:数字 1:字符 2:计算
///
public static CaptchaInfo CreateCaptcha(CaptchaType type = CaptchaType.CHAR, int length = 4, int width = 170,
int height = 50, int fontSize = 20)
{
//初始化验证码
string charCode;
var resultCode = "";
switch (type.ToString())
{
case "NUM":
charCode = CreateNumCode(length);
break;
case "ARITH":
charCode = CreateArithCode(out resultCode);
break;
default:
charCode = CreateCharCode(length);
break;
}
var imageGenerator = new DefaultCaptchaImageGenerator();
var imageGeneratorOption = new CaptchaImageGeneratorOption
{
// 必须设置
ForegroundColors = DefaultColors.Instance.Colors,
Width = width,
Height = height,
FontSize = fontSize,
TextBold = true,
BubbleCount = 1,
FontFamily = DefaultFontFamilys.Instance.Actionj
};
var bytes = imageGenerator.Generate(charCode, imageGeneratorOption);
var captchaInfo = new CaptchaInfo
{
Code = type.ToString() == "ARITH" ? resultCode : charCode,
Image = bytes
};
return captchaInfo;
}
///
/// 获取数字验证码
///
/// 验证码数
///
public static string CreateNumCode(int n)
{
char[] numChar = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
var charCode = string.Empty;
var random = new Random();
for (var i = 0; i < n; i++)
{
charCode += numChar[random.Next(numChar.Length)];
}
return charCode;
}
///
/// 获取字符验证码
///
/// 验证码数
///
public static string CreateCharCode(int n)
{
char[] strChar =
{
'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', '0', '1', '2', '3',
'4', '5', '6', '7', '8', '9', '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'
};
var charCode = string.Empty;
var random = new Random();
for (var i = 0; i < n; i++)
{
charCode += strChar[random.Next(strChar.Length)];
}
return charCode;
}
///
/// 获取运算符验证码
///
///
public static string CreateArithCode(out string resultCode)
{
string checkCode;
var random = new Random();
var intFirst = random.Next(1, 10);//生成第一个数字
var intSec = random.Next(1, 10);//生成第二个数字
int intTemp;
switch (random.Next(1, 3).ToString())
{
case "2":
if (intFirst < intSec)
{
intTemp = intFirst;
intFirst = intSec;
intSec = intTemp;
}
checkCode = intFirst + "-" + intSec + "=";
resultCode = (intFirst - intSec).ToString();
break;
default:
checkCode = intFirst + "+" + intSec + "=";
resultCode = (intFirst + intSec).ToString();
break;
}
return checkCode;
}
}