1
0

SlideJigsawCaptcha.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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="img"></param>
  56. /// <param name="x"></param>
  57. /// <param name="y"></param>
  58. /// <param name="pixels"></param>
  59. private void ReadPixel(Bitmap img, int x, int y, int[] pixels)
  60. {
  61. int xStart = x - 1;
  62. int yStart = y - 1;
  63. int current = 0;
  64. for (int i = xStart; i < 3 + xStart; i++)
  65. {
  66. for (int j = yStart; j < 3 + yStart; j++)
  67. {
  68. int tx = i;
  69. if (tx < 0)
  70. {
  71. tx = -tx;
  72. }
  73. else if (tx >= img.Width)
  74. {
  75. tx = x;
  76. }
  77. int ty = j;
  78. if (ty < 0)
  79. {
  80. ty = -ty;
  81. }
  82. else if (ty >= img.Height)
  83. {
  84. ty = y;
  85. }
  86. pixels[current++] = img.GetPixel(tx, ty).ToArgb();
  87. }
  88. }
  89. }
  90. private void FillMatrix(int[][] matrix, int[] values)
  91. {
  92. int filled = 0;
  93. for (int i = 0; i < matrix.Length; i++)
  94. {
  95. int[] x = matrix[i];
  96. for (int j = 0; j < x.Length; j++)
  97. {
  98. x[j] = values[filled++];
  99. }
  100. }
  101. }
  102. private Color AvgMatrix(int[][] matrix)
  103. {
  104. int r = 0;
  105. int g = 0;
  106. int b = 0;
  107. for (int i = 0; i < matrix.Length; i++)
  108. {
  109. int[] x = matrix[i];
  110. for (int j = 0; j < x.Length; j++)
  111. {
  112. if (j == 1)
  113. {
  114. continue;
  115. }
  116. Color c = Color.FromArgb(x[j]);
  117. r += c.R;
  118. g += c.G;
  119. b += c.B;
  120. }
  121. }
  122. return Color.FromArgb(r / 8, g / 8, b / 8);
  123. }
  124. /// <summary>
  125. /// 根据模板生成拼图
  126. /// </summary>
  127. /// <param name="baseImage"></param>
  128. /// <param name="templateImage"></param>
  129. /// <param name="x"></param>
  130. /// <param name="y"></param>
  131. /// <returns></returns>
  132. private Bitmap CutByTemplate(Bitmap baseImage, Bitmap templateImage, int x, int y)
  133. {
  134. //生成透明背景图
  135. Bitmap newImage = new Bitmap(templateImage.Width, baseImage.Height);
  136. for (int i = 0, newImageWidth = templateImage.Width; i < newImageWidth; i++)
  137. {
  138. for (int j = 0, newImageHeight = baseImage.Height; j < newImageHeight; j++)
  139. {
  140. newImage.SetPixel(i, j, Color.FromArgb(0,0,0,0));
  141. }
  142. }
  143. // 临时数组遍历用于高斯模糊存周边像素值
  144. int[] values = new int[9];
  145. int[][] martrix = { new int[3], new int[3], new int[3] };
  146. int xLength = templateImage.Width;
  147. int yLength = templateImage.Height;
  148. // 模板图像宽度
  149. for (int i = 0; i < xLength; i++)
  150. {
  151. // 模板图片高度
  152. for (int j = 0; j < yLength; j++)
  153. {
  154. // 如果模板图像当前像素点不是透明色 copy源文件信息到目标图片中
  155. int rgb = templateImage.GetPixel(i, j).ToArgb();
  156. if (rgb < 0)
  157. {
  158. Color oriImageColor = baseImage.GetPixel(x + i, y + j);
  159. newImage.SetPixel(i, y + j, oriImageColor);
  160. //抠图区域半透明
  161. //baseImage.SetPixel(x + i, y + j, Color.FromArgb(120, oriImageColor.R, oriImageColor.G, oriImageColor.B));
  162. //抠图区域高斯模糊
  163. ReadPixel(baseImage, x + i, y + j, values);
  164. FillMatrix(martrix, values);
  165. baseImage.SetPixel(x + i, y + j, AvgMatrix(martrix));
  166. }
  167. //防止数组越界判断
  168. if (i == (xLength - 1) || j == (yLength - 1))
  169. {
  170. continue;
  171. }
  172. int rightRgb = templateImage.GetPixel(i + 1, j).ToArgb();
  173. int downRgb = templateImage.GetPixel(i, j + 1).ToArgb();
  174. //描边处理,,取带像素和无像素的界点,判断该点是不是临界轮廓点,如果是设置该坐标像素是白色
  175. if ((rgb >= 0 && rightRgb < 0) || (rgb < 0 && rightRgb >= 0) || (rgb >= 0 && downRgb < 0) || (rgb < 0 && downRgb >= 0))
  176. {
  177. newImage.SetPixel(i, y + j, Color.White);
  178. baseImage.SetPixel(x + i, y + j, Color.White);
  179. }
  180. }
  181. }
  182. return newImage;
  183. }
  184. /// <summary>
  185. /// 根据模板生成干扰图
  186. /// </summary>
  187. /// <param name="baseImage"></param>
  188. /// <param name="templateImage"></param>
  189. /// <param name="x"></param>
  190. /// <param name="y"></param>
  191. private void InterferenceByTemplate(Bitmap baseImage, Bitmap templateImage, int x, int y)
  192. {
  193. // 临时数组遍历用于高斯模糊存周边像素值
  194. int[][] martrix = { new int[3], new int[3], new int[3] };
  195. int[] values = new int[9];
  196. int xLength = templateImage.Width;
  197. int yLength = templateImage.Height;
  198. // 模板图像宽度
  199. for (int i = 0; i < xLength; i++)
  200. {
  201. // 模板图片高度
  202. for (int j = 0; j < yLength; j++)
  203. {
  204. // 如果模板图像当前像素点不是透明色 copy源文件信息到目标图片中
  205. int rgb = templateImage.GetPixel(i, j).ToArgb();
  206. if (rgb < 0)
  207. {
  208. Color oriImageColor = baseImage.GetPixel(x + i, y + j);
  209. //抠图区域半透明
  210. //baseImage.SetPixel(x + i, y + j, Color.FromArgb(120, oriImageColor.R, oriImageColor.G, oriImageColor.B));
  211. //抠图区域高斯模糊
  212. ReadPixel(baseImage, x + i, y + j, values);
  213. FillMatrix(martrix, values);
  214. baseImage.SetPixel(x + i, y + j, AvgMatrix(martrix));
  215. }
  216. //防止数组越界判断
  217. if (i == (xLength - 1) || j == (yLength - 1))
  218. {
  219. continue;
  220. }
  221. int rightRgb = templateImage.GetPixel(i + 1, j).ToArgb();
  222. int downRgb = templateImage.GetPixel(i, j + 1).ToArgb();
  223. //描边处理,,取带像素和无像素的界点,判断该点是不是临界轮廓点,如果是设置该坐标像素是白色
  224. if ((rgb >= 0 && rightRgb < 0) || (rgb < 0 && rightRgb >= 0) || (rgb >= 0 && downRgb < 0) || (rgb < 0 && downRgb >= 0))
  225. {
  226. baseImage.SetPixel(x + i, y + j, Color.White);
  227. }
  228. }
  229. }
  230. }
  231. /// <summary>
  232. /// 更改图片尺寸
  233. /// </summary>
  234. /// <param name="bmp"></param>
  235. /// <param name="width"></param>
  236. /// <param name="height"></param>
  237. /// <returns></returns>
  238. private Bitmap ResizeImage(Bitmap bmp, int width, int height)
  239. {
  240. try
  241. {
  242. Bitmap b = new Bitmap(width, height);
  243. Graphics g = Graphics.FromImage(b);
  244. // 图画质量
  245. g.InterpolationMode = InterpolationMode.HighQualityBicubic;
  246. g.DrawImage(bmp, new Rectangle(0, 0, width, height), new Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel);
  247. g.Dispose();
  248. return b;
  249. }
  250. catch
  251. {
  252. return null;
  253. }
  254. }
  255. /// <summary>
  256. /// 随机范围内数字
  257. /// </summary>
  258. /// <param name="startNum"></param>
  259. /// <param name="endNum"></param>
  260. /// <returns></returns>
  261. public int GetRandomInt(int startNum, int endNum)
  262. {
  263. return (endNum > startNum ? new Random().Next(endNum - startNum) : 0) + startNum;
  264. }
  265. /// <summary>
  266. /// 随机生成拼图坐标
  267. /// </summary>
  268. /// <param name="originalWidth"></param>
  269. /// <param name="originalHeight"></param>
  270. /// <param name="templateWidth"></param>
  271. /// <param name="templateHeight"></param>
  272. /// <returns></returns>
  273. private PointModel GeneratePoint(int originalWidth, int originalHeight, int templateWidth, int templateHeight)
  274. {
  275. Random random = new Random();
  276. int widthDifference = originalWidth - templateWidth;
  277. int heightDifference = originalHeight - templateHeight;
  278. int x;
  279. if (widthDifference <= 0)
  280. {
  281. x = 5;
  282. }
  283. else
  284. {
  285. x = random.Next(originalWidth - templateWidth - 100) + 100;
  286. }
  287. int y;
  288. if (heightDifference <= 0)
  289. {
  290. y = 5;
  291. }
  292. else
  293. {
  294. y = random.Next(originalHeight - templateHeight - 5) + 5;
  295. }
  296. return new PointModel(x, y);
  297. }
  298. /// <summary>
  299. /// 随机生成干扰图坐标
  300. /// </summary>
  301. /// <param name="originalWidth"></param>
  302. /// <param name="originalHeight"></param>
  303. /// <param name="templateWidth"></param>
  304. /// <param name="templateHeight"></param>
  305. /// <param name="blockX"></param>
  306. /// <param name="blockY"></param>
  307. /// <returns></returns>
  308. private PointModel GenerateInterferencePoint(int originalWidth, int originalHeight, int templateWidth, int templateHeight, int blockX, int blockY)
  309. {
  310. int x;
  311. if (originalWidth - blockX - 5 > templateWidth * 2)
  312. {
  313. //在原扣图右边插入干扰图
  314. x = GetRandomInt(blockX + templateWidth + 5, originalWidth - templateWidth);
  315. }
  316. else
  317. {
  318. //在原扣图左边插入干扰图
  319. x = GetRandomInt(100, blockX - templateWidth - 5);
  320. }
  321. int y;
  322. if (originalHeight - blockY - 5 > templateHeight * 2)
  323. {
  324. //在原扣图下边插入干扰图
  325. y = GetRandomInt(blockY + templateHeight + 5, originalHeight - templateHeight);
  326. }
  327. else
  328. {
  329. //在原扣图上边插入干扰图
  330. y = GetRandomInt(5, blockY - templateHeight - 5);
  331. }
  332. return new PointModel(x, y);
  333. }
  334. /// <summary>
  335. /// 获得验证数据
  336. /// </summary>
  337. /// <returns>JObject</returns>
  338. public async Task<CaptchaOutput> GetAsync()
  339. {
  340. //获取网络图片
  341. //var client = new HttpClient();
  342. //var stream = await client.GetStreamAsync("https://picsum.photos/310/155");
  343. //client.Dispose();
  344. //Bitmap baseImage = new Bitmap(stream);
  345. //stream.Dispose();
  346. var oriImage = Image.FromFile($@"{Directory.GetCurrentDirectory()}\wwwroot\captcha\jigsaw\{new Random().Next(1, 4)}.jpg".ToPath());
  347. //更改图片尺寸
  348. //Bitmap baseImage = ResizeImage(oriImage, 310, 155);
  349. Bitmap baseImage = new Bitmap(oriImage);
  350. oriImage.Dispose();
  351. var oriTemplate = Image.FromFile($@"{Directory.GetCurrentDirectory()}\wwwroot\captcha\jigsaw\templates\{new Random().Next(1, 7)}.png".ToPath());
  352. Bitmap templateImage = new Bitmap(oriTemplate);
  353. oriTemplate.Dispose();
  354. int baseWidth = baseImage.Width;
  355. int baseHeight = baseImage.Height;
  356. int templateWidth = templateImage.Width;
  357. int templateHeight = templateImage.Height;
  358. //随机生成拼图坐标
  359. PointModel point = GeneratePoint(baseWidth, baseHeight, templateWidth, templateHeight);
  360. int x = point.X;
  361. int y = point.Y;
  362. //生成拼图
  363. string blockImageBase64 = "data:image/png;base64," + ImgToBase64String(CutByTemplate(baseImage, templateImage, x, y));
  364. //生成干扰图
  365. PointModel interferencePoint = GenerateInterferencePoint(baseWidth, baseHeight, templateWidth, templateHeight, x, y);
  366. InterferenceByTemplate(baseImage, templateImage, interferencePoint.X, interferencePoint.Y);
  367. string baseImageBase64 = "data:image/png;base64," + ImgToBase64String(baseImage);
  368. templateImage.Dispose();
  369. baseImage.Dispose();
  370. var token = Guid.NewGuid().ToString();
  371. CaptchaOutput captchaData = new CaptchaOutput
  372. {
  373. Token = token,
  374. Data = new SlideJigsawCaptchaModel()
  375. {
  376. BlockImage = blockImageBase64,
  377. BaseImage = baseImageBase64
  378. }
  379. };
  380. var key = string.Format(CacheKey.VerifyCodeKey, token);
  381. await _cache.SetAsync(key, point.X);
  382. return captchaData;
  383. }
  384. /// <summary>
  385. /// 检查验证数据
  386. /// </summary>
  387. /// <param name="input"></param>
  388. /// <returns></returns>
  389. public async Task<bool> CheckAsync(CaptchaInput input)
  390. {
  391. if (input == null || input.Data.IsNull())
  392. {
  393. return false;
  394. }
  395. var key = string.Format(CacheKey.VerifyCodeKey, input.Token);
  396. if (await _cache.ExistsAsync(key))
  397. {
  398. try
  399. {
  400. var point = JsonConvert.DeserializeObject<PointModel>(input.Data);
  401. var x = await _cache.GetAsync<int>(key);
  402. if (Math.Abs(x - point.X) < 5)
  403. {
  404. if (input.DeleteCache)
  405. {
  406. await _cache.DelAsync(key);
  407. }
  408. return true;
  409. }
  410. else
  411. {
  412. await _cache.DelAsync(key);
  413. return false;
  414. }
  415. }
  416. catch
  417. {
  418. await _cache.DelAsync(key);
  419. return false;
  420. }
  421. }
  422. else
  423. {
  424. return false;
  425. }
  426. }
  427. }
  428. }