SlideJigsawCaptcha.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. using Admin.Core;
  2. using Admin.Core.Common.Attributes;
  3. using Admin.Core.Common.Cache;
  4. using Newtonsoft.Json;
  5. using System;
  6. using System.Drawing;
  7. using System.Drawing.Drawing2D;
  8. using System.Drawing.Imaging;
  9. using System.IO;
  10. using System.Net.Http;
  11. using System.Threading.Tasks;
  12. namespace Admin.Tools.Captcha
  13. {
  14. /// <summary>
  15. /// 滑块拼图验证
  16. /// </summary>
  17. [SingleInstance]
  18. public class SlideJigsawCaptcha : ICaptcha
  19. {
  20. private readonly ICache _cache;
  21. public SlideJigsawCaptcha(ICache cache)
  22. {
  23. _cache = cache;
  24. }
  25. /// <summary>
  26. /// Bitmap转为base64编码的文本
  27. /// </summary>
  28. /// <param name="bmp"></param>
  29. /// <returns></returns>
  30. private string ImgToBase64String(Bitmap bmp)
  31. {
  32. try
  33. {
  34. MemoryStream ms = new MemoryStream();
  35. bmp.Save(ms, ImageFormat.Png);
  36. byte[] arr = new byte[ms.Length];
  37. ms.Position = 0;
  38. ms.Read(arr, 0, (int)ms.Length);
  39. ms.Close();
  40. return Convert.ToBase64String(arr);
  41. }
  42. catch
  43. {
  44. return null;
  45. }
  46. }
  47. /// <summary>
  48. /// 根据模板生成拼图
  49. /// </summary>
  50. /// <param name="baseImage"></param>
  51. /// <param name="templateImage"></param>
  52. /// <param name="x"></param>
  53. /// <param name="y"></param>
  54. /// <returns></returns>
  55. private Bitmap CutByTemplate(Bitmap baseImage, Bitmap templateImage, int x, int y)
  56. {
  57. Bitmap newImage = new Bitmap(templateImage.Width, baseImage.Height, PixelFormat.Format32bppRgb);
  58. newImage.MakeTransparent();
  59. int xLength = templateImage.Width;
  60. int yLength = templateImage.Height;
  61. // 模板图像宽度
  62. for (int i = 0; i < xLength; i++)
  63. {
  64. // 模板图片高度
  65. for (int j = 0; j < yLength; j++)
  66. {
  67. // 如果模板图像当前像素点不是透明色 copy源文件信息到目标图片中
  68. int rgb = templateImage.GetPixel(i, j).ToArgb();
  69. if (rgb < 0)
  70. {
  71. Color oriImageColor = baseImage.GetPixel(x + i, y + j);
  72. newImage.SetPixel(i, y + j, oriImageColor);
  73. //抠图区域半透明
  74. baseImage.SetPixel(x + i, y + j, Color.FromArgb(120, oriImageColor.R, oriImageColor.G, oriImageColor.B));
  75. }
  76. //防止数组越界判断
  77. if (i == (xLength - 1) || j == (yLength - 1))
  78. {
  79. continue;
  80. }
  81. int rightRgb = templateImage.GetPixel(i + 1, j).ToArgb();
  82. int downRgb = templateImage.GetPixel(i, j + 1).ToArgb();
  83. //描边处理,,取带像素和无像素的界点,判断该点是不是临界轮廓点,如果是设置该坐标像素是白色
  84. if ((rgb >= 0 && rightRgb < 0) || (rgb < 0 && rightRgb >= 0) || (rgb >= 0 && downRgb < 0) || (rgb < 0 && downRgb >= 0))
  85. {
  86. newImage.SetPixel(i, y + j, Color.White);
  87. baseImage.SetPixel(x + i, y + j, Color.White);
  88. }
  89. }
  90. }
  91. return newImage;
  92. }
  93. /// <summary>
  94. /// 根据模板生成干扰图
  95. /// </summary>
  96. /// <param name="baseImage"></param>
  97. /// <param name="templateImage"></param>
  98. /// <param name="x"></param>
  99. /// <param name="y"></param>
  100. private void InterferenceByTemplate(Bitmap baseImage, Bitmap templateImage, int x, int y)
  101. {
  102. int xLength = templateImage.Width;
  103. int yLength = templateImage.Height;
  104. // 模板图像宽度
  105. for (int i = 0; i < xLength; i++)
  106. {
  107. // 模板图片高度
  108. for (int j = 0; j < yLength; j++)
  109. {
  110. // 如果模板图像当前像素点不是透明色 copy源文件信息到目标图片中
  111. int rgb = templateImage.GetPixel(i, j).ToArgb();
  112. if (rgb < 0)
  113. {
  114. Color oriImageColor = baseImage.GetPixel(x + i, y + j);
  115. //抠图区域半透明
  116. baseImage.SetPixel(x + i, y + j, Color.FromArgb(120, oriImageColor.R, oriImageColor.G, oriImageColor.B));
  117. }
  118. //防止数组越界判断
  119. if (i == (xLength - 1) || j == (yLength - 1))
  120. {
  121. continue;
  122. }
  123. int rightRgb = templateImage.GetPixel(i + 1, j).ToArgb();
  124. int downRgb = templateImage.GetPixel(i, j + 1).ToArgb();
  125. //描边处理,,取带像素和无像素的界点,判断该点是不是临界轮廓点,如果是设置该坐标像素是白色
  126. if ((rgb >= 0 && rightRgb < 0) || (rgb < 0 && rightRgb >= 0) || (rgb >= 0 && downRgb < 0) || (rgb < 0 && downRgb >= 0))
  127. {
  128. baseImage.SetPixel(x + i, y + j, Color.White);
  129. }
  130. }
  131. }
  132. }
  133. /// <summary>
  134. /// 更改图片尺寸
  135. /// </summary>
  136. /// <param name="bmp"></param>
  137. /// <param name="width"></param>
  138. /// <param name="height"></param>
  139. /// <returns></returns>
  140. private Bitmap ResizeImage(Bitmap bmp, int width, int height)
  141. {
  142. try
  143. {
  144. Bitmap b = new Bitmap(width, height);
  145. Graphics g = Graphics.FromImage(b);
  146. // 图画质量
  147. g.InterpolationMode = InterpolationMode.HighQualityBicubic;
  148. g.DrawImage(bmp, new Rectangle(0, 0, width, height), new Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel);
  149. g.Dispose();
  150. return b;
  151. }
  152. catch
  153. {
  154. return null;
  155. }
  156. }
  157. /// <summary>
  158. /// 随机范围内数字
  159. /// </summary>
  160. /// <param name="startNum"></param>
  161. /// <param name="endNum"></param>
  162. /// <returns></returns>
  163. public int GetRandomInt(int startNum, int endNum)
  164. {
  165. return (endNum > startNum ? new Random().Next(endNum - startNum) : 0) + startNum;
  166. }
  167. /// <summary>
  168. /// 随机生成拼图坐标
  169. /// </summary>
  170. /// <param name="originalWidth"></param>
  171. /// <param name="originalHeight"></param>
  172. /// <param name="templateWidth"></param>
  173. /// <param name="templateHeight"></param>
  174. /// <returns></returns>
  175. private PointModel GeneratePoint(int originalWidth, int originalHeight, int templateWidth, int templateHeight)
  176. {
  177. Random random = new Random();
  178. int widthDifference = originalWidth - templateWidth;
  179. int heightDifference = originalHeight - templateHeight;
  180. int x;
  181. if (widthDifference <= 0)
  182. {
  183. x = 5;
  184. }
  185. else
  186. {
  187. x = random.Next(originalWidth - templateWidth - 100) + 100;
  188. }
  189. int y;
  190. if (heightDifference <= 0)
  191. {
  192. y = 5;
  193. }
  194. else
  195. {
  196. y = random.Next(originalHeight - templateHeight - 5) + 5;
  197. }
  198. return new PointModel(x, y);
  199. }
  200. /// <summary>
  201. /// 随机生成干扰图坐标
  202. /// </summary>
  203. /// <param name="originalWidth"></param>
  204. /// <param name="originalHeight"></param>
  205. /// <param name="templateWidth"></param>
  206. /// <param name="templateHeight"></param>
  207. /// <param name="blockX"></param>
  208. /// <param name="blockY"></param>
  209. /// <returns></returns>
  210. private PointModel GenerateInterferencePoint(int originalWidth, int originalHeight, int templateWidth, int templateHeight, int blockX, int blockY)
  211. {
  212. int x;
  213. if (originalWidth - blockX - 5 > templateWidth * 2)
  214. {
  215. //在原扣图右边插入干扰图
  216. x = GetRandomInt(blockX + templateWidth + 5, originalWidth - templateWidth);
  217. }
  218. else
  219. {
  220. //在原扣图左边插入干扰图
  221. x = GetRandomInt(100, blockX - templateWidth - 5);
  222. }
  223. int y;
  224. if (originalHeight - blockY - 5 > templateHeight * 2)
  225. {
  226. //在原扣图下边插入干扰图
  227. y = GetRandomInt(blockY + templateHeight + 5, originalHeight - templateHeight);
  228. }
  229. else
  230. {
  231. //在原扣图上边插入干扰图
  232. y = GetRandomInt(5, blockY - templateHeight - 5);
  233. }
  234. return new PointModel(x, y);
  235. }
  236. /// <summary>
  237. /// 获得验证数据
  238. /// </summary>
  239. /// <returns>JObject</returns>
  240. public async Task<CaptchaOutput> GetAsync()
  241. {
  242. //获取网络图片
  243. //var client = new HttpClient();
  244. //var stream = await client.GetStreamAsync("https://picsum.photos/310/155");
  245. //client.Dispose();
  246. //Bitmap baseImage = new Bitmap(stream);
  247. //stream.Dispose();
  248. var oriImage = Image.FromFile(Directory.GetCurrentDirectory() + $@"\wwwroot\captcha\jigsaw\{new Random().Next(1, 4)}.jpg");
  249. //更改图片尺寸
  250. //Bitmap baseImage = ResizeImage(oriImage, 310, 155);
  251. Bitmap baseImage = new Bitmap(oriImage);
  252. oriImage.Dispose();
  253. var oriTemplate = Image.FromFile(Directory.GetCurrentDirectory() + $@"\wwwroot\captcha\jigsaw\templates\{new Random().Next(1, 7)}.png");
  254. Bitmap templateImage = new Bitmap(oriTemplate);
  255. oriTemplate.Dispose();
  256. int baseWidth = baseImage.Width;
  257. int baseHeight = baseImage.Height;
  258. int templateWidth = templateImage.Width;
  259. int templateHeight = templateImage.Height;
  260. //随机生成拼图坐标
  261. PointModel point = GeneratePoint(baseWidth, baseHeight, templateWidth, templateHeight);
  262. int x = point.X;
  263. int y = point.Y;
  264. //生成拼图
  265. string blockImageBase64 = "data:image/png;base64," + ImgToBase64String(CutByTemplate(baseImage, templateImage, x, y));
  266. //生成干扰图
  267. PointModel interferencePoint = GenerateInterferencePoint(baseWidth, baseHeight, templateWidth, templateHeight, x, y);
  268. InterferenceByTemplate(baseImage, templateImage, interferencePoint.X, interferencePoint.Y);
  269. string baseImageBase64 = "data:image/png;base64," + ImgToBase64String(baseImage);
  270. templateImage.Dispose();
  271. baseImage.Dispose();
  272. var token = Guid.NewGuid().ToString();
  273. CaptchaOutput captchaData = new CaptchaOutput
  274. {
  275. Token = token,
  276. Data = new SlideJigsawCaptchaModel()
  277. {
  278. BlockImage = blockImageBase64,
  279. BaseImage = baseImageBase64
  280. }
  281. };
  282. var key = string.Format(CacheKey.VerifyCodeKey, token);
  283. await _cache.SetAsync(key, point.X);
  284. return captchaData;
  285. }
  286. /// <summary>
  287. /// 检查验证数据
  288. /// </summary>
  289. /// <param name="input"></param>
  290. /// <returns></returns>
  291. public async Task<bool> CheckAsync(CaptchaInput input)
  292. {
  293. if (input == null || input.Data.IsNull())
  294. {
  295. return false;
  296. }
  297. var key = string.Format(CacheKey.VerifyCodeKey, input.Token);
  298. if (await _cache.ExistsAsync(key))
  299. {
  300. try
  301. {
  302. var point = JsonConvert.DeserializeObject<PointModel>(input.Data);
  303. var x = await _cache.GetAsync<int>(key);
  304. if (Math.Abs(x - point.X) < 5)
  305. {
  306. if (input.DeleteCache)
  307. {
  308. await _cache.DelAsync(key);
  309. }
  310. return true;
  311. }
  312. else
  313. {
  314. await _cache.DelAsync(key);
  315. return false;
  316. }
  317. }
  318. catch
  319. {
  320. await _cache.DelAsync(key);
  321. return false;
  322. }
  323. }
  324. else
  325. {
  326. return false;
  327. }
  328. }
  329. }
  330. }