Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Net;
- using System.Drawing;
- namespace ddddd
- {
- public struct CaptchaSymbol
- {
- public int LenPx;
- public string Name;
- }
- class AntiCaptcha
- {
- Bitmap img = null;
- public static CaptchaSymbol[] CaptchaSymbols = {new CaptchaSymbol{
- LenPx = 14,
- Name = "1",
- }, new CaptchaSymbol{
- LenPx = 32,
- Name = "2",
- }, new CaptchaSymbol{
- LenPx = 31,
- Name = "3",
- }, new CaptchaSymbol{
- LenPx = 27,
- Name = "4",
- }, new CaptchaSymbol{
- LenPx = 38,
- Name = "5",
- }, new CaptchaSymbol{
- LenPx=33,
- Name = "6",
- }, new CaptchaSymbol{
- LenPx=26,
- Name = "7",
- }, new CaptchaSymbol{
- LenPx=40,
- Name = "8",
- }, new CaptchaSymbol{
- LenPx=35,
- Name = "9",
- }, new CaptchaSymbol{
- LenPx = 39,
- Name = "0"}
- };
- Dictionary<string, List<int>> hTable = new Dictionary<string, List<int>>();
- public AntiCaptcha(string URLimg)
- {
- WebClient webClient = new WebClient();
- 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");
- webClient.DownloadFile(URLimg, "temp.gif");
- this.img = new Bitmap("temp.gif");
- }
- public string GetCode()
- {
- var palette = new Dictionary<Color, int>();
- for (var x = 0; x < img.Width; x++)
- {
- for (var y = 0; y < img.Height; y++)
- {
- var clr = img.GetPixel(x, y);
- if (!palette.ContainsKey(clr))
- {
- palette.Add(clr, 1);
- }
- else
- {
- palette[clr] = palette[clr] + 1;
- }
- }
- }
- var i = 0;
- foreach (var p in palette)
- {
- if (p.Value > 20)
- {
- var temp = this.ClearBitmap(img, p.Key);
- temp.Save(String.Format("mask-{0}.bmp", i));
- i++;
- }
- }
- Bitmap a = new Bitmap("mask-0.bmp");
- for (int j = 1; j <= 5; j++)
- {
- GetPosition(a, 8 * j, 16, j);
- }
- int oldWidth = 0;
- int newWidth = 0;
- int CountPixel = 0;
- string result = null;
- for (int j = 0; j < hTable["width"].Count; j++)
- {
- newWidth = hTable["width"][j];
- CountPixel = CountPixels(a, newWidth, 16, oldWidth);
- if (CountPixel > 0)
- {
- foreach (CaptchaSymbol si in CaptchaSymbols)
- {
- if (si.LenPx == CountPixel)
- {
- result += si.Name;
- break;
- }
- }
- }
- oldWidth = hTable["width"][j];
- }
- return result;
- }
- private Bitmap ClearBitmap(Bitmap input, Color clr)
- {
- var result = new Bitmap(input.Width, input.Height);
- for (var x = 0; x < input.Width; x++)
- {
- for (var y = 0; y < input.Height; y++)
- {
- var color = input.GetPixel(x, y);
- result.SetPixel(x, y, clr == color ? Color.White : Color.Black);
- }
- }
- return result;
- }
- private int CountPixels(Bitmap img, int Width, int Height, int oldWidth)
- {
- int a = 0;
- for (int y = 0; y < Height; y++)
- {
- for (int x = oldWidth; x < Width; x++)
- {
- if (img.GetPixel(x, y).R == 0)
- a++;
- }
- }
- return a;
- }
- private int GetPosition(Bitmap img, int Width, int Height, int j)
- {
- hTable["width"] = new List<int>();
- int OldWidth = j * 8 - 8;
- int MaxWidth = 0;
- int MaxHeight = 0;
- for (var x = 0; x < Width; x++)
- {
- for (var y = 0; y < Height; y++)
- {
- if (img.GetPixel(x, y).R == 255)
- MaxHeight++;
- if (MaxHeight == 16)
- {
- hTable["width"].Add(x);
- break;
- }
- }
- MaxHeight = 0;
- }
- return MaxWidth;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement