uniGUI学习之随机验证码生成及判断

效果图:

uniGUI学习之随机验证码生成及判断(59)_验证码

 

uses AuthenticodeGenerate, ExtCtrls;
  • 1.

AuthenticodeGenerate.pas

unit AuthenticodeGenerate;

interface

uses
  SysUtils, Windows, ExtCtrls, Graphics;

function GenerateAuthenticode(const Img: TImage;
  const Len: Integer = 4): string;

implementation

const
cCharDigitArrayLen = 10;
cCharDigitArray: array [0 .. cCharDigitArrayLen - 1] of Char = ('0', '1', '2', '3', '4', '5', '6','7', '8', '9');

cCharLowerLetterArrayLen = 13;
cCharLowerLetterArray: array [0 .. cCharLowerLetterArrayLen - 1]
of wideChar = ('涂', '一', '二', '三', '赵', '王', '孙', '李', '张', '熊', '五', '拼', '磊'); //也可以添加中文字符

  cCharUpperLetterArrayLen = 19;
  cCharUpperLetterArray: array [0 .. cCharUpperLetterArrayLen - 1]
    of Char = ('A', 'B', 'C', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P',
    'Q', 'R', 'S', 'V', 'W', 'Y');

  cArrayTypeNum = 3;//上面三种 类型数 也可再添加,添加后,在 // 出随机字符串,里添加4对应处理

{ cCLen =8;
cCArray: array [0 .. cCLen - 1] of Char = ('-','-','-','-','-','-','-','-');

cArrayTypeNum = 4;}

  cFontNameNum = 5;
  cFontNameArray: array [0 .. cFontNameNum - 1] of string = ('Arial', 'Tahoma',
    '宋体', '幼圆', '微软雅黑');

procedure NoiseImage(const Img: TImage);
const
  cNoiseLineNum = 5;
  cNoisePointNum = 50;
var
  I: Integer;
  X: Integer;
  Y: Integer;
begin
  for I := 0 to cNoiseLineNum - 1 do
  begin
    Img.Canvas.Pen.Style := psSolid;

    case Random(3) of
      0:
        Img.Canvas.Pen.Color := clBlack;
      1:
        Img.Canvas.Pen.Color := clGray;
    else
      Img.Canvas.Pen.Color := clSilver;
    end;

    X := Random(Img.Width);
    Y := Random(Img.Height);
    Img.Canvas.MoveTo(X, Y);
    Img.Canvas.LineTo(X + Random(Img.Width - X), Y + Random(Img.Height - Y));
  end;

  for I := 0 to cNoisePointNum - 1 do
  begin
    case Random(3) of
      0:
        Img.Canvas.Pixels[Random(Img.Width), Random(Img.Height)] := clBlack;
      1:
        Img.Canvas.Pixels[Random(Img.Width), Random(Img.Height)] := clGray;
    else
      Img.Canvas.Pixels[Random(Img.Width), Random(Img.Height)] := clSilver;
    end;
  end;
end;

function GenerateCharacterAuthenticode(const Img: TImage;
  const Len: Integer = 4): string;
var
  I: Integer;
  V: Char;
  X: Integer;
  Y: Integer;
  L: Integer;
  str: string;
begin
  // 出随机字符串
  Result := '';

  for I := 0 to Len - 1 do
  begin
    case Random(cArrayTypeNum) of
      0:
        begin
          V := cCharDigitArray[Random(cCharDigitArrayLen)];
          Result := Result + V;
        end;
      1:
        begin
          V := cCharLowerLetterArray[Random(cCharLowerLetterArrayLen)];
          Result := Result + V;
        end;
      2:
      begin
        V := cCharUpperLetterArray[Random(cCharUpperLetterArrayLen)];
        Result := Result + V;
      end;
      
    end;
  end;

  L := 2 + Random(2);
  str := Result;
  Img.Picture := nil;

  // /开始字符串 扭曲变形
  for I := 0 to Length(str) - 1 do
  begin
    Img.Canvas.Font.Size := Random(5) + 17;   //17为默认字体大小
    Img.Canvas.Font.Color := RGB(Random(256) and $C0, Random(256) and $C0,
      Random(256) and $C0);
    case Random(2) of
      0:
        Img.Canvas.Font.Style := [fsBold];
      1:
        Img.Canvas.Font.Style := [fsItalic];

    end;
    Img.Canvas.Font.Name := cFontNameArray[Random(cFontNameNum)];
    X := Random(4) + L;
    Y := Random(2) + 4;
    Img.Canvas.TextOut(X, Y, Result[I + 1]);
    L := 8+ X + Img.Canvas.TextWidth(Result[I + 1]) + Random(2);//8为字符间距
  end;

  // 制造背景图噪点
  NoiseImage(Img);
end;

function GenerateAuthenticode(const Img: TImage; const Len: Integer): string;
begin
  Result := GenerateCharacterAuthenticode(Img, Len);
end;

initialization
    Randomize;
end.
  • .

调用及生成

procedure TMainForm.UniImage1Click(Sender: TObject);
var
  img1: TImage;
begin
  img1 := TImage.Create(self);
  try
     img1.Width:=250;//图形验证码的宽度
     img1.Height:=50;//图形验证码的高度

    UniLabel1.Caption := GenerateAuthenticode(img1, 6);//6为验证码位数,可以改成4
    UniImage1.Picture.Bitmap.Assign(img1.Picture.Bitmap);
  finally
    FreeAndNil(img1);
  end;
end;

验证:

登录后复制 

procedure TMainForm.UniButton2Click(Sender: TObject);
begin
if uniEdit1.Text= UniLabel1.Caption then      showmessage('asdf');
end;
  • 9
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蝈蝈(GuoGuo)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值