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.Media,
  FMX.StdCtrls, FMX.Layouts, FMX.ExtCtrls, System.Actions,
  FMX.ActnList, FMX.StdActns, FMX.MediaLibrary.Actions, FMX.Memo,
  FMX.Controls.Presentation, FMX.ScrollBox, FMX.Memo.Types,
  Winsoft.Android.Obr;

type
  TFormMain = class(TForm)
    ButtonScan: TButton;
    Memo: TMemo;
    ImageControl: TImageControl;
    procedure ButtonScanClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
    BarcodeDecoder: TBarcodeDecoder;
  public
    { Public declarations }
  end;

var
  FormMain: TFormMain;

implementation

uses System.Rtti;

{$R *.fmx}

procedure TFormMain.FormCreate(Sender: TObject);
begin
  BarcodeDecoder := TBarcodeDecoder.Create;
end;

procedure TFormMain.FormDestroy(Sender: TObject);
begin
  BarcodeDecoder.Free;
end;

function BytesToStr(Bytes: TBytes): string;
var
  I: Integer;
begin
  Result := '';
  for I := 0 to Length(Bytes) - 1 do
  begin
    if I <> 0 then
      Result := Result + ' ';
    Result := Result + IntToHex(Bytes[I], 2);
  end;
end;

procedure TFormMain.ButtonScanClick(Sender: TObject);
var
  Barcodes: TDecodeResults;
  Points: TArray<TPointF>;
  ByteSegments: TArray<TBytes>;
  I, J: Integer;
begin
  Memo.Lines.Clear;
  Barcodes := BarcodeDecoder.Decode(ImageControl.Bitmap);
  for I := 0 to Length(Barcodes) - 1 do
  begin
    Memo.Lines.Append('Barcode ' + IntToStr(I + 1));
    Memo.Lines.Append('  Format: ' + IntToStr(Ord(Barcodes[I].Format)));
    Memo.Lines.Append('  Format: ' + TRttiEnumerationType.GetName<TBarcodeFormat>(Barcodes[I].Format));
    Memo.Lines.Append('  Format name: ' + Barcodes[I].FormatName);
    Memo.Lines.Append('  Text: ' + Barcodes[I].Text);
    Memo.Lines.Append('  Bytes: ' + BytesToStr(Barcodes[I].Bytes));
    Memo.Lines.Append('  Timestamp: ' + IntToStr(Barcodes[I].Timestamp));
    Memo.Lines.Append('  Bit count: ' + IntToStr(Barcodes[I].BitCount));
    Memo.Lines.Append('  Country: ' + Barcodes[I].Country);
    Memo.Lines.Append('  Correction level: ' + Barcodes[I].CorrectionLevel);
    Memo.Lines.Append('  Extension: ' + Barcodes[I].Extension);
    Memo.Lines.Append('  Price: ' + Barcodes[I].Price);
    Memo.Lines.Append('  Symbology identifier: ' + Barcodes[I].SymbologyIdentifier);

    ByteSegments := Barcodes[I].ByteSegments;
    for J := 0 to Length(ByteSegments) - 1 do
      Memo.Lines.Append('  Byte segment ' + IntToStr(J + 1) + ': ' + BytesToStr(ByteSegments[J]));

    Points := Barcodes[I].Points;
    for J := 0 to Length(Points) - 1 do
      Memo.Lines.Append('  Point ' + IntToStr(J + 1) + ': ' + FloatToStr(Points[J].X) + ',' + FloatToStr(Points[J].Y));
  end;
end;

end.
