Accessing properties of classes wrapped with "DelphiInterface"

177 Views Asked by At

I have a FMX project in C++Builder 10.2 Tokyo that is targeted for Android only. I am trying to make use of the Camera2 API. I used the Java2Pas tool to create the Delphi interfaces and classes that I need, combining them into a single Pascal file which I have added to my C++ project.

Parts of this pascal file look like this:

  JTextureView_SurfaceTextureListenerClass = interface(JObjectClass)
    ['{106DB13E-C1B4-4898-B906-0143D97C0075}']
    function onSurfaceTextureDestroyed(JSurfaceTextureparam0 : JSurfaceTexture) : boolean; cdecl;// (Landroid/graphics/SurfaceTexture;)Z A: $401
    procedure onSurfaceTextureAvailable(JSurfaceTextureparam0 : JSurfaceTexture; Integerparam1 : Integer; Integerparam2 : Integer) ; cdecl;// (Landroid/graphics/SurfaceTexture;II)V A: $401
    procedure onSurfaceTextureSizeChanged(JSurfaceTextureparam0 : JSurfaceTexture; Integerparam1 : Integer; Integerparam2 : Integer) ; cdecl;// (Landroid/graphics/SurfaceTexture;II)V A: $401
    procedure onSurfaceTextureUpdated(JSurfaceTextureparam0 : JSurfaceTexture) ; cdecl;// (Landroid/graphics/SurfaceTexture;)V A: $401
  end;

  [JavaSignature('android/view/TextureView_SurfaceTextureListener')]
  JTextureView_SurfaceTextureListener = interface(JObject)
    ['{58A7FBD1-27B9-44AC-B013-F077E1BF5975}']
    function onSurfaceTextureDestroyed(JSurfaceTextureparam0 : JSurfaceTexture) : boolean; cdecl;// (Landroid/graphics/SurfaceTexture;)Z A: $401
    procedure onSurfaceTextureAvailable(JSurfaceTextureparam0 : JSurfaceTexture; Integerparam1 : Integer; Integerparam2 : Integer) ; cdecl;// (Landroid/graphics/SurfaceTexture;II)V A: $401
    procedure onSurfaceTextureSizeChanged(JSurfaceTextureparam0 : JSurfaceTexture; Integerparam1 : Integer; Integerparam2 : Integer) ; cdecl;// (Landroid/graphics/SurfaceTexture;II)V A: $401
    procedure onSurfaceTextureUpdated(JSurfaceTextureparam0 : JSurfaceTexture) ; cdecl;// (Landroid/graphics/SurfaceTexture;)V A: $401
  end;

  TJTextureView_SurfaceTextureListener = class(TJavaGenericImport<JTextureView_SurfaceTextureListenerClass, JTextureView_SurfaceTextureListener>)
  end;

The TJTextureView_SurfaceTextureListener interface needs to be used as a callback object, so I added some event handlers to it and changed it to look like this:

  TOnSurfaceTextureDestroyed = function(aSurface: JSurfaceTexture): Boolean of object;
  TOnSurfaceTextureUpdated = procedure(aSurface: JSurfaceTexture) of object;
  TOnSurfaceTextureSize = procedure(aSurface: JSurfaceTexture; aWidth:Integer; aHeight: Integer)  of object;

  TJTextureView_SurfaceTextureListener = class(TJavaGenericImport<JTextureView_SurfaceTextureListenerClass, JTextureView_SurfaceTextureListener>)
  protected
    FOnTextureDestroyed: TOnSurfaceTextureDestroyed;
    FOnTextureUpdated: TOnSurfaceTextureUpdated;
    FOnTextureAvailable: TOnSurfaceTextureSize;
    FOnTextureSizeChanged: TOnSurfaceTextureSize;

  public
    function onSurfaceTextureDestroyed(aSurface : JSurfaceTexture) : boolean; cdecl;
    procedure onSurfaceTextureAvailable(aSurface : JSurfaceTexture; aWidth : Integer; aHeight : Integer) ; cdecl;
    procedure onSurfaceTextureSizeChanged(aSurface : JSurfaceTexture; aWidth : Integer; aHeight : Integer) ; cdecl;
    procedure onSurfaceTextureUpdated(aSurface : JSurfaceTexture) ; cdecl;

    property OnTextureDestroyed: TOnSurfaceTextureDestroyed read FOnTextureDestroyed write FOnTextureDestroyed;
    property OnTextureUpdated: TOnSurfaceTextureUpdated read FOnTextureUpdated write FOnTextureUpdated;
    property OnTextureAvailable: TOnSurfaceTextureSize read FOnTextureAvailable write FOnTextureAvailable;
    property OnTextureSizeChanged: TOnSurfaceTextureSize read FOnTextureSizeChanged write FOnTextureSizeChanged;
  end;

