Skip to content

Commit 6066fb0

Browse files
committed
调整生成验证码的代码以支持linux
1 parent 46ba550 commit 6066fb0

File tree

4 files changed

+103
-1
lines changed

4 files changed

+103
-1
lines changed

.Net6版本/VOL.Core/Utilities/VierificationCode.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Drawing;
44
using System.Drawing.Imaging;
55
using System.IO;
6+
using System.Runtime.Versioning;
67
using System.Text;
78

89
namespace VOL.Core.Utilities
@@ -31,6 +32,9 @@ public static string RandomText()
3132
}
3233
return code;
3334
}
35+
36+
[SupportedOSPlatform("Windows")]
37+
[Obsolete("仅在 Windows 上支持 System.Drawing.Common,具体请参考:https://docs.microsoft.com/zh-cn/dotnet/core/compatibility/core-libraries/6.0/system-drawing-common-windows-only")]
3438
public static string CreateBase64Imgage(string code)
3539
{
3640
Random random = new Random();
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
using System;
2+
using System.IO;
3+
using System.Linq;
4+
using SkiaSharp;
5+
6+
namespace VOL.Core.Utilities;
7+
8+
public static class VierificationCodeHelpers
9+
{ //验证码字体集合
10+
private static readonly string[] fonts = { "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "宋体" };
11+
private static readonly SKColor[] colors = { SKColors.Black, SKColors.Red, SKColors.DarkBlue, SKColors.Green,
12+
SKColors.Orange, SKColors.Brown, SKColors.DarkCyan, SKColors.Purple};
13+
14+
/// <summary>
15+
///
16+
/// </summary>
17+
/// <param name="code"></param>
18+
/// <returns></returns>
19+
public static string CreateBase64Image(string code)
20+
{
21+
var random = new Random();
22+
var info = new SKImageInfo((int)code.Length * 18, 32);
23+
using var bitmap = new SKBitmap(info);
24+
using var canvas = new SKCanvas(bitmap);
25+
26+
canvas.Clear(SKColors.White);
27+
28+
using var pen = new SKPaint();
29+
pen.FakeBoldText = true;
30+
pen.Style = SKPaintStyle.Stroke;
31+
pen.TextSize = 0.9f * info.Width * pen.TextSize / pen.MeasureText(code);
32+
33+
//绘制随机字符
34+
for (int i = 0; i < code.Length; i++)
35+
{
36+
pen.Color = random.GetRandom(colors);//随机颜色索引值
37+
pen.Typeface = SKTypeface.FromFamilyName(random.GetRandom(fonts), 700, 20, SKFontStyleSlant.Italic);//配置字体
38+
var point = new SKPoint()
39+
{
40+
X = i * 12,
41+
Y = info.Height - ((i + 1) % 2 == 0 ? 2 : 4)
42+
};
43+
canvas.DrawText(code.Substring(i, 1), point, pen);//绘制一个验证字符
44+
}
45+
46+
//绘制噪点
47+
var points = Enumerable.Range(0, 100).Select(
48+
_ => new SKPoint(random.Next(bitmap.Width), random.Next(bitmap.Height))
49+
).ToArray();
50+
canvas.DrawPoints(
51+
SKPointMode.Points,
52+
points,
53+
pen);
54+
55+
//绘制贝塞尔线条
56+
for (int i = 0; i < 2; i++)
57+
{
58+
var p1 = new SKPoint(0, random.Next(bitmap.Height));
59+
var p2 = new SKPoint(random.Next(bitmap.Width), random.Next(bitmap.Height));
60+
var p3 = new SKPoint(random.Next(bitmap.Width), random.Next(bitmap.Height));
61+
var p4 = new SKPoint(bitmap.Width, random.Next(bitmap.Height));
62+
63+
var touchPoints = new SKPoint[] { p1, p2, p3, p4 };
64+
65+
using var bPen = new SKPaint();
66+
bPen.Color = random.GetRandom(colors);
67+
bPen.Style = SKPaintStyle.Stroke;
68+
69+
using var path = new SKPath();
70+
path.MoveTo(touchPoints[0]);
71+
path.CubicTo(touchPoints[1], touchPoints[2], touchPoints[3]);
72+
canvas.DrawPath(path, bPen);
73+
}
74+
return bitmap.ToBase64String(SKEncodedImageFormat.Png);
75+
}
76+
77+
public static T GetRandom<T>(this Random random, T[] tArray)
78+
{
79+
if (random == null) random = new Random();
80+
return tArray[random.Next(tArray.Length)];
81+
}
82+
83+
/// <summary>
84+
/// SKBitmap转Base64String
85+
/// </summary>
86+
/// <param name="bitmap"></param>
87+
/// <param name="format"></param>
88+
/// <returns></returns>
89+
public static string ToBase64String(this SKBitmap bitmap, SKEncodedImageFormat format)
90+
{
91+
using var memStream = new MemoryStream();
92+
using var wstream = new SKManagedWStream(memStream);
93+
bitmap.Encode(wstream, format, 32);
94+
memStream.TryGetBuffer(out ArraySegment<byte> buffer);
95+
return $"{Convert.ToBase64String(buffer.Array, 0, (int)memStream.Length)}";
96+
}
97+
}

.Net6版本/VOL.Core/VOL.Core.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="6.0.0" />
5555
<!--<PackageReference Include="MySql.Data" Version="8.0.13" />-->
5656
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="6.0.0" />
57+
<PackageReference Include="SkiaSharp.NativeAssets.Linux" Version="2.88.0" />
5758
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.17.0" />
5859
<PackageReference Include="ZKWeb.System.Drawing" Version="4.0.1" />
5960
</ItemGroup>

.Net6版本/VOL.WebApi/Controllers/System/Partial/Sys_UserController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ public IActionResult GetVierificationCode()
169169
string code = VierificationCode.RandomText();
170170
var data = new
171171
{
172-
img = VierificationCode.CreateBase64Imgage(code),
172+
img = VierificationCodeHelpers.CreateBase64Image(code),
173173
uuid = Guid.NewGuid()
174174
};
175175
HttpContext.GetService<IMemoryCache>().Set(data.uuid.ToString(), code, new TimeSpan(0, 5, 0));

0 commit comments

Comments
 (0)