Исходник капчи (captcha)

Исходники делфи, готовые программы/приложения и т.д.
Ответить
Аватара пользователя
glGizma
Site Admin
Сообщения: 182
Зарегистрирован: Ср сен 27, 2017 2:20 pm

Исходник капчи (captcha)

Сообщение glGizma » Сб янв 19, 2019 3:35 am

Код: Выделить всё

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls, Mask;

type
  TForm1 = class(TForm)
    Image1: TImage;
    Label1: TLabel;
    MaskEdit1: TMaskEdit;
    Button1: TButton;
    lerr: TLabel;
    procedure Captcha(Code : Integer);
    procedure FormCreate(Sender: TObject);
    procedure SetCaptcha;
    function TestCaptcha(C : String) : Boolean;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  CurCod : Integer;
implementation

{$R *.dfm}

{ TForm1 }

procedure TForm1.Button1Click(Sender: TObject);
begin
  if TestCaptcha(MaskEdit1.Text) then Halt
                                 else lerr.Show;
end;

procedure TForm1.Captcha(Code: Integer);
const lines = 6;
var x,y,g : integer;
    str   : string;
begin
  Image1.Canvas.Brush.Color := clWhite;
  Image1.Canvas.Brush.Style := bsSolid;
  Image1.Canvas.FillRect(Image1.Canvas.ClipRect);
  if (Code < 99999) or (Code > 1000000) then Image1.Canvas.TextOut(4,4,'INVALID CODE');
  for x := 0 to Image1.Width - 1 do
    for y := 0 to Image1.Height - 1 do
      if Random(1000) mod 2 = 0 then begin
        g := Random(100)+100;
        Image1.Canvas.Pixels[x,y] := rgb(g,g,g);
      end;
  Image1.Canvas.Brush.Style := bsClear;
  for x := 1 to 6 do begin
    g := Random(100)+100;
    case Random(3) of
      0 : Image1.Canvas.Font.Color := rgb(g,0,0);
      1 : Image1.Canvas.Font.Color := rgb(0,g,0);
      2 : Image1.Canvas.Font.Color := rgb(0,0,g);
    end;
    case random(2) of
      0 : Image1.Canvas.Font.Style := [fsBold];
      1 : Image1.Canvas.Font.Style := [];
    end;
    Image1.Canvas.Font.Size := 15 + Random(6);
    Image1.Canvas.TextOut((x-1)*15+10, random(image1.Height-Image1.Canvas.TextHeight('1')), IntToStr(Code)[x]);
  end;
  Image1.Canvas.Pen.Color := clBlack;
  for x := 0 to lines - 1 do begin
    Image1.Canvas.MoveTo(0, Random(Image1.Height));
    Image1.Canvas.LineTo(Image1.Width, Random(Image1.Height));
  end;

end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Randomize;
  SetCaptcha;
end;

procedure TForm1.SetCaptcha;
begin
  CurCod := Random(899999) + 100000;
  Captcha(CurCod);
end;

function TForm1.TestCaptcha(C: String): Boolean;
begin
  Result := IntToStr(CurCod) = C;
end;

end.
Вложения
Captcha.zip
(237.05 КБ) 99 скачиваний
111.jpg
111.jpg (19.07 КБ) 2165 просмотров

Ответить