unit Demos.ImageOcr.Forms.Main;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ComCtrls, Vcl.ToolWin, Vcl.ExtDlgs,
  System.ImageList, Vcl.ImgList, System.Actions, Vcl.ActnList, Vcl.ExtCtrls,
  Vcl.StdCtrls, Vcl.Imaging.JPEG, Vcl.Imaging.pngimage,
  Azure.CognitiveServices.Core.Entities,
  Azure.CognitiveServices.ImageOcr.Types;

type

{ TMainForm }

  TMainForm = class(TForm)
    MainActionList: TActionList;
    LoadPhotoAction: TAction;
    OcrPhotoAction: TAction;
    ClearDataAction: TAction;
    AppExitAction: TAction;
    MainImageList: TImageList;
    PhotoOpenDialog: TOpenPictureDialog;
    MainToolbar: TToolBar;
    LoadPhotoButton: TToolButton;
    FindFaceButton: TToolButton;
    ClearDataButton: TToolButton;
    AppExitButton: TToolButton;
    ClientPanel: TPanel;
    PhotoScrollBox: TScrollBox;
    PhotoImage: TImage;
    Splitter1: TSplitter;
    TextMemo: TMemo;
    ToolButton1: TToolButton;
    OcrEnhancedButton: TToolButton;
    OcrEnhancedAction: TAction;
    procedure MainActionListUpdate(Action: TBasicAction; var Handled: Boolean);
    procedure AppExitActionExecute(Sender: TObject);
    procedure LoadPhotoActionExecute(Sender: TObject);
    procedure ClearDataActionExecute(Sender: TObject);
    procedure OcrPhotoActionExecute(Sender: TObject);
    procedure OcrEnhancedActionExecute(Sender: TObject);
  private
    FHasResponse: Boolean;
    FResponseData: TAcsImageOcrResponse;
    FPhotoData: TBytes;
    procedure ShowResponse();
  public
    constructor Create(AOwner: TComponent); override;
  end;

var
  MainForm: TMainForm;

implementation

{$R *.dfm}

uses
  System.IOUtils,
  Azure.CognitiveServices.ImageOcr.HTTP;

{ TMainForm }

constructor TMainForm.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  ShowResponse();
end;

procedure TMainForm.AppExitActionExecute(Sender: TObject);
begin
  Close();
end;

procedure TMainForm.ClearDataActionExecute(Sender: TObject);
begin
  FHasResponse := False;
  ShowResponse;
end;

procedure TMainForm.LoadPhotoActionExecute(Sender: TObject);
begin
  if not PhotoOpenDialog.Execute then
    Exit;

  FPhotoData := TFile.ReadAllBytes(PhotoOpenDialog.FileName);

  var LStream := TMemoryStream.Create;
  try
    LStream.Write(FPhotoData, Length(FPhotoData));
    LStream.Position := 0;
    PhotoImage.Picture.LoadFromStream(LStream);
  finally
    LStream.Free;
  end;

  PhotoImage.Left := 0;
  PhotoImage.Top := 0;
  PhotoImage.AutoSize := True;

  ClearDataAction.Execute;
end;

procedure TMainForm.MainActionListUpdate(Action: TBasicAction;
  var Handled: Boolean);
begin
  ClearDataAction.Enabled := FHasResponse;
  OcrPhotoAction.Enabled := Length(FPhotoData) > 0;
  OcrEnhancedAction.Enabled := OcrPhotoAction.Enabled;
  Handled := True;
end;

procedure TMainForm.OcrEnhancedActionExecute(Sender: TObject);
begin
  OcrEnhancedAction.Checked := not OcrEnhancedAction.Checked;
end;

procedure TMainForm.OcrPhotoActionExecute(Sender: TObject);
var
  LConfig: IAcsConfiguration;
  LBroker: TAcsImageOcrHttpBroker;
  LRequest: TAcsImageOcrRequest;
begin
  LConfig := LoadAcsConfiguration();

  with LRequest do
  begin
    CacheImage := False;
    Language := 'ita'; // eng
    Enhanced := OcrEnhancedAction.Checked;
    ImageData := FPhotoData;
    ImageType := 'image/jpeg';
  end;

  LBroker := TAcsImageOcrHttpBroker.Create(LConfig);
  try
    LBroker.Endpoint := 'https://???.cognitiveservices.azure.com/contentmoderator/moderate/v1.0/ProcessImage/OCR';
    FResponseData := LBroker.Execute(LRequest);
    FHasResponse := True;
  finally
    LBroker.Free;
  end;

  ShowResponse();
end;

procedure TMainForm.ShowResponse;
begin
  if not FHasResponse then
  begin
    TextMemo.Lines.Clear;
    Exit;
  end;

  TextMemo.Lines.Text := FResponseData.Text;
end;

end.
