unit QRCodeDemo;

interface

uses
  QRCode,Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ComCtrls, ExtCtrls,printers;

type
  TQRCodeDemoForm = class(TForm)
    Image1: TImage;
    btnRefresh: TButton;
    btnExit: TButton;
    btnPrint: TButton;
    Button1: TButton;
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Margin: TLabel;
    Label6: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    txtCode: TEdit;
    cEncoding: TComboBox;
    cFNC1: TComboBox;
    txtSize: TEdit;
    txtMargin: TEdit;
    cVersion: TComboBox;
    txtAppIdentifier: TEdit;
    TabSheet2: TTabSheet;
    Label10: TLabel;
    Label7: TLabel;
    txtindex: TEdit;
    txtTotal: TEdit;
    chkStructuredAppend: TCheckBox;
    ComboEC: TComboBox;
    chkAutoConfigurate: TCheckBox;
    Label5: TLabel;     
    txtPrinterSize: TEdit;
    procedure btnRefreshClick(Sender: TObject);
    procedure btnPrintClick(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure btnExitClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    qrcode:TQRCode;
  public
    { Public declarations }
  end;

var
  QRCodeDemoForm: TQRCodeDemoForm;


implementation

{$R *.DFM}

procedure TQRCodeDemoForm.btnRefreshClick(Sender: TObject);
var
dummy:integer;
value:integer;
begin

{create new barcode}
{encoding}
if (cEncoding.itemIndex>=0) then qrcode.encoding:=TQRCodeEncoding(cEncoding.itemIndex);

{value}
qrcode.code:=txtCode.text;
qrcode.processTilde:=true;
{format}
if (cVersion.itemIndex>=0) then qrcode.preferredVersion:=cVersion.itemIndex+1;

if (cFNC1.itemIndex>=0) then qrcode.fnc1mode:=TQRCodeFNC1Mode(cFNC1.itemIndex);

if (ComboEC.itemIndex>=0) then qrcode.correctionLevel:=TQRCodeECLevel(ComboEC.itemIndex);

{dots size}
val(txtSize.text,value,dummy);
qrcode.moduleWidth:=value;
{margin}
val(txtMargin.text,value,dummy);
qrcode.marginPixels:=value;
{rune}
if (txtAppIdentifier.text='') then txtAppIdentifier.text:='-1';
val(txtAppIdentifier.text,value,dummy);
qrcode.applicationIndicator:=value;
{correction level}


qrcode.width:=Image1.width;
qrcode.height:=Image1.height;

{structured append}
qrcode.structuredAppend:=false;
if (chkStructuredAppend.Checked) then begin
                                qrcode.structuredAppend:=true;
end;

qrcode.structuredAppendCounter:=1;
val(txtTotal.text,value,dummy);
qrcode.structuredAppendCounter:=value;

qrcode.structuredAppendIndex:=1;
val(txtIndex.text,value,dummy);
qrcode.structuredAppendindex:=value;

qrcode.autoconfigurate:=chkAutoConfigurate.Checked;
qrcode.paintBarcode(Image1.Canvas);


end;

procedure TQRCodeDemoForm.btnPrintClick(Sender: TObject);
var
value:integer;
dummy:integer;
begin
     with printer do
     begin
		BeginDoc;

                {dots size}
                val(txtPrinterSize.text,value,dummy);
                qrcode.moduleWidth:=value;

                qrcode.offsetX:=printer.PageWidth div 10;
		qrcode.paintBarcode(Canvas);

		EndDoc;
     end;

end;

procedure TQRCodeDemoForm.Button1Click(Sender: TObject);
var
bmp:TBitmap;
begin
       bmp:=TBitmap.create();
     bmp.height:=400;
     bmp.width:=400;
     qrcode.paintBarcode(bmp.Canvas);
     bmp.SaveToFile('qrcode.bmp');

     MessageDlg('Image saved to qrcode.bmp.', mtInformation,      [mbOk], 0);


end;

procedure TQRCodeDemoForm.btnExitClick(Sender: TObject);
begin
hide();

end;


procedure TQRCodeDemoForm.FormCreate(Sender: TObject);
begin
qrcode:=TQRCode.Create(nil) ;
qrcode.autoConfigurate:=true;
{refresh}

cEncoding.itemIndex:=4;
ComboEC.itemIndex:=0;
cVersion.itemIndex:=0;
btnRefreshClick(nil);
end;
end.
