1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
| unit DelphiZXingQRCodeTestAppMainForm;
// Demo app for ZXing QRCode port to Delphi, by Debenu Pty Ltd
// www.debenu.com
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, DelphiZXingQRCode, Vcl.ExtCtrls,
Vcl.StdCtrls;
type
TForm1 = class(TForm)
edtText: TEdit;
Label1: TLabel;
cmbEncoding: TComboBox;
Label2: TLabel;
Label3: TLabel;
edtQuietZone: TEdit;
Label4: TLabel;
PaintBox1: TPaintBox;
ComboBox1: TComboBox;
procedure FormDestroy(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure PaintBox1Paint(Sender: TObject);
procedure edtTextChange(Sender: TObject);
procedure cmbEncodingChange(Sender: TObject);
procedure edtQuietZoneChange(Sender: TObject);
procedure PaintBox1DblClick(Sender: TObject);
private
QRCodeBitmap: TBitmap;
public
procedure Update;
end;
var
Form1: TForm1;
implementation
uses Vcl.Printers, System.Math;
{$R *.dfm}
procedure TForm1.cmbEncodingChange(Sender: TObject);
begin
Update;
end;
procedure TForm1.edtQuietZoneChange(Sender: TObject);
begin
Update;
end;
procedure TForm1.edtTextChange(Sender: TObject);
begin
Update;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
QRCodeBitmap := TBitmap.Create;
Update;
ComboBox1.Items.Assign(Printer.Printers);
ComboBox1.ItemIndex := Printer.PrinterIndex;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
QRCodeBitmap.Free;
end;
procedure TForm1.PaintBox1DblClick(Sender: TObject);
var
X, Y, Z, I: Integer;
Zoom: TBitmap;
begin
Printer.PrinterIndex := ComboBox1.ItemIndex;
Printer.BeginDoc();
edtText.Text := 'Hello world : Delphi is PowerFull for SHINE Adventure - Minimum';
X := 50;
Y := 50;
Printer.Canvas.Draw(X, Y, QRCodeBitmap);
Inc(Y, QRCodeBitmap.Height);
for I := 1 to 6 do
begin
Zoom := TBitmap.Create();
try
Z := Trunc(Power(2, I));
edtText.Text := Format('Hello world : Delphi is PowerFull for SHINE Adventure - Zoom %dx', [Z])
;
Zoom.Width := QRCodeBitmap.Width * Z;
Zoom.Height := QRCodeBitmap.Height * Z;
Inc(Y, 50);
Zoom.Canvas.StretchDraw(Rect(0, 0, Z * QRCodeBitmap.Width, Z * QRCodeBitmap.Height), QRCodeBitmap);
Printer.Canvas.Draw(X, Y, Zoom);
Inc(Y, Z * QRCodeBitmap.Height);
finally
Zoom.Free();
end;
end;
Printer.EndDoc();
end;
procedure TForm1.PaintBox1Paint(Sender: TObject);
var
Scale: Double;
begin
PaintBox1.Canvas.Brush.Color := clWhite;
PaintBox1.Canvas.FillRect(Rect(0, 0, PaintBox1.Width, PaintBox1.Height));
if ((QRCodeBitmap.Width > 0) and (QRCodeBitmap.Height > 0)) then
begin
if (PaintBox1.Width < PaintBox1.Height) then
begin
Scale := PaintBox1.Width / QRCodeBitmap.Width;
end else
begin
Scale := PaintBox1.Height / QRCodeBitmap.Height;
end;
PaintBox1.Canvas.StretchDraw(Rect(0, 0, Trunc(Scale * QRCodeBitmap.Width), Trunc(Scale * QRCodeBitmap.Height)), QRCodeBitmap);
end;
end;
procedure TForm1.Update;
var
QRCode: TDelphiZXingQRCode;
Row, Column: Integer;
begin
QRCode := TDelphiZXingQRCode.Create;
try
QRCode.Data := edtText.Text;
QRCode.Encoding := TQRCodeEncoding(cmbEncoding.ItemIndex);
QRCode.QuietZone := StrToIntDef(edtQuietZone.Text, 4);
QRCodeBitmap.SetSize(QRCode.Rows, QRCode.Columns);
for Row := 0 to QRCode.Rows - 1 do
begin
for Column := 0 to QRCode.Columns - 1 do
begin
if (QRCode.IsBlack[Row, Column]) then
begin
QRCodeBitmap.Canvas.Pixels[Column, Row] := clBlack;
end else
begin
QRCodeBitmap.Canvas.Pixels[Column, Row] := clWhite;
end;
end;
end;
finally
QRCode.Free;
end;
PaintBox1.Repaint;
end;
end. |
Partager