Content

.NET?SkiaSharp?生成二维码验证码及指定区域截取方法实现(js 二维码识别插件)不看后悔

文章摘要

这篇文章介绍了使用SkiaSharp.QrCode库中的一个名为`ImgHelper`的类,该类包含三个静态方法,用于生成QR码、从图片截取特定区域以及生成数字验证码。具体来说: - `GetQrCode`方法用于生成包含给定文本信息的QR码。它使用QRCodeGenerator创建QR码对象,并将其绘制到一个500x500的白色背景上。最后,它将生成的图像编码为PNG格式并返回。 - `Screenshot`方法用于从指定的图片文件中截取特定区域。它接受源图路径、偏移参数(距上、左的距离)和目标区域的宽度、高度作为输入,并返回截取的图像数据。 - `GetVerifyCode`方法用于生成数字验证码。它创建一个彩色背景,填充白色,并在背景上添加随机线条和点,然后将给定的文本写在特定位置,最后返回编码为PNG格式的图像数据。 这些方法共同实现了生成QR码、截取图像区域以及生成数字验证码的功能,适用于需要自定义图像处理的场景。

using SkiaSharp.QrCode; namespace Common { public class ImgHelper { /// <summary> /// 生成二维码 /// </summary> /// <param name=”text”>二维码内容</param> /// <returns></returns> public static byte[] GetQrCode(string text) { using QRCodeGenerator generator=new(); using var qr=generator.CreateQrCode(text, ECCLevel.L); SKImageInfo info=new(500, 500); using var surface=SKSurface.Create(info); using var canvas=surface.Canvas; canvas.Render(qr, info.Width, info.Height, SKColors.White, SKColors.Black); using var image=surface.Snapshot(); using var data=image.Encode(SKEncodedImageFormat.Png, 100); return data.ToArray(); } /// <summary> /// 从图片截取部分区域 /// </summary> /// <param name=”fromImagePath”>源图路径</param> /// <param name=”offsetX”>距上</param> /// <param name=”offsetY”>距左</param> /// <param name=”width”>宽度</param> /// <param name=”height”>高度</param> /// <returns></returns> public static byte[] Screenshot(string fromImagePath, int offsetX, int offsetY, int width, int height) { using var original=SKBitmap.Decode(fromImagePath); using SKBitmap bitmap=new(width, height); using SKCanvas canvas=new(bitmap); SKRect sourceRect=new(offsetX, offsetY, offsetX + width, offsetY + height); SKRect destRect=new(0, 0, width, height); canvas.DrawBitmap(original, sourceRect, destRect); using var img=SKImage.FromBitmap(bitmap); using SKData p=img.Encode(SKEncodedImageFormat.Png, 100); return p.ToArray(); } /// <summary> /// 获取图像数字验证码 /// </summary> /// <param name=”text”>验证码内容,如4为数字</param> /// <returns></returns> public static byte[] GetVerifyCode(string text) { int width=128; int height=45; Random random=new(); //创建bitmap位图 using SKBitmap image=new(width, height, SKColorType.Bgra8888, SKAlphaType.Premul); //创建画笔 using SKCanvas canvas=new(image); //填充背景颜色为白色 canvas.DrawColor(SKColors.White); //画图片的背景噪音线 for (int i=0; i < (width * height * 0.015); i++) { using SKPaint drawStyle=new(); drawStyle.Color=new(Convert.ToUInt32(random.Next(Int32.MaxValue))); canvas.DrawLine(random.Next(0, width), random.Next(0, height), random.Next(0, width), random.Next(0, height), drawStyle); } //将文字写到画布上 using (SKPaint drawStyle=new()) { drawStyle.Color=SKColors.Red; 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); canvas.DrawText(text, emWidth, emHeight, drawStyle); } //画图片的前景噪音点 for (int i=0; i < (width * height * 0.6); i++) { image.SetPixel(random.Next(0, width), random.Next(0, height), new SKColor(Convert.ToUInt32(random.Next(Int32.MaxValue)))); } using var img=SKImage.FromBitmap(image); using SKData p=img.Encode(SKEncodedImageFormat.Png, 100); return p.ToArray(); } } }