当前位置:   article > 正文

『验证码生成』在Linux中实现基于_sixlabors.imagesharp 使用 linux 验证码

sixlabors.imagesharp 使用 linux 验证码

用来搜索目录中所有的字体信息,并汇总生成fonts.scale文件

yum -y install ttmkfdir

  • 1
  • 2

image.png

4. 查看字体库是否已安装成功

fc-list

  • 1
  • 2

image.png

二、Docker环境

FROM microsoft/dotnet:2.2.0-aspnetcore-runtime
WORKDIR /app
COPY . .
RUN apt-get update -y && apt-get install -y libgdiplus && apt-get clean && ln -s /usr/lib/libgdiplus.so /usr/lib/gdiplus.dll
EXPOSE 80
ENTRYPOINT ["dotnet", "<你的入口程序集>"]

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

三、.NET 代码实现封装

1. SixLabors.ImageSharp方案

1.1 效果

1.jpg

1.2 源码链接

https://github.com/SixLabors/ImageSharp

1.3 Nuget依赖
<!--Nuget依赖-->
<PackageReference Include="SixLabors.ImageSharp" Version="2.1.3" />
<PackageReference Include="SixLabors.ImageSharp.Drawing" Version="1.0.0-beta15" />

  • 1
  • 2
  • 3
  • 4
1.4 Helper代码封装
using SixLabors.ImageSharp;
using SixLabors.Fonts;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
using SixLabors.ImageSharp.Drawing.Processing;
using System;
using System.Linq;

namespace VertifyCode
{
    public class VerifyCodeHelper
    {
        private static readonly Color[] Colors = { Color.Black, Color.Red, Color.Blue, Color.Green, Color.Orange, Color.Brown,
                                                  Color.Brown,Color.DarkBlue};
        private static readonly char[] Chars = { '2','3','4','5','6','8','9',
                                                'A','B','C','D','E','F','G','H','J','K', 'L','M','N','P','R','S','T','W','X','Y' };
        //private static readonly int Width = 90;
        //private static readonly int Height = 35;

        private static string GenCode(int num)
        {
            var code = string.Empty;
            var r = new Random();

            for (int i = 0; i < num; i++)
            {
                code += Chars[r.Next(Chars.Length)].ToString();
            }

            return code;
        }

        public static (string code, byte[] bytes) CreateValidateGraphic(int CodeLength, int Width = 80, int Height = 40, int FontSize = 14)
        {
            var code = GenCode(CodeLength);
            var r = new Random();
            using var image = new Image<Rgba32>(Width, Height);
            // 字体
            var font = SystemFonts.CreateFont(SystemFonts.Families.First().Name, FontSize, FontStyle.Bold);
            image.Mutate(ctx =>
                         {
                             // 白底背景
                             ctx.Fill(Color.White);

                             // 画验证码
                             for (int i = 0; i < code.Length; i++)
                {
                    ctx.DrawText(code[i].ToString()
                                 , font
                                 , Colors[r.Next(Colors.Length)]
                                 , new PointF(20 \* i + 10, r.Next(2, 12)));
                }

                             // 画干扰线
                             for (int i = 0; i < 6; i++)
                {
                    var pen = new Pen(Colors[r.Next(Colors.Length)], 1);
                    var p1 = new PointF(r.Next(Width), r.Next(Height));
                    var p2 = new PointF(r.Next(Width), r.Next(Height));

                    ctx.DrawLines(pen, p1, p2);
                }

                             // 画噪点
                             for (int i = 0; i < 60; i++)
                {
                    var pen = new Pen(Colors[r.Next(Colors.Length)], 1);
                    var p1 = new PointF(r.Next(Width), r.Next(Height));
                    var p2 = new PointF(p1.X + 1f, p1.Y + 1f);

                    ctx.DrawLines(pen, p1, p2);
                }
                         });
            using var ms = new System.IO.MemoryStream();

            // 格式 自定义
            image.SaveAsPng(ms);
            return (code, ms.ToArray());
        }
    }
}


  • 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

2. SkiaSharp方案

2.1 效果

1.png

2.2 源码链接

https://github.com/mono/SkiaSharp

2.3 Nuget依赖
<!--Nuget依赖-->
<PackageReference Include="SkiaSharp" Version="2.88.3" />
<PackageReference Include="SkiaSharp.NativeAssets.Linux.NoDependencies" Version="2.88.3" />

  • 1
  • 2
  • 3
  • 4
2.4 Helper代码封装
using SkiaSharp;
using System;

namespace VertifyCode
{

    public class VerifyCodeImageSharpHelper
    {

        private static readonly char[] Chars = { '2','3','4','5','6','8','9',
       'A','B','C','D','E','F','G','H','J','K', 'L','M','N','P','R','S','T','W','X','Y' };

        private static string GenCode(int num)
        {
            var code = string.Empty;
            var r = new Random();

            for (int i = 0; i < num; i++)
            {
                code += Chars[r.Next(Chars.Length)].ToString();
            }

            return code;
        }

        public static (string code, byte[] bytes) CreateValidateGraphic(int codeLength, int width = 80, int height = 40, int fontSize = 20)
        {
            var code = GenCode(codeLength);

            byte[] imageBytes = null;
            int image2d_x = 0;
            int image2d_y = 0;
            SKRect size;
            int compensateDeepCharacters = 0;
            using (SKPaint drawStyle = CreatePaint(fontSize))
            {
                size = MeasureText(code, drawStyle);


**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化的资料的朋友,可以点击这里获取!](https://bbs.csdn.net/topics/618542503)**

**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**
  • 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
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/715789
推荐阅读
相关标签
  

闽ICP备14008679号