123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268 |
- using Newtonsoft.Json;
- using System;
- using System.IO;
- using System.Threading.Tasks;
- using System.Collections.Generic;
- using SixLabors.ImageSharp;
- using SixLabors.ImageSharp.Drawing.Processing;
- using SixLabors.ImageSharp.Processing;
- using SixLabors.ImageSharp.Formats.Png;
- using SixLabors.ImageSharp.Drawing;
- using SixLabors.ImageSharp.PixelFormats;
- using ZhonTai.Admin.Core.Attributes;
- using ZhonTai.Admin.Tools.Cache;
- namespace ZhonTai.Admin.Tools.Captcha
- {
-
-
-
- [SingleInstance]
- public class SlideJigsawCaptchaTool : ICaptchaTool
- {
- private readonly ICacheTool _cache;
- public SlideJigsawCaptchaTool(ICacheTool cache)
- {
- _cache = cache;
- }
-
-
-
-
-
-
- public int GetRandomInt(int startNum, int endNum)
- {
- return (endNum > startNum ? new Random().Next(endNum - startNum) : 0) + startNum;
- }
-
-
-
-
-
-
-
-
- private PointModel GeneratePoint(int originalWidth, int originalHeight, int templateWidth, int templateHeight)
- {
- Random random = new Random();
- int widthDifference = originalWidth - templateWidth;
- int heightDifference = originalHeight - templateHeight;
- int x;
- if (widthDifference <= 0)
- {
- x = 5;
- }
- else
- {
- x = random.Next(originalWidth - templateWidth - 100) + 100;
- }
- int y;
- if (heightDifference <= 0)
- {
- y = 5;
- }
- else
- {
- y = random.Next(originalHeight - templateHeight - 5) + 5;
- }
- return new PointModel(x, y);
- }
-
-
-
-
-
-
-
-
-
-
- private PointModel GenerateInterferencePoint(int originalWidth, int originalHeight, int templateWidth, int templateHeight, int blockX, int blockY)
- {
- int x;
- if (originalWidth - blockX - 5 > templateWidth * 2)
- {
-
- x = GetRandomInt(blockX + templateWidth + 5, originalWidth - templateWidth);
- }
- else
- {
-
- x = GetRandomInt(100, blockX - templateWidth - 5);
- }
- int y;
- if (originalHeight - blockY - 5 > templateHeight * 2)
- {
-
- y = GetRandomInt(blockY + templateHeight + 5, originalHeight - templateHeight);
- }
- else
- {
-
- y = GetRandomInt(5, blockY - templateHeight - 5);
- }
- return new PointModel(x, y);
- }
- private static ComplexPolygon CalcBlockShape(Image<Rgba32> holeTemplateImage)
- {
- int temp = 0;
- var pathList = new List<IPath>();
- holeTemplateImage.ProcessPixelRows(accessor =>
- {
- for (int y = 0; y < holeTemplateImage.Height; y++)
- {
- var rowSpan = accessor.GetRowSpan(y);
- for (int x = 0; x < rowSpan.Length; x++)
- {
- ref Rgba32 pixel = ref rowSpan[x];
- if (pixel.A != 0)
- {
- if (temp == 0)
- {
- temp = x;
- }
- }
- else
- {
- if (temp != 0)
- {
- pathList.Add(new RectangularPolygon(temp, y, x - temp, 1));
- temp = 0;
- }
- }
- }
- }
- });
- return new ComplexPolygon(new PathCollection(pathList));
- }
-
-
-
-
- public async Task<CaptchaOutput> GetAsync(string captchaKey)
- {
-
-
-
-
-
- using var baseImage = await Image.LoadAsync<Rgba32>($@"{Directory.GetCurrentDirectory()}\wwwroot\captcha\jigsaw\{new Random().Next(1, 4)}.jpg".ToPath());
-
- using var templateImage = await Image.LoadAsync<Rgba32>($@"{Directory.GetCurrentDirectory()}\wwwroot\captcha\jigsaw\templates\{new Random().Next(1, 7)}.png".ToPath());
- int baseWidth = baseImage.Width;
- int baseHeight = baseImage.Height;
- int templateWidth = templateImage.Width;
- int templateHeight = templateImage.Height;
-
- using var blockImage = new Image<Rgba32>(templateWidth, templateHeight);
-
- using var sliderBlockImage = new Image<Rgba32>(templateWidth, baseHeight);
-
- PointModel blockPoint = GeneratePoint(baseWidth, baseHeight, templateWidth, templateHeight);
-
- var blockShape = CalcBlockShape(templateImage);
-
- blockImage.Mutate(x =>
- {
- x.Clip(blockShape, p => p.DrawImage(baseImage, new Point(-blockPoint.X, -blockPoint.Y), 1));
- });
-
-
-
- sliderBlockImage.Mutate(x => x.DrawImage(blockImage, new Point(0, blockPoint.Y), 1));
-
- baseImage.Mutate(x => x.DrawImage(templateImage, new Point(blockPoint.X, blockPoint.Y), (float)0.5));
-
- PointModel interferencePoint = GenerateInterferencePoint(baseWidth, baseHeight, templateWidth, templateHeight, blockPoint.X, blockPoint.Y);
-
- baseImage.Mutate(x => x.DrawImage(templateImage, new Point(interferencePoint.X, interferencePoint.Y), (float)0.5));
- var token = Guid.NewGuid().ToString();
- var captchaData = new CaptchaOutput
- {
- Token = token,
- Data = new SlideJigsawCaptchaDto()
- {
- BaseImage = baseImage.ToBase64String(PngFormat.Instance),
- BlockImage = sliderBlockImage.ToBase64String(PngFormat.Instance)
- }
- };
- var key = string.Format(captchaKey, token);
- await _cache.SetAsync(key, blockPoint.X);
- return captchaData;
- }
-
-
-
-
-
- public async Task<bool> CheckAsync(CaptchaInput input)
- {
- if (input == null || input.Data.IsNull())
- {
- return false;
- }
- var key = string.Format(input.CaptchaKey, input.Token);
- if (await _cache.ExistsAsync(key))
- {
- try
- {
- var point = JsonConvert.DeserializeObject<PointModel>(input.Data);
- var x = await _cache.GetAsync<int>(key);
- if (Math.Abs(x - point.X) < 5)
- {
- if (input.DeleteCache)
- {
- await _cache.DelAsync(key);
- }
- return true;
- }
- else
- {
- await _cache.DelAsync(key);
- return false;
- }
- }
- catch
- {
- await _cache.DelAsync(key);
- return false;
- }
- }
- else
- {
- return false;
- }
- }
- }
- }
|