⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ndocr.dpr

📁 一个不错的QQ2009验证码自动识别程序源代码
💻 DPR
字号:
library NdOcr;

uses
  SysUtils,Windows,Classes,Graphics,Math,GifImage,Jpeg,PngImage,IdHTTP,IdCookieManager;

type
  TBArray = Array of byte;
  TModuleLib = record
  	kind: string;
  	hdl: THandle;
    loadLib: function(fn,libName:pchar):Integer;stdcall;
    freeLib: procedure();stdcall;
    getCode: function(var ary:TBArray;w,h:Integer;para:pchar):pchar;stdcall;
  end;

var
	moduleLibAry: Array of TModuleLib;

{$R *.res}

function getCodeFromStream(var pStm:TMemoryStream;colorDepth,kind,para:pchar):pchar;stdcall;
var
  buf: Array[0..3] of byte;
  imgAry: TBArray;
  i,j,lineSize,dataOff,dataSize,imgW,imgH: Integer;
  bmp: TBITMAP;
  gif: TGIFImage;
  jpg: TJPEGImage;
  png: TPNGobject;
  stm: TMemoryStream;
begin
  stm := TMemoryStream.Create();
  stm.LoadFromStream(pStm);

  stm.Position := 0;
  stm.ReadBuffer(buf[0],4);
  stm.Position := 0;
  bmp := TBITMAP.Create();
  if (buf[0]=66) and (buf[1]=77) then begin
    bmp.LoadFromStream(stm)
  end else if (buf[0]=71) and (buf[1]=73) and (buf[2]=70) then begin
    gif := TGifImage.Create();
    gif.LoadFromStream(stm);
    bmp.Assign(gif);
    gif.Free()
  end else if (buf[1]=80) and (buf[2]=78) and (buf[3]=71) then begin
    png := TPNGobject.Create();
    png.LoadFromStream(stm);
    bmp.Assign(png);
    png.Free();
  end else begin
    jpg := TJpegImage.Create();
    jpg.LoadFromStream(stm);
    bmp.Assign(jpg);
    jpg.Free();
  end;

  imgW := bmp.Width;
  imgH := bmp.Height;
  if StrComp(colorDepth,'') <> 0 then bmp.PixelFormat := TPixelFormat(StrToInt(colorDepth));
  bmp.PixelFormat := pf24Bit;
  stm.Clear();
  bmp.SaveToStream(stm);
  bmp.Free();

  stm.Position := 0;
  stm.Seek(10,soCurrent);
  stm.ReadBuffer(buf[0],4);
  dataOff := buf[0] + buf[1] shl 8 + buf[2] shl 16 + buf[3] shl 24;
  lineSize := ((imgW*24+31) shr 5) shl 2;
  stm.Seek(20,soCurrent);
  stm.ReadBuffer(buf[0],4);
  dataSize := buf[0] + buf[1] shl 8 + buf[2] shl 16 + buf[3] shl 24;
  SetLength(imgAry,imgW*imgH*3);
  stm.Position := dataOff + dataSize - lineSize;
  for i:=0 to imgH-1 do
  begin
    stm.ReadBuffer(imgAry[i*imgW*3],imgW*3);
    stm.Seek(-imgW*3-lineSize,soCurrent);
  end;
  stm.Free();

  j := Length(moduleLibAry);
  for i:=0 to j-1 do
    if StrComp(pchar(moduleLibAry[i].kind),kind) = 0 then break;
  if i >= j then result := '?'
  else result := moduleLibAry[i].getCode(imgAry,imgW,imgH,para);
end;

function getCodeFromFile(fn,colorDepth,kind,para:pchar):pchar;stdcall;
var stm: TMemoryStream;
begin
  if FileExists(fn) then
  begin
    stm := TMemoryStream.Create();
    stm.LoadFromFile(fn);
    result := getCodeFromStream(stm,colorDepth,kind,para);
    stm.Free();
  end else
    result := '?';
end;

function getCodeFromUrl(url,domain,cookie,colorDepth,kind,para:pchar):pchar;stdcall;
var
  idhttp: TIDHttp;
  stm: TMemoryStream;
begin
  idhttp := TIDHttp.Create(nil);
  idhttp.CookieManager := TIDCookieManager.Create(idhttp);
  idhttp.CookieManager.AddCookie(cookie,domain);
  stm := TMemoryStream.Create();
  idhttp.Get(url,stm);
  idhttp.free();
  result := getCodeFromStream(stm,colorDepth,kind,para);
  stm.Free();
end;

function loadLib(fn,kind,libFn,libName:pchar):Integer;stdcall;
var
  n,j: Integer;
  mdl: TModuleLib;
begin
  kind := pchar(LowerCase(string(kind)));
  j := Length(moduleLibAry);
  for n:=0 to j-1 do
    if StrComp(pchar(moduleLibAry[n].kind),kind) = 0 then break;
  if n >= j then
  begin
    result := 0;
    mdl.hdl := LoadLibrary(fn);
    if mdl.hdl = 0 then exit;
    mdl.loadLib := GetProcAddress(mdl.hdl,'loadLib');
    mdl.freeLib := GetProcAddress(mdl.hdl,'freeLib');
    mdl.getCode := GetProcAddress(mdl.hdl,'getCode');
    if (@mdl.loadLib=nil) or (@mdl.freeLib=nil) or (@mdl.getCode=nil) then
    begin
      FreeLibrary(mdl.hdl);
      exit;
    end;
    mdl.kind := kind;
    SetLength(moduleLibAry,j+1);
    moduleLibAry[j] := mdl;
    n := j;
  end;
  result := moduleLibAry[n].loadLib(libFn,libName);
end;

procedure freeLib(kind:pchar);stdcall;
var
  n,j: Integer;
begin
  kind := pchar(LowerCase(string(kind)));
  j := Length(moduleLibAry) - 1;
  if StrComp(kind,'') = 0 then begin
    for n:=0 to j do
    begin
      moduleLibary[n].freeLib();
      FreeLibrary(moduleLibAry[n].hdl);
    end;
    SetLength(moduleLibAry,0);
  end else begin
    for n:=0 to j do
      if StrComp(pchar(moduleLibAry[n].kind),kind) = 0 then break;
    if n <= j then
    begin
      moduleLibary[n].freeLib();
      FreeLibrary(moduleLibAry[n].hdl);
    end;
    if n < j then moduleLibAry[n] := moduleLibAry[j];
    if n <= j then SetLength(moduleLibAry,j);
  end;
end;

exports
  loadLib,
  freeLib,
  getCodeFromFile,
  getCodeFromUrl,
  getCodeFromStream;

begin
end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -