Код:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, IdHttp, IdMultipartFormData, StrUtils;
function recognize(filename: string; apikey: string; is_phrase: boolean; is_regsense: boolean; is_numeric: boolean; min_len: integer; max_len: integer): string;
type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Edit2: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function recognize (filename: string; apikey: string; is_phrase: boolean; is_regsense: boolean; is_numeric: boolean; min_len: integer; max_len: integer): string;
var
ftype, tmpstr, captcha_id: string;
i: integer;
http: tidhttp;
multi: tidmultipartformdatastream;
begin
if fileexists(filename)=false then begin result:='error: file not found'; exit; end;
ftype:='image/pjpeg';
if strpos(pchar(filename),'jpg')<>nil then ftype:='image/pjpeg';
if strpos(pchar(filename),'gif')<>nil then ftype:='image/gif';
if strpos(pchar(filename),'png')<>nil then ftype:='image/png';
multi:=tidmultipartformdatastream.create;
multi.addformfield('method','post');
multi.addformfield('key',apikey);
multi.addfile('file',filename,ftype);
if is_phrase=true then multi.addformfield('phrase','1');
if is_regsense=true then multi.addformfield('regsense','1');
if is_numeric=true then multi.addformfield('numeric','1');
if min_len>0 then multi.addformfield('min_len',inttostr(min_len));
if max_len>0 then multi.addformfield('max_len',inttostr(max_len));
http:=tidhttp.create(nil);
tmpstr:=http.post('http://antigate.com/in.php',multi);
http.free; multi.free;
deletefile(filename);
captcha_id:='';
if strpos(pchar(tmpstr),'error_')<>nil then begin result:=tmpstr; exit; end;
if strpos(pchar(tmpstr),'ok|')<>nil then captcha_id:=ansireplacestr(tmpstr,'ok|','');
if captcha_id='' then result:='error: bad captcha id';
for i:=0 to 20 do
begin
sleep (5000);
http:=tidhttp.create(nil);
tmpstr:=http.get('http://antigate.com/res.php?key='+apikey+'&action=get&id='+captcha_id);
http.free;
if strpos(pchar(tmpstr),'error_')<>nil then begin result:=tmpstr; exit; end;
if strpos(pchar(tmpstr),'ok|')<>nil then
begin
result:=ansireplacestr(tmpstr,'ok|','');
exit;
end;
end;
result:='error_timeout';
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Edit2.Text:=recognize('captcha.jpg', Edit1.Text, False, False, False, 0, 0);
end;
end.