.Net WebAPI生成可跨平台图片二维码

2 篇文章 0 订阅
1 篇文章 0 订阅
  1. 安装nuget SkiaSharp
  2. 创建 VerifyCodeHelper帮助类
public class VerifyCodeHelper
 {
        /// <summary>
        /// 获取图像数字验证码
        /// </summary>
        /// <param name="text">验证码内容,如4为数字</param>
        /// <returns></returns>
        public static byte[] GetVerifyCode(string text)
        {

            int width = 74;
            int height = 36;

            Random random = new();
            //创建bitmap位图
            using SKBitmap image = new(width, height, SKColorType.Bgra8888, SKAlphaType.Premul);
            //创建画笔
            using SKCanvas canvas = new(image);
            //填充背景颜色为白色
            canvas.DrawColor(SKColors.White);
            //颜色列表
            SKColor[] colors = { SKColors.Black, SKColors.Red, SKColors.Blue, SKColors.Green, SKColors.Orange, SKColors.Brown, SKColors.DarkBlue };
            //画图片的背景噪音线
            for (int i = 0; i < 20; i++)
            {
                using SKPaint drawStyle = new();
                drawStyle.Color = colors[random.Next(colors.Length)];
                canvas.DrawLine(random.Next(0, width), random.Next(0, height), random.Next(0, width), random.Next(0, height), drawStyle);
            }
            //将文字写到画布上
            using (SKPaint drawStyle = new())
            {
                drawStyle.TextSize = height;
                drawStyle.StrokeWidth = 1;
                float emHeight = height - (float)height * (float)0.14;
                float emWidth = ((float)width / text.Length) - ((float)width * (float)0.13) + 5;
                for (int i = 0; i < text.Length; i++)
                {
                    drawStyle.Color = colors[random.Next(colors.Length)];

                    var jg = (width - (emWidth * text.Length)) / text.Length - 1;
                    //画在画板上
                    canvas.DrawText(text[i].ToString(),  emWidth * i + jg, emHeight, drawStyle);
                }

            }

            //画图片的前景噪音点
            for (int i = 0; i < 100; i++)
            {
                image.SetPixel(random.Next(0, width), random.Next(0, height), colors[random.Next(colors.Length)]);
            }

            using var img = SKImage.FromBitmap(image);
            using SKData p = img.Encode(SKEncodedImageFormat.Png, 100);
            return p.ToArray();
        }

    }
  1. 控制器调用
		[HttpGet("ObtainImageVerificationCode")]
        [CustomAnonymousFiler]
        public IActionResult ObtainImageVerificationCode()
        {
            //var images = ImageVerificationCode.CreateVerifyCode(4, VerifyCodeType.NUM);
            Random rad = new Random(); //实例化随机数产生器rad;
            int value = rad.Next(1000, 10000);
            var arr = VerifyCodeHelper.GetVerifyCode(value.ToString());
            return Ok(new { Id = key, Image = "data:image/png;base64," + Convert.ToBase64String(arr) });
        }
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值