delphi 绘制二维码

uses
   vcl.Graphics, DelphiZXingQRCode;

procedure CreateQRCode(AImg: vcl.Graphics.TBitMap; const ACode: String);
const
  IMG_SCALE = 25; //放大倍数
var
  QRCode: TDelphiZXingQRCode;
  Row, Column: Integer;
begin
  if (AImg = nil) or (Trim(ACode) = '') then Exit;

  QRCode := TDelphiZXingQRCode.Create;
  try
    QRCode.Data := ACode;
    QRCode.Encoding := TQRCodeEncoding(qrUTF8BOM);
    QRCode.QuietZone := 1;
    QRCode.Data := ACode;

    AImg.Height := QRCode.Rows * IMG_SCALE;
    AImg.Width := QRCode.Columns * IMG_SCALE;

    AImg.Canvas.Lock;
    AImg.Canvas.Brush.Color := clWhite;
    AImg.Canvas.FillRect(Rect(0, 0, AImg.Width, AImg.Height));
    AImg.Canvas.Brush.Color := clBlack;
    for Row := 0 to QRCode.Rows - 1 do
      for Column := 0 to QRCode.Columns - 1 do
        if QRCode.IsBlack[Row, Column] then
        begin
          AImg.Canvas.FillRect(Rect(Column * IMG_SCALE,  Row * IMG_SCALE,
            Column * IMG_SCALE + IMG_SCALE, Row * IMG_SCALE + IMG_SCALE));
        end;
    AImg.Canvas.Unlock;
  finally
    QRCode.Free;
  end;
end;

 

posted @ 2017-11-25 15:48  伯通心智  阅读(2110)  评论(0编辑  收藏  举报