首页 > 移动平台 > 详细

Delphi xe7 FireMonkey / Mobile (Android, iOS)生成 QR Code完整实例

时间:2019-01-07 23:53:47      阅读:5487      评论:0      收藏:0      [点我收藏+]

标签:bits   andro   positive   next   tdi   necessary   purpose   5.0   cal   

这个实例在windows、OS X、IOS和Android等平台运行正常。
本文参考这个网站提供的方法:http://zarko-gajic.iz.hr/firemonkey-mobile-android-ios-qr-code-generation-using-delphi-xe-5-delphizxingqrcode/

代码中用到的DelphiZXingQRCode.Pas点这下载

技术分享图片
  1 unit Unit3;
  2 
  3 interface
  4 
  5 uses
  6   System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  7   FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.Objects,
  8   FMX.Controls.Presentation, FMX.Edit, FMX.StdCtrls,DelphiZXingQRCode,
  9   FMX.ListBox,system.math;
 10 
 11 type
 12   TForm3 = class(TForm)
 13     Button1: TButton;
 14     edtText: TEdit;
 15     imgQRCode: TImage;
 16     cmbEncoding: TComboBox;
 17     edtQuietZone: TEdit;
 18     procedure Button1Click(Sender: TObject);
 19   private
 20     { Private declarations }
 21     BMP: TBitmap;
 22   public
 23     { Public declarations }
 24   end;
 25 
 26 var
 27   Form3: TForm3;
 28 
 29 implementation
 30 
 31 {$R *.fmx}
 32 
 33 procedure TForm3.Button1Click(Sender: TObject);
 34 const
 35   downsizeQuality: Integer = 2; // bigger value, better quality, slower rendering
 36 var
 37   QRCode: TDelphiZXingQRCode;
 38   Row, Column: Integer;
 39   pixelColor : TAlphaColor;
 40   vBitMapData : TBitmapData;
 41   pixelCount, y, x: Integer;
 42   columnPixel, rowPixel: Integer;
 43   function GetPixelCount(AWidth, AHeight: Single): Integer;
 44   begin
 45     if QRCode.Rows > 0 then
 46       Result := Trunc(Min(AWidth, AHeight)) div QRCode.Rows
 47     else
 48       Result := 0;
 49   end;
 50 begin
 51   QRCode := TDelphiZXingQRCode.Create;
 52   try
 53     QRCode.Data := edtText.Text;
 54     QRCode.Encoding := TQRCodeEncoding(cmbEncoding.ItemIndex);
 55     QRCode.QuietZone := StrToIntDef(edtQuietZone.Text, 4);
 56     pixelCount := GetPixelCount(imgQRCode.Width, imgQRCode.Height);
 57     case imgQRCode.WrapMode of
 58       TImageWrapMode.iwOriginal,TImageWrapMode.iwTile,TImageWrapMode.iwCenter:
 59       begin
 60         if pixelCount > 0 then
 61           imgQRCode.Bitmap.SetSize(QRCode.Columns * pixelCount,
 62             QRCode.Rows * pixelCount);
 63       end;
 64       TImageWrapMode.iwFit:
 65       begin
 66         if pixelCount > 0 then
 67         begin
 68           imgQRCode.Bitmap.SetSize(QRCode.Columns * pixelCount * downsizeQuality,
 69             QRCode.Rows * pixelCount * downsizeQuality);
 70           pixelCount := pixelCount * downsizeQuality;
 71         end;
 72       end;
 73       TImageWrapMode.iwStretch:
 74         raise Exception.Create(‘Not a good idea to stretch the QR Code‘);
 75     end;
 76//     if imgQRCode.Bitmap.Canvas.BeginScene then
 77//     begin
 78       try
 79         imgQRCode.Bitmap.Canvas.Clear(TAlphaColors.White);
 80         if pixelCount > 0 then
 81         begin
 82           if imgQRCode.Bitmap.Map(TMapAccess.maWrite, vBitMapData)  then
 83           begin
 84             try
 85               for Row := 0 to QRCode.Rows - 1 do
 86               begin
 87                 for Column := 0 to QRCode.Columns - 1 do
 88                 begin
 89                   if (QRCode.IsBlack[Row, Column]) then
 90                     pixelColor := TAlphaColors.Black
 91                   else
 92                     pixelColor := TAlphaColors.White;
 93                   columnPixel := Column * pixelCount;
 94                   rowPixel := Row * pixelCount;
 95                   for x := 0 to pixelCount - 1 do
 9