Der Code generiert eine
Zahlenfolge als Captcha in die Imagekomponente, die sich dann per Knopfdruck durch eine MaskEdit Box vergleichen lässt.
Die MaskEdit Box muss den Wert EditMask = "999999;1;~" haben.
uses Mask
//
var
Form1: TForm1;
CurCod : Integer;
//
procedure TForm1.Captcha(Code: Integer);
const lines = 6; // HIER DIE ANZAHL DER ZEICHEN SETZTEN
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;
function TForm1.TestCaptcha(C: String): Boolean;
begin
Result := IntToStr(CurCod) = C;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Randomize;
SetCaptcha;
end;
procedure TForm1.SetCaptcha;
begin
CurCod := Random(899999) + 100000;
Captcha(CurCod);
end;
Beispiel :
procedure TForm1.Button1Click(Sender: TObject);
begin
if TestCaptcha(MaskEdit1.Text) = true then
Label1.Caption := 'Captcha Succes'
else
Label1.Caption := 'Captcha Error';
end;
Keine Kommentare:
Kommentar veröffentlichen