Einzelnen Beitrag anzeigen

Rollo62

Registriert seit: 15. Mär 2007
3.926 Beiträge
 
Delphi 12 Athens
 
#5

AW: Fmx Mobile IFMXCameraService TakePhoto

  Alt 4. Nov 2015, 08:21
Hallo Sir Rufo,

dankesehr für die Erkenntnis, manchmal kommt man nur mit einem nil so richtig weiter
Trotzdem verstehe ich immer noch nicht wozu der Parameter überhaupt da ist.

Naja, ich habe hier man versucht den ganzen Kram zu vereinfachen
so das man für ein Foto einfach das hier aufrufen kann:

Code:
 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
                      );

Der Parameter Size funktioniert, aber scheint auf ein Maximum je nach Device begrenzt zu sein.
Kennt jemand den Trick wie man das Maximum vorher abfragen kann ?

UPDATE:
Ich haber gerade auf Android bei einem Galaxy S5 getestet
mit Size(500, 500) bekomme ich nur ein Bmp mit (230, 408) BitmapScale = 1 zurück.
Also auf iOS scheint (bis jetzt) Size zu stimmen, auf Android schonmal nicht.
Wie bekommt man das Ganze 1:1 hin auf allen Platformen ?


Hier die ganze Unit

Code:
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.
Rollo

Geändert von Rollo62 ( 4. Nov 2015 um 09:06 Uhr)
  Mit Zitat antworten Zitat