Advertisement
Guest User

Untitled

a guest
Jan 30th, 2022
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.05 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Net;
  5. using System.Drawing;
  6.  
  7. namespace ddddd
  8. {
  9.     public struct CaptchaSymbol
  10.     {
  11.         public int LenPx;
  12.         public string Name;
  13.     }
  14.  
  15.     class AntiCaptcha
  16.     {
  17.         Bitmap img = null;
  18.  
  19.         public static CaptchaSymbol[] CaptchaSymbols = {new CaptchaSymbol{
  20.             LenPx = 14,
  21.             Name = "1",
  22.         }, new CaptchaSymbol{
  23.             LenPx = 32,
  24.             Name = "2",
  25.         }, new CaptchaSymbol{
  26.             LenPx = 31,
  27.             Name = "3",
  28.         }, new CaptchaSymbol{
  29.             LenPx = 27,
  30.             Name = "4",
  31.         }, new CaptchaSymbol{
  32.             LenPx = 38,
  33.             Name = "5",
  34.         }, new CaptchaSymbol{
  35.             LenPx=33,
  36.             Name = "6",
  37.         }, new CaptchaSymbol{
  38.             LenPx=26,
  39.             Name = "7",
  40.         }, new CaptchaSymbol{
  41.             LenPx=40,
  42.             Name = "8",
  43.         }, new CaptchaSymbol{
  44.             LenPx=35,
  45.             Name = "9",
  46.         }, new CaptchaSymbol{
  47.             LenPx = 39,
  48.             Name = "0"}
  49.         };
  50.  
  51.         Dictionary<string, List<int>> hTable = new Dictionary<string, List<int>>();
  52.         public AntiCaptcha(string URLimg)
  53.         {
  54.             WebClient webClient = new WebClient();
  55.             webClient.Headers.Add("user-agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.2 (KHTML like Gecko) Chrome/15.0.874.102 Safari/535.2");
  56.             webClient.DownloadFile(URLimg, "temp.gif");
  57.             this.img = new Bitmap("temp.gif");
  58.         }
  59.  
  60.         public string GetCode()
  61.         {
  62.             var palette = new Dictionary<Color, int>();
  63.             for (var x = 0; x < img.Width; x++)
  64.             {
  65.                 for (var y = 0; y < img.Height; y++)
  66.                 {
  67.                     var clr = img.GetPixel(x, y);
  68.                     if (!palette.ContainsKey(clr))
  69.                     {
  70.                         palette.Add(clr, 1);
  71.                     }
  72.                     else
  73.                     {
  74.                         palette[clr] = palette[clr] + 1;
  75.                     }
  76.                 }
  77.             }
  78.             var i = 0;
  79.             foreach (var p in palette)
  80.             {
  81.                 if (p.Value > 20)
  82.                 {
  83.                     var temp = this.ClearBitmap(img, p.Key);
  84.                     temp.Save(String.Format("mask-{0}.bmp", i));
  85.                     i++;
  86.                 }
  87.             }
  88.  
  89.             Bitmap a = new Bitmap("mask-0.bmp");
  90.             for (int j = 1; j <= 5; j++)
  91.             {
  92.                 GetPosition(a, 8 * j, 16, j);
  93.             }
  94.  
  95.             int oldWidth = 0;
  96.             int newWidth = 0;
  97.             int CountPixel = 0;
  98.             string result = null;
  99.             for (int j = 0; j < hTable["width"].Count; j++)
  100.             {
  101.                 newWidth = hTable["width"][j];
  102.                 CountPixel = CountPixels(a, newWidth, 16, oldWidth);
  103.                 if (CountPixel > 0)
  104.                 {
  105.                     foreach (CaptchaSymbol si in CaptchaSymbols)
  106.                     {
  107.                         if (si.LenPx == CountPixel)
  108.                         {
  109.                             result += si.Name;
  110.                             break;
  111.                         }
  112.  
  113.                     }
  114.                 }
  115.                 oldWidth = hTable["width"][j];
  116.  
  117.             }
  118.  
  119.             return result;
  120.         }
  121.  
  122.         private Bitmap ClearBitmap(Bitmap input, Color clr)
  123.         {
  124.             var result = new Bitmap(input.Width, input.Height);
  125.             for (var x = 0; x < input.Width; x++)
  126.             {
  127.                 for (var y = 0; y < input.Height; y++)
  128.                 {
  129.                     var color = input.GetPixel(x, y);
  130.                     result.SetPixel(x, y, clr == color ? Color.White : Color.Black);
  131.                 }
  132.             }
  133.  
  134.             return result;
  135.         }
  136.  
  137.         private int CountPixels(Bitmap img, int Width, int Height, int oldWidth)
  138.         {
  139.             int a = 0;
  140.             for (int y = 0; y < Height; y++)
  141.             {
  142.                 for (int x = oldWidth; x < Width; x++)
  143.                 {
  144.                     if (img.GetPixel(x, y).R == 0)
  145.                         a++;
  146.                 }
  147.             }
  148.             return a;
  149.         }
  150.  
  151.         private int GetPosition(Bitmap img, int Width, int Height, int j)
  152.         {
  153.             hTable["width"] = new List<int>();
  154.             int OldWidth = j * 8 - 8;
  155.             int MaxWidth = 0;
  156.             int MaxHeight = 0;
  157.  
  158.             for (var x = 0; x < Width; x++)
  159.             {
  160.                 for (var y = 0; y < Height; y++)
  161.                 {
  162.                     if (img.GetPixel(x, y).R == 255)
  163.                         MaxHeight++;
  164.                     if (MaxHeight == 16)
  165.                     {
  166.                         hTable["width"].Add(x);
  167.                         break;
  168.                     }
  169.                 }
  170.                 MaxHeight = 0;
  171.  
  172.             }
  173.             return MaxWidth;
  174.         }
  175.  
  176.     }
  177. }
  178.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement