unit S4.Media.Photo;
interface
uses
System.Types, System.SysUtils
, FMX.Graphics
;
//
// Class for triggering the camera dialog and taking a picture from Camera
//
//
// Usage:
// TS4TakePhoto.Execute(TSize.Create(EditPhotoSize.Text.ToInteger,
// EditPhotoSize.Text.ToInteger),
// procedure (const bmpPhoto : TBitmap)
// begin
// Image1.Bitmap.Assign(bmpPhoto);
//
// TextPhotoResult.Text := 'Result W: ' + bmpPhoto.Width.ToString +
// ', H: ' + bmpPhoto.Height.ToString +
// Format(' Scale %1.4f', [bmpPhoto.BitmapScale])
// ;
//
// end,
// procedure
// begin
// ShowMessage('Take Photo was cancelled');
// end
// );
// or
//
// TS4TakePhoto.Execute(TSize.Create(EditPhotoSize.Text.ToInteger,
// EditPhotoSize.Text.ToInteger),
// procedure (const bmpPhoto : TBitmap)
// begin
// Image1.Bitmap.Assign(bmpPhoto);
//
// TextPhotoResult.Text := 'Result W: ' + bmpPhoto.Width.ToString +
// ', H: ' + bmpPhoto.Height.ToString +
// Format(' Scale %1.4f', [bmpPhoto.BitmapScale])
// ;
//
// end
// );
type
TS4TakePhotoResultProc = reference to procedure (const bmpPhoto : TBitmap);
TS4TakePhoto = class
private
class var _Single : TS4TakePhoto;
class var procResult : TS4TakePhotoResultProc;
class var procCancel : TProc;
procedure EvDidCancelTaking;
procedure EvDidFinishTaking(Image: TBitmap);
protected
class destructor Destroy;
public
destructor Destroy; override;
//
// sizeBmp defines the desired image size, but is limited to device maximum,
// bmp tries to BestFit into cx, cy with keeping the AspectRatio
//
// Returns True if TakePhoto triggered
// Returns False if CameraServiuce unavailable
//
class function Execute(const sizeBmp : TSize;
const prResult : TS4TakePhotoResultProc;
const prCancel : TProc = nil) : Boolean;
end;
implementation
Uses
FMX.MediaLibrary
, FMX.Platform
;
class destructor TS4TakePhoto.Destroy;
begin
FreeAndNil( _Single );
end;
destructor TS4TakePhoto.Destroy;
begin
inherited;
end;
procedure TS4TakePhoto.EvDidCancelTaking;
begin
if Assigned(procCancel) then
procCancel;
end;
procedure TS4TakePhoto.EvDidFinishTaking(Image: TBitmap);
begin
if Assigned(procResult) then
procResult( Image );
end;
class function TS4TakePhoto.Execute(const sizeBmp : TSize;
const prResult : TS4TakePhotoResultProc;
const prCancel : TProc) : Boolean;
var
ICameraService : IFMXCameraService;
parCam : TParamsPhotoQuery;
begin
if not Assigned(_Single) then
_Single := TS4TakePhoto.Create;
_Single.procResult := prResult;
_Single.procCancel := prCancel;
if TPlatformServices.Current.SupportsPlatformService(IFMXCameraService, ICameraService) then
begin
parCam.RequiredResolution := sizeBmp;
parCam.Editable := False;
parCam.NeedSaveToAlbum := False;
parCam.OnDidFinishTaking := _Single.EvDidFinishTaking;
parCam.OnDidCancelTaking := _Single.EvDidCancelTaking;
ICameraService.TakePhoto(nil, parCam);
ICameraService := nil;
Result := True;
end
else
begin
Result := False;// No CameraService available
end;
end;
end.