unit Main;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.StdCtrls,
  FBarcode, FMX.Edit, FMX.ListBox, FMX.Controls.Presentation,
  FMX.Layouts, FMX.ExtCtrls, FMX.Objects, FMX.Colors, FMX.Ani;

type
  TFormMain = class(TForm)
    FBarcode: TFBarcode;
    LabelError: TLabel;
    ComboBox: TComboBox;
    Edit: TEdit;
    Image: TImage;
    TrackBar: TTrackBar;
    PanelBarcode: TPanel;
    PanelColor: TPanel;
    ComboColorBoxBackColor: TComboColorBox;
    ComboColorBoxForeColor: TComboColorBox;
    ColorAnimation1: TColorAnimation;
    procedure FormCreate(Sender: TObject);
    procedure EditTyping(Sender: TObject);
    procedure ComboBoxChange(Sender: TObject);
    procedure EditChange(Sender: TObject);
    procedure FormResize(Sender: TObject);
    procedure TrackBarChange(Sender: TObject);
    procedure ComboColorBoxBackColorChange(Sender: TObject);
    procedure ComboColorBoxForeColorChange(Sender: TObject);
  private
    { Private declarations }
    Scale: Single;
    procedure ShowBarcode;
  public
    { Public declarations }
  end;

var
  FormMain: TFormMain;

implementation

uses FMX.Platform;

{$R *.fmx}

procedure TFormMain.ShowBarcode;
begin
  try
    LabelError.Text := '';
    Image.BeginUpdate;
    try
      Image.Bitmap.Assign(nil);
      Image.SetBounds(
        (PanelBarcode.Width - FBarcode.Bitmap.Width / Scale) / 2.0,
        (PanelBarcode.Height - FBarcode.Bitmap.Height / Scale) / 2.0,
        FBarcode.Bitmap.Width / Scale,
        FBarcode.Bitmap.Height / Scale);
      Image.Bitmap.Assign(FBarcode.Bitmap);
    finally
      Image.EndUpdate;
    end;
  except
    on E: Exception do
    begin
      LabelError.Text := E.Message;
      Image.Bitmap.Assign(nil);
    end;
  end;
end;

procedure TFormMain.TrackBarChange(Sender: TObject);
begin
  FBarcode.Scale := TrackBar.Value / 10.0;
  ShowBarcode;
end;

procedure TFormMain.ComboBoxChange(Sender: TObject);
begin
  FBarcode.SymbologyName := ComboBox.Selected.Text;
  ShowBarcode;
end;

procedure TFormMain.ComboColorBoxBackColorChange(Sender: TObject);
begin
  FBarcode.BackColor := ComboColorBoxBackColor.Color;
  ShowBarcode;
end;

procedure TFormMain.ComboColorBoxForeColorChange(Sender: TObject);
begin
  FBarcode.ForeColor := ComboColorBoxForeColor.Color;
  ShowBarcode;
end;

procedure TFormMain.EditChange(Sender: TObject);
begin
  FBarcode.InputText := Edit.Text;
  ShowBarcode;
end;

procedure TFormMain.EditTyping(Sender: TObject);
begin
  FBarcode.InputText := Edit.Text;
  ShowBarcode;
end;

procedure TFormMain.FormCreate(Sender: TObject);
var
  ScreenSrv: IFMXScreenService;
  Symbology: TSymbology;
  Items: TStringList;
begin
  if TPlatformServices.Current.SupportsPlatformService(IFMXScreenService, ScreenSrv) then
    Scale := ScreenSrv.GetScreenScale
  else
    Scale := 1.0;

  FBarcode.InputText := Edit.Text;

  Items := TStringList.Create;
  try
    for Symbology := Low(Symbology) to High(Symbology) do
      Items.Add(FBarcode.SymbologyNames[Symbology]);
    Items.Sort;

    ComboBox.Items.Assign(Items);
    ComboBox.ItemIndex := ComboBox.Items.IndexOf('QR Code');
  finally
    FreeAndNil(Items);
  end;
end;

procedure TFormMain.FormResize(Sender: TObject);
begin
  ShowBarcode;
end;

end.
