日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Delphi识别读取验证码

發布時間:2024/4/14 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Delphi识别读取验证码 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?

unit OCR;interfaceuses Windows, SysUtils, Graphics, Classes, PNGImage, GIFImage, JPEG, Math, AsphyreZlib;typeTOCRLibSetting = record //驗證碼庫設置SaveBMP: Boolean; //存儲轉換后的Bmp文件BmpPath: String; //Bmp存儲路徑BmpPrefix: String; //Bmp文件前綴BmpSuffix: String; //Bmp文件后綴end;type//圖像大小類TOCRSz = recordW,H: Byte; //寬,高end;//特征碼模板庫類TOCRTemplates = recordCount: Byte; //數量Names: array of String; //名稱OCRFiles: array of String; //文件名/路徑OCRSz: array of TOCRSz; //圖像大小YaoqiuSS: array of Byte; //是否為算式end;//初始化驗證碼庫 function InitOCRLib: Boolean; //取消使用Dll procedure CancelUseDLL; //加載驗證碼模板庫 function LoadOCRLib(const AFileName: String = ''): Boolean; //圖像轉換為BMP function ALL2BMP(const Filename : String; const Bmp: TBitmap): Boolean; //加載資源dll function LoadOCRResourceDLL(const ADllName: String): Boolean; //識別驗證碼 function RecogOCR(var Success: Boolean; const ImageFile: String): String; //更改特征碼模板 function LoadOCRTemplate(const TmplID: Integer): Boolean; //加載特征碼文件 function LoadOCRFile(const ocrFile: String; const IsAutoSS: Boolean = False): Boolean; //查找驗證碼特征文件 function FetchOCR(const StartIndex: Integer; const Width,Height: Integer; const AOCRName: String = ''): Integer; //驗證碼庫設置 function SetOCRLib(const ASetting: TOCRLibSetting): Boolean; //獲得驗證碼庫設置 function GetOCRLibSetting: TOCRLibSetting; //獲得驗證碼模板庫 function GetOCRTemplates: TOCRTemplates; //獲取最后識別時間(毫秒) function GetLastRecogTime: DWORD; //調用AspriseOcr//function RecogOCRByOCRLib(const FileName: String): String;//釋放驗證碼庫/清除特征碼文件 function FreeOcr: Boolean;//procedure SetPicFormat(Format: Byte);constFMT_AUTO = 4; //自動FMT_PNG = 2; //pngFMT_BMP = 1; //bmpFMT_GIF = 3; //gifFMT_JPEG = 0; //jpg/jpegimplementationuses IniFiles, SSUtils;typeRSpeicalEffects = record //特殊效果To1Line: Boolean; //字符歸位RemoveZD: Boolean; //消除噪點Y0: Byte; //Y軸偏移XcZD: Byte; //噪點閥值end;type //字符特征碼RChar = recordMyChar: char; //字符used: Boolean; //已使用MyCharInfo: array[0..49, 0..49] of byte; //字符圖像end;type //字符特征文件RCharInfo = recordcharwidth: byte; //字符寬度charheight: byte; //字符高度X0: byte; //第一個字符開始x偏移TotalChars: byte; //圖象字符總數CusDiv : boolean; //自定義二值化運算DivCmp : Byte; // 0:> 1:= 2:<<br> DivColr : TColor; //二值化閥值_CmpChr,_CmpBg: Boolean; //比較字符(黑色),比較背景(白色)_ClrRect: Boolean; //清除矩形_RectLen: Byte; //矩形長度allcharinfo: array[0..42] of RChar; //字符特征碼列表end;typeTOcrVersionSng = array [0..1] of Byte;TOcrVersion = record //版本號First,Minjor: Byte; //版本Author: String[10]; //作者Name: String[20]; //特征碼名稱end;ROcrLibFile = recordSng: TOcrVersionSng; //版本標識Ver: TOcrVersion; //版本W,H: Byte; //圖像寬,高Effect: RSpeicalEffects; //特殊效果CharInfo: RCharInfo; //特征碼EffectBLW: Boolean; //通用二值化end;TOcrLibDllInfo = recordDllFile: String;MDLRPrefix: String;MDLRType: String;end;var_BITMAP: TBitmap; //識別圖像MycharInfo: RCharInfo; //特征碼_Effect: RSpeicalEffects; //特效_EffBLW: Boolean; //通用二值化SSCode: Byte; //是否為算式varBmW,BmH: Integer; //特征碼圖像寬,高OcrName: String; //特征碼名稱_PicFormat: Byte; //圖像格式_PicWidth,_PicHeight: Byte; //實際圖像寬,高Templates: TOCRTemplates; //模板列表Setting: TOCRLibSetting;LastRecogTime: DWORD;varUseDll: Boolean;DllInfo: TOcrLibDllInfo;constSP = '@';procedure CancelUseDLL;beginUseDll := False;end;function GetLastRecogTime: DWORD;beginResult := LastRecogTime;end;function GetOCRLibSetting: TOCRLibSetting;beginResult := Setting;end;function GetOCRTemplates: TOCRTemplates;beginResult := Templates;end;function LoadOCRResourceDLL(const ADllName: String): Boolean;varstrm: TResourceStream;hDll: THandle;S: String;function GetTempPathFileName: String;varSPath, SFile : PChar;beginSPath := AllocMem(MAX_PATH);SFile := AllocMem(MAX_PATH);GetTempPath(MAX_PATH, SPath);GetTempFileName(SPath, '~OC', 0, SFile);Result := String(SFile);FreeMem(SPath, MAX_PATH);FreeMem(SFile, MAX_PATH);DeleteFile(Result);end;beginResult := False;tryhDll := LoadLibrary(PChar(ADllName));if hDll <> 0 thenbegintrystrm := TResourceStream.Create(hDll,'SDSOFT_OCR',PChar('OCR'));S := GetTempPathFileName;strm.SaveToFile(S);tryUseDll := True;Result := LoadOCRLib(S);exceptUseDll := False;end;if Result = False then UseDll := False;if UseDll = True then DllInfo.DllFile := ADllName;DeleteFile(S);finallyFreeLibrary(hDll);end;end;Result := True;exceptend;end;function SetOCRLib(const ASetting: TOCRLibSetting): Boolean;beginResult := False;trySetting := ASetting;Result := True;exceptend;end;function InitOCRLib: Boolean;beginResult := False;tryUseDll := False;DllInfo.DllFile := '';DllInfo.MDLRPrefix := '';DllInfo.MDLRType := '';_BITMAP := nil;FillChar(MycharInfo,SizeOf(RCharInfo),#0);MycharInfo.DivCmp := 3;MycharInfo.DivColr := $7FFFFF;MycharInfo._CmpChr := True;MycharInfo._CmpBg := False;MycharInfo.X0 := 0;MycharInfo.charwidth := 0;MycharInfo.CusDiv := False;MycharInfo.charheight := 0;FillChar(_Effect,SizeOf(RSpeicalEffects),#0);_Effect.To1Line := False;_Effect.RemoveZD := False;Setting.SaveBMP := False;Setting.BmpPrefix := 'OCR';Setting.BmpSuffix := '';LastRecogTime := 0;exceptend;end;function FetchOCR(const StartIndex: Integer; const Width,Height: Integer; const AOCRName: String = ''): Integer;varI: Integer;beginResult := -1;for I := StartIndex to Integer(Templates.Count) - 1 dobeginif (Templates.Names[I] = AOCRName) or((Templates.OCRSz[I].W = Width) and (Templates.OCRSz[I].H = Height))thenbeginResult := I;Break;end;end;end;function LoadOCRLib(const AFileName: String = ''): Boolean;varIni: TIniFile;S,S2: String;I,J: Integer;FileName: String;beginResult := False;FileName := AFileName;if FileName = '' thenFileName := ExtractFilePath(ParamStr(0))+'OCR.INI';tryTemplates.Count := 0;SetLength(Templates.Names,0);SetLength(Templates.OCRFiles,0);Ini := TIniFile.Create(FileName);Templates.Count := Byte(Ini.ReadInteger('OCRLIB','TCNT',0));SetLength(Templates.Names,Templates.Count*SizeOf(String));SetLength(Templates.OCRFiles,Templates.Count*SizeOf(String));SetLength(Templates.OCRSz,Templates.Count*SizeOf(TOCRSz));SetLength(Templates.YaoqiuSS,Templates.Count*SizeOf(Byte));for I := 0 to Templates.Count - 1 dobeginS := Ini.ReadString('OCRLIB','T'+IntToStr(I),'');if S <> '' thenbeginJ := Pos(';',S);S2 := Copy(S,1,J-1);S := Copy(S,J+1,Length(S)-J+1);if UseDll then Templates.OCRFiles[I] := S2else Templates.OCRFiles[I] := ExtractFilePath(ParamStr(0))+S2;J := Pos(';',S);S2 := Copy(S,1,J-1);S := Copy(S,J+1,Length(S)-J+1);Templates.OCRSz[I].W := Byte(StrToInt(S2));J := Pos(';',S);S2 := Copy(S,1,J-1);S := Copy(S,J+1,Length(S)-J+1);Templates.OCRSz[I].H := Byte(StrToInt(S2));Templates.YaoqiuSS[I] := Byte(StrToInt(S));Templates.Names[I] := Ini.ReadString('OCRNAME','T'+IntToStr(I),'');end;end;if UseDll = True thenbeginDllInfo.MDLRPrefix := Ini.ReadString('DLLSETTING','Prefix','');DllInfo.MDLRType := Ini.ReadString('DLLSETTING','ResourceType','OCR');end;Ini.Free;Result := True;exceptend;end;function LoadOCRFile(const ocrFile: String; const IsAutoSS: Boolean = False): Boolean;varFstrm: TFileStream;strm: TMemoryStream;dat: ROcrLibFile;function VersVerify: Boolean;beginResult := (dat.Sng[0] = Byte('O')) and (dat.Sng[1] = Byte('C'));end;beginResult := False;tryFstrm := TFileStream.Create(ocrFile,fmOpenRead);strm := TMemoryStream.Create;tryFstrm.Position := 0;ZDecompressStream(FStrm,strm);Fstrm.Free;strm.Position := 0;strm.Read(dat,SizeOf(ROcrLibFile));if VersVerify = True thenbeginMycharInfo := dat.CharInfo;_Effect := dat.Effect;BmW := dat.W;BmH := dat.H;OcrName := dat.Ver.Name;_EffBLW := dat.EffectBLW;Result := True;end;finallystrm.Free;end;if IsAutoSS = True then SSCode := 1else SSCode := 0;exceptend;end;procedure To1Line(const Bmp: TBitmap; Y0,X0,Chw,CharL: Byte);typexByteArray = array of Byte;varX,Y: Integer;Ch: TBitmap;MinJL: xByteArray;function MinArr(const Data: xByteArray; const Count: Integer): Byte;varI: Integer;beginif Count = 0 then Exit;Result := Data[0];for I := 0 to Count - 1 dobeginif Data[I] < Result then Result := Data[I];end;end;procedure GetMinJL(const nChar: Byte);varK,L,M: Byte;c: TColor;MinJLS: xByteArray;beginK := X0 + nChar * Chw;SetLength(MinJLS,Chw);for L := 0 to Chw - 1 dobeginM := 0;c := Bmp.Canvas.Pixels[K+L,M+Y0];while (c <> clBlack) and (M <= Bmp.Height) dobegininc(M);c := Bmp.Canvas.Pixels[K+L,M+Y0];end;MinJLS[L] := M;end;MinJL[nChar] := MinArr(MinJLS,Chw);SetLength(MinJLS,0);end;beginSetLength(MinJL,CharL);Ch := TBitmap.Create;for X := 0 to CharL - 1 dobeginGetMinJL(X);Y := X0 + X * Chw;Ch.Width := Chw;Ch.Height := Bmp.Height - MinJL[X];Ch.Canvas.Brush.Color := clWhite;Ch.Canvas.Brush.Style := bsSolid;Ch.Canvas.Pen.Color := clWhite;Ch.Canvas.Pen.Style := psSolid;Ch.Canvas.Rectangle(0,0,Ch.Width,Ch.Height);Ch.Canvas.CopyRect(Rect(0,0,Ch.Width,Ch.Height),Bmp.Canvas,Rect(Y,MinJL[X],Y+Chw,Bmp.Height));Bmp.Canvas.Brush.Color := clWhite;Bmp.Canvas.Brush.Style := bsSolid;Bmp.Canvas.Pen.Color := clWhite;Bmp.Canvas.Pen.Style := psSolid;Bmp.Canvas.Rectangle(Y,MinJL[X],Y+Chw,Bmp.Height);Bmp.Canvas.CopyRect(Rect(Y,Y0,Y+Chw,Bmp.Height-MinJL[X]),Ch.Canvas,Rect(0,0,Ch.Width,Ch.Height));end;Ch.Free;SetLength(MinJL,0);end;function GetTail(str,sp : String): Integer;varTemp : String;beginTemp := Str;Delete(Temp,1,Pos(sp,str)+length(sp)-1);Result := StrToInt(Temp);end;procedure SlQuickSort(Sl : TStringList; iLo, iHi: Integer);varLo, Hi, Mid : Integer;T : String;beginLo := iLo;Hi := iHi;Mid := GetTail(Sl[(Lo + Hi) div 2],Sp);repeatwhile GetTail(Sl[Lo],Sp) < Mid do Inc(Lo);while GetTail(Sl[Hi],Sp) > Mid do Dec(Hi);if Lo <= Hi thenbeginT := sl[Lo];sl[Lo] := sl[Hi];sl[Hi] := T;Inc(Lo);Dec(Hi);end;until Lo > Hi;if Hi > iLo then SlQuickSort(Sl, iLo, Hi);if Lo < iHi then SlQuickSort(Sl, Lo, iHi);end;Function HexToInt(Hex :String):Int64;Var Sum : Int64;I,L : Integer;BeginL := Length(Hex);Sum := 0;For I := 1 to L DoBeginSum := Sum * 16;If ( Ord(Hex[I]) >= Ord('0')) and (Ord(Hex[I]) <= Ord('9')) thenSum := Sum + Ord(Hex[I]) - Ord('0')else If ( Ord(Hex[I]) >= Ord('A') ) and (Ord(Hex[I]) <= Ord('F')) thenSum := Sum + Ord(Hex[I]) - Ord('A') + 10else If ( Ord(Hex[I]) >= Ord('a') ) and ( Ord(Hex[I]) <= Ord('f')) thenSum := Sum + Ord(Hex[I]) - Ord('a') + 10elseBeginSum := -1;break;End;End;Result := Sum;End;function GetHead(str,sp : String):string;beginResult:=copy(str,1,pos(sp,str)-1);end;procedure WhiteBlackImgEx(const bmp: TBitmap);typexByteArray = array of Byte;varp: PByteArray;J,Y,W: Integer;arr: xByteArray;function AverageArr(const Data: xByteArray; const Count: Integer): Int64;varI: Integer;beginResult := 0;if Count = 0 then Exit;for I := 0 to Count - 1 dobeginResult := Result + Data[I];end;Result := Round(Result/Count);end;beginbmp.PixelFormat := pf24bit;SetLength(arr,bmp.Height*bmp.Width);for Y := 0 to bmp.Height - 1 dobeginp := bmp.ScanLine[Y];J := 0;while J < bmp.Width*3 dobeginarr[(Y*bmp.Width)+J div 3] := Round((p[J]+p[J+1]+p[J+2])/3);Inc(J,3);end;end;W := Byte(AverageArr(Arr,bmp.Height*bmp.Width));for Y := 0 to bmp.Height - 1 dobeginp := bmp.ScanLine[Y];J := 0;while J < bmp.Width*3 dobeginif Round((p[J]+p[J+1]+p[J+2])/3) >= W thenbeginp[J] := 0;p[J+1] := 0;p[J+2] := 0;end elsebeginp[J] := MaxByte;p[J+1] := MaxByte;p[J+2] := MaxByte;end;Inc(J,3);end;end;SetLength(Arr,0);end;procedure Ranse(const bmp: TBitmap; const Color: TColor);varc: TColor;X,Y: Integer;r1,g1,b1: Byte;r2,g2,b2: Byte;beginr1 := GetRValue(Color);g1 := GetGValue(Color);b1 := GetBValue(Color);for X := 0 to bmp.Width - 1 dobeginfor Y := 0 to bmp.Height - 1 dobeginc := Bmp.Canvas.Pixels[X,Y];r2 := GetRValue(c);g2 := GetGValue(c);b2 := GetBValue(c);// if (c <> clWhite) and (c <> clBlack) then// beginr2 := Round(r1*Min(Abs(r2-MaxByte),MaxByte-r2)/MaxByte);g2 := Round(g1*Min(Abs(g2-MaxByte),MaxByte-g2)/MaxByte);b2 := Round(b1*Min(Abs(b2-MaxByte),MaxByte-b2)/MaxByte);c := RGB(r2,g2,b2);Bmp.Canvas.Pixels[X,Y] := c;// end;end;end;end;procedure Grayscale(const bmp: TBitmap);varp: PByteArray;J,Y,W: Integer;beginbmp.PixelFormat := pf24bit;for Y := 0 to bmp.Height - 1 dobeginp := bmp.ScanLine[Y];J := 0;while J < bmp.Width*3 dobeginW := (P[J] * 28 + P[J+1] *151 + P[J+2] * 77);W := W shr 8;P[J] := Byte(W);P[J+1] := Byte(W);P[J+2] := Byte(W);Inc(J,3);end;end;//bmp.PixelFormat := pf1bit;//bmp.PixelFormat := pf24bit; end;function ALL2BMP(const Filename : String; const Bmp: TBitmap): Boolean;varGIF: TGIFImage;jpg: TJPEGImage;PNG: TPNGobject;FileEx: String;beginResult := False;tryFileEx := UpperCase(ExtractFileExt(filename));if FileEx = '.PNG' thenbeginPNG := TPNGobject.Create;tryPNG.LoadFromFile(filename);_PicFormat := 2;BMP.Assign(PNG);except//not png imageend;PNG.Free;end else if FileEx = '.BMP' thentryBMP.LoadFromFile(filename);_PicFormat := 1;except//not bmp imageendelse if FileEx = '.GIF' thenbeginGIF := TGIFImage.Create;tryGIF.LoadFromFile(filename);_PicFormat := 3;BMP.Assign(GIF);except//not gif imageend;GIF.Free;end else if (FileEx = '.JPG') or (FileEx = '.JPEG') thenbeginJPG := TJPEGImage.Create;tryJPG.LoadFromFile(filename);_PicFormat := 4;BMP.Assign(JPG);except//not jpg imageend;JPG.Free;end;//if _PicFormat = 0 thentryBMP.LoadFromFile(FileName);_PicFormat := 1;exceptend;if _PicFormat = 0 thenbeginPNG := TPNGobject.Create;tryPNG.LoadFromFile(FileName);_PicFormat := 2;BMP.Assign(PNG);finallyPNG.Free;end;end;if _PicFormat = 0 thenbeginGIF := TGIFImage.Create;tryGIF.LoadFromFile(FileName);_PicFormat := 3;BMP.Assign(GIF);finallyGIF.Free;end;end;if _PicFormat = 0 thenbeginJPG := TJPEGImage.Create;tryJPG.LoadFromFile(FileName);BMP.Assign(JPG);_PicFormat := 4;finallyJPG.Free;end;end;Result := True;exceptend;end;function PIC2BMP(filename : String): TBITMAP;varGIF: TGIFImage;jpg: TJPEGImage;BMP: TBITMAP;PNG: TPNGobject;FileEx: String;i, j, x: Byte;b : boolean;//SrcRGB : pByteArray;ClPixel : TColor;beginb := False;ClPixel := 0;FileEx := UpperCase(ExtractFileExt(filename));BMP := TBITMAP.Create;if FileEx = '.PNG' thenbeginPNG := TPNGobject.Create;tryPNG.LoadFromFile(filename);_PicFormat := 2;BMP.Assign(PNG);except//not png imageend;PNG.Free;end else if FileEx = '.BMP' thentryBMP.LoadFromFile(filename);_PicFormat := 1;except//not bmp imageendelse if FileEx = '.GIF' thenbeginGIF := TGIFImage.Create;tryGIF.LoadFromFile(filename);_PicFormat := 3;BMP.Assign(GIF);except//not gif imageend;GIF.Free;end else if (FileEx = '.JPG') or (FileEx = '.JPEG') thenbeginJPG := TJPEGImage.Create;tryJPG.LoadFromFile(filename);_PicFormat := 4;JPG.Grayscale := TRUE;BMP.Assign(JPG);except//not jpg imageend;JPG.Free;end;//if _PicFormat = 0 thentryBMP.LoadFromFile(FileName);_PicFormat := 1;exceptend;if _PicFormat = 0 thenbeginPNG := TPNGobject.Create;tryPNG.LoadFromFile(FileName);_PicFormat := 2;BMP.Assign(PNG);finallyPNG.Free;end;end;if _PicFormat = 0 thenbeginGIF := TGIFImage.Create;tryGIF.LoadFromFile(FileName);_PicFormat := 3;BMP.Assign(GIF);finallyGIF.Free;end;end;if _PicFormat = 0 thenbeginJPG := TJPEGImage.Create;tryJPG.LoadFromFile(FileName);JPG.Grayscale := TRUE;BMP.Assign(JPG);_PicFormat := 4;finallyJPG.Free;end;end;_PicWidth := BMP.Width;_PicHeight := BMP.Height;//BMP.SaveToFile(_PicFile+'.BMP');//Fetch(_BbsType,_PicWidth,_PicHeight,_PicFormat,_CodeUrl);if _EffBLW thenbeginGrayscale(bmp);Ranse(bmp,clRed);WhiteBlackImgEx(bmp);end elsebeginBmp.PixelFormat := pf24Bit;// make picture only black and whitefor j := 0 to BMP.Height - 1 dobeginSrcRGB := BMP.ScanLine[j];for i := 0 to BMP.Width - 1 dobeginif MycharInfo._ClrRect thenbeginx := MycharInfo._RectLen;if (iBMP.Width-1-x)or(j>BMP.Height-1-x) thenbeginSrcRGB[i*3] := $ff;SrcRGB[i*3+1] := $ff;SrcRGB[i*3+2] := $ff;continue;end;end;ClPixel := HexToInt(IntToHex(SrcRGB[i*3],2)+IntToHex(SrcRGB[i*3+1],2)+IntToHex(SrcRGB[i*3+2],2));if MycharInfo.CusDiv thenbegincase MycharInfo.DivCmp of0: b := ClPixel > MycharInfo.DivColr;1: b := ClPixel = MycharInfo.DivColr;2: b := ClPixel < MycharInfo.DivColr;4: b := ClPixel <> MycharInfo.DivColr;end;end elseb := ClPixel > MycharInfo.DivColr;if b then beginSrcRGB[i*3] := $ff;SrcRGB[i*3+1] := $ff;SrcRGB[i*3+2] := $ff;end else beginSrcRGB[i*3] := 0;SrcRGB[i*3+1] := 0;SrcRGB[i*3+2] := 0;end;end;end;end;{BMP.Canvas.lock;for i := 0 to BMP.Width - 1 dofor j := 0 to BMP.Height - 1 dobeginif _ClrRect thenbeginx := _RectLen;if (iBMP.Width-1-x)or(j>BMP.Height-1-x) thenbeginBMP.Canvas.Pixels[i, j] := clwhite;continue;end;end;if _CusDiv thenbegincase _DivCmp of0: b := BMP.Canvas.Pixels[i, j] > _DivColr;1: b := BMP.Canvas.Pixels[i, j] = _DivColr;2: b := BMP.Canvas.Pixels[i, j] < _DivColr;end;end elseb := BMP.Canvas.Pixels[i, j] > _DivColr;if b thenBMP.Canvas.Pixels[i, j] := clwhiteelseBMP.Canvas.Pixels[i, j] := clblack;end;BMP.Canvas.Unlock; }result := BMP;end;function CMPBMP(SBMP: TBITMAP; x0, m: integer): integer;vari, j: integer;//SrcRGB : pByteArray;beginresult := 0;for j := 0 to MycharInfo.charheight -1 dobeginSrcRGB := SBMP.ScanLine[j];for i := 0 to MycharInfo.charwidth -1 dobeginif MycharInfo._CmpChr and (SrcRGB[(x0+i)*3] = 0) and (MycharInfo.allcharinfo[m].MyCharInfo[i, j] = 0) thenInc(Result);if MycharInfo._CmpBg and (SrcRGB[(x0+i)*3] > 0) and (MycharInfo.allcharinfo[m].MyCharInfo[i, j] = 1) thenInc(Result);end;end;{result := 0;SBMP.Canvas.Lock;for i := 0 to MycharInfo.charwidth - 1 dofor j := 0 to MycharInfo.charHeight - 1 dobeginif _CmpChr and (SBMP.Canvas.Pixels[x0 + i, j] = 0) and (MycharInfo.allcharinfo[m].MyCharInfo[i, j] = 0) thenInc(Result);if _CmpBg and (SBMP.Canvas.Pixels[x0 + i, j] > 0) and (MycharInfo.allcharinfo[m].MyCharInfo[i, j] = 1) thenInc(Result);end;SBMP.Canvas.Unlock; }end;function CMPBMPPRO(SBMP: TBITMAP; x0, m: integer): integer;vari, j : integer;xj : byte;Ret : Integer;//SrcRGB : pByteArray;beginresult := 99999;for xj := 0 to _BITMAP.Height - MycharInfo.charheight dobeginRet := 0;for j := 0 to MycharInfo.charHeight - 1 dobeginSrcRGB := SBMP.ScanLine[j+xj];for i := 0 to MycharInfo.charwidth - 1 dobeginif MycharInfo._CmpChr and (SrcRGB[(x0+i)*3] = 0) and (MycharInfo.allcharinfo[m].MyCharInfo[i, j] = 0) thenInc(Ret);if MycharInfo._CmpBg and (SrcRGB[(x0+i)*3] > 0) and (MycharInfo.allcharinfo[m].MyCharInfo[i, j] = 1) thenInc(Ret);end;end;if result > Ret thenresult := Ret;end;{result := 99999;SBMP.Canvas.Lock;for xj := 0 to _BITMAP.Height - MycharInfo.charheight dobeginRet := 0;for i := 0 to MycharInfo.charwidth - 1 dofor j := 0 to MycharInfo.charHeight - 1 dobeginif _CmpChr and (SBMP.Canvas.Pixels[x0 + i, j+xj] = 0) and (MycharInfo.allcharinfo[m].MyCharInfo[i, j] = 0) thenInc(Ret);if _CmpBg and (SBMP.Canvas.Pixels[x0 + i, j+xj] > 0) and (MycharInfo.allcharinfo[m].MyCharInfo[i, j] = 1) thenInc(Ret);end;if result > Ret thenresult := Ret;end;SBMP.Canvas.Unlock; }end;function GetStringFromImage(SBMP: TBITMAP): String; //const// SpeicalChars: array [0..6] of String = ('+','-','*','/','(',')','='); vark, m, x: integer;alike : Integer;S : String;Sort : boolean;SlAlike : TStringList;begin//DebugStr('SBMP_W_H',IntToStr(SBMP.Width)+'*'+IntToStr(SBMP.Height),'e:');result := '';if _Effect.To1Line = True thenbegintryTo1Line(SBMP,_Effect.Y0,MycharInfo.X0,MycharInfo.charwidth,Mycharinfo.TotalChars);exceptend;end;SlAlike := TStringList.Create;for k := 0 to MycharInfo.TotalChars - 1 dobeginx := MycharInfo.X0 + MyCharInfo.charwidth * k;//DebugLog('k:'+IntToStr(k)+' '+'x:'+IntToStr(x));SlAlike.Clear;Sort := True;for m := 0 to 42 dobeginif Mycharinfo.allcharinfo[m].used = True thenbegin{if m>35 thenS := SpeicalChars[m-36]else if m>9 thenS := Chr(m+87)elseS := IntToStr(m); }S := Mycharinfo.allcharinfo[m].MyChar;if SBMP.Height = MycharInfo.charheight thenAlike := CMPBMP(SBMP, x, m)elseAlike := CMPBMPPRO(SBMP, x, m);//DebugLog('m:'+s+' '+'Alike:'+IntToStr(Alike));if Alike = 0 thenbeginResult := Result + S;//DebugLog('get_it:'+s);//DebugStr('GET_IT','GET '+S+ ' AS '+IntToStr(k+1)+ 'TH NUM','e:');Sort := False;break;end elseSlAlike.Add(S + Sp + IntToStr(Alike));end;end;if Sort thenbeginSlQuickSort(SlAlike,0,SlAlike.Count-1);result := result + GetHead(SlAlike[0],Sp);//DebugLog('get_it_by_sort:'+GetHead(SlAlike[0],Sp));//DebugStr('GET_IT_SORT','GET '+GetHead(SlAlike[0],Sp)+ ' AS '+IntToStr(k)+ 'TH NUM','e:');//SlAlike.SaveToFile('f:\'+IntToStr(k)+'.txt');end;end;SlAlike.Free;end;function RecogOCR(var Success: Boolean; const ImageFile: String): String;beginSuccess := False;try_BITMAP := nil;LastRecogTime := GetTickCount;_BITMAP := PIC2BMP(ImageFile);Result := GetStringFromImage(_BITMAP);LastRecogTime := GetTickCount-LastRecogTime;SaveBmp;_BITMAP.Free;Success := True;if SSCode = 1 then Result := SSUtils.RecogSuanshi(Result);exceptLastRecogTime := 0;end;end;end. //----------------------------------------------------------//---------------------------------------------------------- unit SSUtils;interfaceuses Windows, SysUtils, CalcExpress;function RecogSuanshi(const S: String): String;implementationfunction DeleteFh(const S: String; const Fh: Char): String;varI: Integer;beginResult := '';for I := 1 to Length(S) dobeginif S[I] <> Fh thenbeginResult := Result + S[I];end;end;end;function RecogSuanshi(const S: String): String; constargv: array [0..1] of Extended = (0,1);varS2: String;cexp: TCalcExpress;beginResult := '計算錯誤!';trycexp := TCalcExpress.Create(nil);tryS2 := DeleteFh(S,'?');S2 := DeleteFh(S,'=');S2 := StringReplace(S2,'','+',[rfReplaceAll]);S2 := StringReplace(S2,'','-',[rfReplaceAll]);S2 := StringReplace(S2,'','*',[rfReplaceAll]);S2 := StringReplace(S2,'','/',[rfReplaceAll]);S2 := StringReplace(S2,'×','*',[rfReplaceAll]);S2 := StringReplace(S2,'÷','/',[rfReplaceAll]);S2 := StringReplace(S2,'','+',[rfReplaceAll]);S2 := StringReplace(S2,'','-',[rfReplaceAll]);cexp.Formula := S2;Result := IntToStr(Round(cexp.calc(argv)));exceptend;finallycexp.Free;end;end;end.

?

總結

以上是生活随笔為你收集整理的Delphi识别读取验证码的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。