原文地址

Java、PHP、C#等很容易在网上找到生成验证码图片的代码,Delphi却寥寥无几,昨天花了一整天时间,做了个跨平台的验证码,可以用在C/S和B/S端,支持Windows、Linux、Android、IOS等。对于验证码图形的混淆,只做了简单的随机线条生成,并且随机数是使用系统自带的Randomize和Random函数,Randomize随机数初始化函数由于取种子是保存在全局变量中,虽然是integer类型,但不排除非线程安全问题,所以实际应用中,还需要线程保护。

C/S和B/S上的显示效果:

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;
end;

感谢作者晴空无彩虹无私的分享!

【转】DelphiXE10.2.3——跨平台生成验证码图片的更多相关文章

  1. DelphiXE10.2.3——跨平台生成验证码图片

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

  2. java web学习总结(九) -------------------通过Servlet生成验证码图片

    一.BufferedImage类介绍 生成验证码图片主要用到了一个BufferedImage类,如下:

  3. JavaWeb---总结(九)通过Servlet生成验证码图片

    一.BufferedImage类介绍 生成验证码图片主要用到了一个BufferedImage类,如下: 创建一个DrawImage Servlet,用来生成验证码图片  1 package gacl. ...

  4. Java 生成验证码图片

    生成验证码图片并对提交的输入进行验证 // HttpServletResponse常见应用——生成验证码 // 利用BufferedImage类生产随机图片 public static final i ...

  5. javaweb学习总结(九)—— 通过Servlet生成验证码图片

    一.BufferedImage类介绍 生成验证码图片主要用到了一个BufferedImage类,如下:

  6. 012. asp.net生成验证码图片(汉字示例/字母+数字)

    protected void Page_Load(object sender, EventArgs e) { //生成验证码图片的基本步骤 string checkCode = "新年快乐& ...

  7. J2EE如何生成验证码图片和点击刷新验证码

    验证码图片生成步骤 创建BufferedImage对象. 获取BufferedImage的画笔,即调用getGraphics()方法获取Graphics对象. 调用Graphics对象的setColo ...

  8. java web 学习九(通过servlet生成验证码图片)

    一.BufferedImage类介绍 生成验证码图片主要用到了一个BufferedImage类,如下:

  9. java web,生成验证码图片的技术

    偶然知道原来有些网站的验证码图片都是随机生成的,后来听人讲了一下,就做了这个小例子 生成图片,绘制背景,数字,干扰线用到了java.awt包,主要使用BufferedImage来生成图片,然后使用Gr ...

随机推荐

  1. [Pytorch]深度模型的显存计算以及优化

    原文链接:https://oldpan.me/archives/how-to-calculate-gpu-memory 前言 亲,显存炸了,你的显卡快冒烟了! torch.FatalError: cu ...

  2. 常见文档一览表 -httpclient

    httpclient http://www.yeetrack.com/?p=779 虫师『性能测试』文章大汇总 https://www.cnblogs.com/fnng/archive/2012/08 ...

  3. Java中的垃圾回收机制

    1. 垃圾回收的意义 在C++中,对象所占的内存在程序结束运行之前一直被占用,在明确释放之前不能分配给其它对象:而在Java中,当没有对象引用指向原先分配给某个对象的内存时,该内存便成为垃圾.JVM的 ...

  4. 数组类型的退化Decay

    Decay即数组在某些情况下将退化为指针. 测试代码: #include <iostream> #include <typeinfo> template <typenam ...

  5. gpg: no valid OpenPGP data found. 解决办法

    在Ubuntu14.04 server(amd64)上面安装ros,运行命令 wget http://packages.ros.org/ros.key -O - | sudo apt-key add ...

  6. Windows下使用pip安装python包是报错-UnicodeDecodeError: 'ascii' codec can't decode byte 0xcb in position 0

    先交待下开发环境: 操作系统:Windows 7 Python版本:2.7.9 Pip版本:6.1.1 其他环境忽略 在windows下使用pip下载python包,出现如下错误 Collecting ...

  7. 雷林鹏分享:Ruby 判断

    Ruby 判断 Ruby 提供了其他现代语言中很常见的条件结构.在这里,我们将解释所有的条件语句和 Ruby 中可用的修饰符. Ruby if...else 语句 语法 if conditional ...

  8. 关于一致性hash详细

    一致性哈希算法在1997年由麻省理工学院提出的一种分布式哈希(DHT)实现算法,设计目标是为了解决因特网中的热点(Hot spot)问题,初衷和CARP十分类似.一致性哈希修正了CARP使用的简 单哈 ...

  9. 大年三十。让字母在屏幕上奔跑:(sleep , system"clear")

    system "clear",ruby清屏(osk系统上,window上用system "cls"). https://stackoverflow.com/qu ...

  10. Pollywog CodeForces - 917C (状压)

    链接 大意: 一共n个格子, 初始$x$只蝌蚪在前$x$个格子, 每次最左侧的蝌蚪向前跳, 跳跃距离在范围[1,k], 并且每只蝌蚪跳跃都有一定花费, 有$q$个格子上有石头, 若有蝌蚪跳到某块石头上 ...