SlideJigsawCaptcha.cs 13 KB

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