$("#img-code").bind( 'click', function () {
$(this).attr('src','VerifyCode?t='+Math.random());
});

unit uVerifyCode;

interface

uses System.Classes, System.SysUtils, FMX.Types, FMX.Objects, FMX.Graphics,
System.UIConsts, System.UITypes,
{$IFDEF MSWINDOWS}ActiveX, {$ENDIF MSWINDOWS}
System.Types; type
// 生成验证码组件
TGenerateVerifyCode = class
private const
// 定义字典表,不要零(0),因为零和字母O样子太接近
arrStr: array [ .. ] of char = (
'','','','','','','','','',
'A','B','C','D','E','F','G','H','I',
'J','K','L','M','N','O','P','Q','R',
'S','T','U','V','W','X','Y','Z');
private
FBitmapWidth: integer; // 图片宽度
FBitmapHeight: integer; // 图片高度
FCodeCount: integer; // 取验证码字符的个数,默认是4个字符
FFontName: string; // 字体名称
FMinFontSize: integer; // 最小字体大小
FRandomLineCount: integer; // 背景随机线条数
FTransparency: byte; // 背景随机线条的透明度
FXRandomLen: integer; // X的随机值长度
FYRandomLen: integer; // Y的随机值长度
// 画出验证码函数
function VerifyCodeDrawImg(Img: TImage): string;
public
constructor Create();
procedure GetVerifyCodeAndImage(ImageStream: TStream;
var VerifyCode: string);
published
property Width: integer read FBitmapWidth write FBitmapWidth;
property Height: integer read FBitmapHeight write FBitmapHeight;
property CodeCount: integer read FCodeCount write FCodeCount;
property FontName: string read FFontName write FFontName;
property MinFontSize: integer read FMinFontSize write FMinFontSize;
property RandomLineCount: integer read FRandomLineCount
write FRandomLineCount;
property Transparency: byte read FTransparency write FTransparency;
property XRandomLen: integer read FXRandomLen write FXRandomLen;
property YRandomLen: integer read FYRandomLen write FYRandomLen;
end; implementation constructor TGenerateVerifyCode.Create();
begin
inherited;
FBitmapWidth := ;
FBitmapHeight := ;
FCodeCount := ;
FFontName := '宋体';
FMinFontSize := ;
FRandomLineCount := ;
FTransparency := ;
FXRandomLen := ;
FYRandomLen := ;
end; // 获取验证码和影像的流数据
procedure TGenerateVerifyCode.GetVerifyCodeAndImage(ImageStream: TStream;
var VerifyCode: string);
var
Img: FMX.Objects.TImage;
begin
{$IFDEF MSWINDOWS}CoInitialize(nil); {$ENDIF MSWINDOWS}
Img := FMX.Objects.TImage.Create(nil);
try
Img.Bitmap := FMX.Graphics.TBitmap.Create(FBitmapWidth, FBitmapHeight);
// 宽100,高40
Img.Bitmap.Canvas.BeginScene;
VerifyCode := VerifyCodeDrawImg(Img);
Img.Bitmap.Canvas.EndScene;
Img.Bitmap.SaveToStream(ImageStream); // 写到流中
finally
freeandnil(Img);
end;
end; // 画出验证码函数
function TGenerateVerifyCode.VerifyCodeDrawImg(Img: TImage): string;
var
I, j, k: integer;
X, Y, W, H: Single;
vLeft: Single;
strResult: RawByteString;
begin
// 只取4个字符
For j := to FCodeCount do
begin
Randomize;
k := Random() mod ;
strResult := strResult + trim(arrStr[k]);
end;
vLeft := ;
Img.Bitmap.Canvas.Font.Family := FFontName;
for j := to FRandomLineCount do // 随机画100条线
begin
Randomize;
Img.Bitmap.Canvas.Stroke.Color := MakeColor(Random() and $C0,
Random() and $C0, Random() and $C0, FTransparency);
Img.Bitmap.Canvas.DrawLine(pointf(Random(), Random()),
pointf(Random(), Random()), );
end;
// 随机字体颜色,这里暂时不用每个字符一个随机颜色
Img.Bitmap.Canvas.Fill.Color := MakeColor((Random() and $C0),
(Random() and $C0), (Random() and $C0));
// 背景色反色
Img.Bitmap.Clear(Img.Bitmap.Canvas.Fill.Color xor $FFFFFF);
for I := to length(strResult) do
begin
Randomize;
// 字体大小
Img.Bitmap.Canvas.Font.Size := Random() + FMinFontSize;
if Img.Bitmap.Canvas.Font.Size < (FMinFontSize + ) then
Img.Bitmap.Canvas.Font.Size := Img.Bitmap.Canvas.Font.Size + ;
if Random() = then
Img.Bitmap.Canvas.Font.Style := [TFontStyle.fsBold]
else
Img.Bitmap.Canvas.Font.Style := [TFontStyle.fsItalic];
begin
X := Random(FXRandomLen) + vLeft;
Y := Random(FYRandomLen);
W := Img.Bitmap.Canvas.TextWidth(strResult[I]);
H := Img.Bitmap.Canvas.TextHeight(strResult[I]);
Img.Bitmap.Canvas.FillText(TRectF.Create(X, Y, X + W, Y + H),
strResult[I], false, , [], TTextAlign.taCenter, TTextAlign.taCenter);
vLeft := X + W + ;
end;
end;
Result := strResult; // 返回值
end; end.

使用方法:

C/S与B/S共同创建方法

var
FGenerateVerifyCode: TGenerateVerifyCode;
begin
FGenerateVerifyCode := TGenerateVerifyCode.Create;
end;

一、C/S使用方法:

procedure TForm1.Button1Click(Sender: TObject);
var
ImageStream: TMemoryStream;
VerifyCode: string;
begin
ImageStream:= TMemoryStream.Create;
try
GetVerifyCodeAndImage(ImageStream, VerifyCode);
Label1.Text:=VerifyCode;
ImageStream.Position:=;
Image1.Bitmap.LoadFromStream(ImageStream);
finally
ImageStream.Free;
end;
end;

二、B/S使用方法:

1、HTML中加入Img元素

<img src="VerifyCode" id="img-code" class="Verify-Code" style="cursor:pointer" alt="验证码" title="看不清楚?点一下图片刷新">

2、JS代码

$("#img-code").bind( 'click', function () {
$(this).attr('src','VerifyCode?t='+Math.random());
});

3、服务端代码

// 获取验证码图片
function TSystemPagePlugin.Method_VerifyCode(Request: TWebRequest;
Response: TWebResponse): boolean;
VAR
VerifyCode: string;
begin
Response.ContentStream := TMemoryStream.Create;
Response.ContentType := 'application/binary;';
// 获取验证码图片和数值
GetVerifyCodeAndImage(Response.ContentStream, VerifyCode);
result := true; ---------------------
作者:晴空无彩虹
来源:****
原文:https://blog.****.net/u011784006/article/details/80827181
版权声明:本文为博主原创文章,转载请附上博文链接!

相关文章