最近也开始研究验证码了。。delphi写的。。
//拍拍抢购http://captcha.paipai.com/getimage
//http://captcha.qq.com/getimage
//http://www.51.la/user/vcode.asp
function DownLoadOCRCode(const imgURL:pchar):TBitmap;stdcall;
function parseurldirectory(const imgURL:pchar):pchar;
var
pctmp:pchar;
btmp:Byte;
destination:Pchar;
begin
Result := '';
btmp:= Pos('.',imgURL);
pctmp:=pchar(Copy(imgURL,btmp+1,length(imgURL)-btmp));
btmp:= Pos('.',pctmp);
pctmp:=pchar(Copy(pctmp,1,btmp-1));
strcopy(destination,pctmp); //不加这句话,直接result:=pctmp 时候 当目录名是数字51的时候,返回的空乱码?
//showmessage(strpas(sDesti));
Result := destination;
end;
function GetImgStyleFromSteam(const Stream:TMemoryStream):Pchar;
var
buffer:word;
imgStyle:pchar;
imgStream:TMemoryStream;
begin
Result := '';
try
imgStream := TMemoryStream.Create;
imgStream.LoadFromStream(stream);
imgStream.Position := 0;
if imgStream.Size = 0 then exit;
imgStream.ReadBuffer(buffer,2);
case buffer of
$4D42:imgStyle:='.bmp';
$D8FF:imgStyle:='.jpeg';
$4947:imgStyle:='.gif';
$5089:imgStyle:='.png';
end;
finally
imgStream.Free;
end;
Result := imgStyle;
end;
Var
imgStream:TMemoryStream;
_Http:TidHttp;
imgDirectory:Pchar;
bitmap:TBitmap;
jpeg:TJpegimage;
buffer:word;
imgStyle:pchar; //文件类型 BMP JPG PNG等
begin
Result:=nil;
try
bitmap := TBitmap.Create;
imgStream:=TMemoryStream.Create;
try
_Http := TIdhttp.Create(nil);
_Http.Get(imgURl,imgStream);
imgStyle:=GetImgStyleFromSteam(imgStream);
if imgStyle = '.jpeg' then
begin
jpeg:= Tjpegimage.Create;
imgStream.Position := 0;
jpeg.LoadFromStream(imgStream);
bitmap.Assign(jpeg);
jpeg.Free;
end
else if imgStyle = '.bmp' then
begin
imgStream.Position := 0;
bitmap.LoadFromStream(imgStream);
end;
imgDirectory :=parseurlDirectory(imgURL); //图片目录
if imgDirectory <> '' then
begin
imgDirectory:=pchar(ExtractFilePath(paramstr(0))+ strpas(imgDirectory));
if not DirectoryExists(imgDirectory) then CreateDir(imgDirectory);
bitmap.SaveToFile(imgDirectory+'\'+formatdatetime('yyyy-mm-dd hh-mm-ss',now)+'.bmp');
Result:= Tbitmap.Create;
Result:=bitmap;
end;
except
On E:Exception do
begin
Result:= nil;
end;
end;
finally
imgStream.Free;
_Http.Free;
//bitmap.Free;
end;
end;