implementation

{ TJTextureView_SurfaceTextureListener }

procedure TJTextureView_SurfaceTextureListener.onSurfaceTextureAvailable(aSurface: JSurfaceTexture; aWidth, aHeight: Integer);
begin
  if Assigned(FOnTextureAvailable) then
    FOnTextureAvailable(aSurface, aWidth, aHeight);
end;

function TJTextureView_SurfaceTextureListener.onSurfaceTextureDestroyed(aSurface: JSurfaceTexture): boolean;
begin
  if Assigned(FOnTextureDestroyed) then
    Result := FOnTextureDestroyed(aSurface)
  else
    Result := False;
end;

procedure TJTextureView_SurfaceTextureListener.onSurfaceTextureSizeChanged(aSurface: JSurfaceTexture; aWidth, aHeight: Integer);
begin
  if Assigned(FOnTextureSizeChanged) then
    FOnTextureSizeChanged(aSurface, aWidth, aHeight);
end;

procedure TJTextureView_SurfaceTextureListener.onSurfaceTextureUpdated(aSurface: JSurfaceTexture);
begin
  if Assigned(FOnTextureUpdated) then
    FOnTextureUpdated(aSurface);
end;

The IDE created the Header file that looks like this:

__interface JTextureView_SurfaceTextureListenerClass;
typedef System::DelphiInterface<JTextureView_SurfaceTextureListenerClass> _di_JTextureView_SurfaceTextureListenerClass;
__interface JTextureView_SurfaceTextureListener;
typedef System::DelphiInterface<JTextureView_SurfaceTextureListener> _di_JTextureView_SurfaceTextureListener;
class DELPHICLASS TJTextureView_SurfaceTextureListener;

__interface  INTERFACE_UUID("{106DB13E-C1B4-4898-B906-0143D97C0075}") JTextureView_SurfaceTextureListenerClass  : public Androidapi::Jni::Javatypes::JObjectClass 
{
    virtual bool __cdecl onSurfaceTextureDestroyed(Androidapi::Jni::Graphicscontentviewtext::_di_JSurfaceTexture JSurfaceTextureparam0) = 0 ;
    virtual void __cdecl onSurfaceTextureAvailable(Androidapi::Jni::Graphicscontentviewtext::_di_JSurfaceTexture JSurfaceTextureparam0, int Integerparam1, int Integerparam2) = 0 ;
    virtual void __cdecl onSurfaceTextureSizeChanged(Androidapi::Jni::Graphicscontentviewtext::_di_JSurfaceTexture JSurfaceTextureparam0, int Integerparam1, int Integerparam2) = 0 ;
    virtual void __cdecl onSurfaceTextureUpdated(Androidapi::Jni::Graphicscontentviewtext::_di_JSurfaceTexture JSurfaceTextureparam0) = 0 ;
};

__interface  INTERFACE_UUID("{58A7FBD1-27B9-44AC-B013-F077E1BF5975}") JTextureView_SurfaceTextureListener  : public Androidapi::Jni::Javatypes::JObject 
{
    virtual bool __cdecl onSurfaceTextureDestroyed(Androidapi::Jni::Graphicscontentviewtext::_di_JSurfaceTexture JSurfaceTextureparam0) = 0 ;
    virtual void __cdecl onSurfaceTextureAvailable(Androidapi::Jni::Graphicscontentviewtext::_di_JSurfaceTexture JSurfaceTextureparam0, int Integerparam1, int Integerparam2) = 0 ;
    virtual void __cdecl onSurfaceTextureSizeChanged(Androidapi::Jni::Graphicscontentviewtext::_di_JSurfaceTexture JSurfaceTextureparam0, int Integerparam1, int Integerparam2) = 0 ;
    virtual void __cdecl onSurfaceTextureUpdated(Androidapi::Jni::Graphicscontentviewtext::_di_JSurfaceTexture JSurfaceTextureparam0) = 0 ;
};

typedef bool __fastcall (__closure *TOnSurfaceTextureDestroyed)(Androidapi::Jni::Graphicscontentviewtext::_di_JSurfaceTexture aSurface);
typedef void __fastcall (__closure *TOnSurfaceTextureUpdated)(Androidapi::Jni::Graphicscontentviewtext::_di_JSurfaceTexture aSurface);
typedef void __fastcall (__closure *TOnSurfaceTextureSize)(Androidapi::Jni::Graphicscontentviewtext::_di_JSurfaceTexture aSurface, int aWidth, int aHeight);

class PASCALIMPLEMENTATION TJTextureView_SurfaceTextureListener : public Androidapi::Jnibridge::TJavaGenericImport__2<_di_JTextureView_SurfaceTextureListenerClass,_di_JTextureView_SurfaceTextureListener> 
{
    typedef Androidapi::Jnibridge::TJavaGenericImport__2<_di_JTextureView_SurfaceTextureListenerClass,_di_JTextureView_SurfaceTextureListener>  inherited;

protected:
    TOnSurfaceTextureDestroyed FOnTextureDestroyed;
    TOnSurfaceTextureUpdated FOnTextureUpdated;
    TOnSurfaceTextureSize FOnTextureAvailable;
    TOnSurfaceTextureSize FOnTextureSizeChanged;

public:
    bool __cdecl onSurfaceTextureDestroyed(Androidapi::Jni::Graphicscontentviewtext::_di_JSurfaceTexture aSurface);
    void __cdecl onSurfaceTextureAvailable(Androidapi::Jni::Graphicscontentviewtext::_di_JSurfaceTexture aSurface, int aWidth, int aHeight);
    void __cdecl onSurfaceTextureSizeChanged(Androidapi::Jni::Graphicscontentviewtext::_di_JSurfaceTexture aSurface, int aWidth, int aHeight);
    void __cdecl onSurfaceTextureUpdated(Androidapi::Jni::Graphicscontentviewtext::_di_JSurfaceTexture aSurface);
    __property TOnSurfaceTextureDestroyed OnTextureDestroyed = {read=FOnTextureDestroyed, write=FOnTextureDestroyed};
    __property TOnSurfaceTextureUpdated OnTextureUpdated = {read=FOnTextureUpdated, write=FOnTextureUpdated};
    __property TOnSurfaceTextureSize OnTextureAvailable = {read=FOnTextureAvailable, write=FOnTextureAvailable};
    __property TOnSurfaceTextureSize OnTextureSizeChanged = {read=FOnTextureSizeChanged, write=FOnTextureSizeChanged};
public:
    /* TObject.Create */ inline __fastcall TJTextureView_SurfaceTextureListener(void) : Androidapi::Jnibridge::TJavaGenericImport__2<_di_JTextureView_SurfaceTextureListenerClass,_di_JTextureView_SurfaceTextureListener> () { }
    /* TObject.Destroy */ inline __fastcall virtual ~TJTextureView_SurfaceTextureListener(void) { }

};

Inside my C++ code, I am trying to use it like this:

_di_JTextureView_SurfaceTextureListener = TJTextureView_SurfaceTextureListener::JavaClass->init();
SurfaceTextureListener->OnTextureAvailable = SetupCamera;

But the compiler gives me an error:

no member named 'OnTextureAvailable' in 'Androidcamera2::JTextureView_SurfaceTextureListener'

Can anyone tell me what I am doing wrong here?

1

There are 1 best solutions below

4
On

You're getting the error message because the JTextureView_SurfaceTextureListener interface does not have the OnTextureAvailable property.

The idea here is that the JTextureView_SurfaceTextureListener interface is the callback, and you should implement the JTextureView_SurfaceTextureListener interface in your own object. Then, in your onSurfaceTextureAvailable implementation, you call SetupCamera.

Note that you'll have to call TextureView.setSurfaceTextureListener and pass an instance of your JTextureView_SurfaceTextureListener instance in order to register your listener.