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

type
  TFormMain = class(TForm)
    ButtonTakePhoto: TButton;
    FObr: TFObr;
    ActionList: TActionList;
    TakePhotoFromCameraAction: TTakePhotoFromCameraAction;
    Memo: TMemo;
    ImageControl: TImageControl;
    procedure TakePhotoFromCameraActionDidFinishTaking(Image: TBitmap);
    procedure TakePhotoFromCameraActionDidCancelTaking;
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FormMain: TFormMain;

implementation

{$if CompilerVersion >= 33}
  {$define D103PLUS} // Delphi 10.3 or higher
{$endif}

{$if CompilerVersion >= 35}
  {$define D11PLUS} // Delphi 11 or higher
{$endif}

{$ifdef D103PLUS}
uses Androidapi.JNI.Os, Androidapi.JNI.JavaTypes,
  System.Permissions, FMX.DialogService, Androidapi.Helpers;
{$endif D103PLUS}

{$R *.fmx}

procedure TFormMain.FormCreate(Sender: TObject);
begin
{$ifdef D103PLUS}
  PermissionsService.RequestPermissions([
    JStringToString(TJManifest_permission.JavaClass.CAMERA),
    JStringToString(TJManifest_permission.JavaClass.READ_EXTERNAL_STORAGE),
    JStringToString(TJManifest_permission.JavaClass.WRITE_EXTERNAL_STORAGE)],

  {$ifdef D11PLUS}
    procedure(const APermissions: TClassicStringDynArray; const AGrantResults: TClassicPermissionStatusDynArray)
  {$else}
    procedure(const APermissions: TArray<string>; const AGrantResults: TArray<TPermissionStatus>)
  {$endif D11PLUS}
    begin
      ButtonTakePhoto.Enabled := (Length(AGrantResults) = 3)
        and (AGrantResults[0] = TPermissionStatus.Granted)
        and (AGrantResults[1] = TPermissionStatus.Granted)
        and (AGrantResults[2] = TPermissionStatus.Granted);
    end,

  {$ifdef D11PLUS}
    procedure(const APermissions: TClassicStringDynArray; const APostRationaleProc: TProc)
  {$else}
    procedure(const APermissions: TArray<string>; const APostRationaleProc: TProc)
  {$endif D11PLUS}
    begin
      APostRationaleProc;
    end);
{$endif D103PLUS}
end;

procedure TFormMain.TakePhotoFromCameraActionDidCancelTaking;
begin
  Memo.Lines.Clear;
end;

procedure TFormMain.TakePhotoFromCameraActionDidFinishTaking(Image: TBitmap);
var
  i: Integer;
  Barcode: TObrSymbol;
begin
  Memo.Lines.Clear;

  ImageControl.Bitmap.Assign(Image);

  FObr.Active := True;
  FObr.Picture.Assign(Image);
  FObr.Scan;
  if FObr.BarcodeCount = 0 then
    Memo.Lines.Append('No barcode found')
  else
    for i := 0 to FObr.BarcodeCount - 1 do
    begin
      Barcode := FObr.Barcode[i];
      Memo.Lines.Append(Barcode.SymbologyName + Barcode.SymbologyAddonName + ' ' + Barcode.OrientationName + ' ' + Barcode.DataUtf8);
    end;
end;

end.
