我试图在Delphi 10.2 Tokyo中创建一个使用Camera2 API的Android应用程序。我使用Java2OP来创建我需要的接口。其中一些接口用于回调/监听器。例如,Java2OP已将其转换为CameraDevice.StateCallback:
{2,}
我理解我应该将JCameraDevice_StateCallbackClass = interface(JObjectClass)
['{3F5A7394-FD15-439C-9BFB-DF8D43F9F930}']
{class} function _GetERROR_CAMERA_DEVICE: Integer; cdecl;
{class} function _GetERROR_CAMERA_DISABLED: Integer; cdecl;
{class} function _GetERROR_CAMERA_IN_USE: Integer; cdecl;
{class} function _GetERROR_CAMERA_SERVICE: Integer; cdecl;
{class} function _GetERROR_MAX_CAMERAS_IN_USE: Integer; cdecl;
{class} function init: JCameraDevice_StateCallback; cdecl;
{class} property ERROR_CAMERA_DEVICE: Integer read _GetERROR_CAMERA_DEVICE;
{class} property ERROR_CAMERA_DISABLED: Integer read _GetERROR_CAMERA_DISABLED;
{class} property ERROR_CAMERA_IN_USE: Integer read _GetERROR_CAMERA_IN_USE;
{class} property ERROR_CAMERA_SERVICE: Integer read _GetERROR_CAMERA_SERVICE;
{class} property ERROR_MAX_CAMERAS_IN_USE: Integer read _GetERROR_MAX_CAMERAS_IN_USE;
end;
[JavaSignature('android/hardware/camera2/CameraDevice$StateCallback')]
JCameraDevice_StateCallback = interface(JObject)
['{3A3944F5-A71F-4CD6-98C6-04B8D65C3B52}']
procedure onClosed(camera: JCameraDevice); cdecl;//Deprecated
procedure onDisconnected(camera: JCameraDevice); cdecl;//Deprecated
procedure onError(camera: JCameraDevice; error: Integer); cdecl;//Deprecated
procedure onOpened(camera: JCameraDevice); cdecl;//Deprecated
end;
TJCameraDevice_StateCallback = class(TJavaGenericImport<JCameraDevice_StateCallbackClass, JCameraDevice_StateCallback>) end;
与我想用作回调/监听器的接口(在本例中为TJavaLocal
)结合起来。这是我做的:
单位CamDevStateCallback;
JCameraDevice_StateCallbackClass
此代码无法编译,因为它抱怨interface
uses
Androidapi.JNIBridge, android.hardware.camera2;
type
TCamera2Event = procedure(camera: JCameraDevice) of object;
TCamera2ErrorEvent = procedure(camera: JCameraDevice; error: Integer) of object;
TCamDevStateCallback = class(TJavaLocal, JCameraDevice_StateCallback)
protected
FOnClosed: TCamera2Event;
FOnDisconnected: TCamera2Event;
FOnError: TCamera2ErrorEvent;
FOnOpen: TCamera2Event;
public
procedure onClosed(camera: JCameraDevice); cdecl;
procedure onDisconnected(camera: JCameraDevice); cdecl;
procedure onError(camera: JCameraDevice; error: Integer); cdecl;
procedure onOpened(camera: JCameraDevice); cdecl;
class function CreateNew(aOnOpen, aOnClosed, aOnDisconnected: TCamera2Event; aOnError: TCamera2ErrorEvent): JCameraDevice_StateCallback;
property OnCameraClosed: TCamera2Event read FOnClosed write FOnClosed;
property OnCameraDisconnected: TCamera2Event read FOnDisconnected write FOnDisconnected;
property OnCameraError: TCamera2ErrorEvent read FOnError write FOnError;
property OnCameraOpen: TCamera2Event read FOnOpen write FOnOpen;
end;
implementation
{ TCamDevStateCallback }
class function TCamDevStateCallback.CreateNew(aOnOpen, aOnClosed, aOnDisconnected: TCamera2Event;
aOnError: TCamera2ErrorEvent): JCameraDevice_StateCallback;
var
tmpObj: TCamDevStateCallback;
begin
tmpObj := TCamDevStateCallback.Create;
tmpObj.OnCameraClosed := aOnClosed;
tmpObj.OnCameraDisconnected := aOnDisconnected;
tmpObj.OnCameraError := aOnError;
tmpObj.OnCameraOpen := aOnOpen;
Result := TJCameraDevice_StateCallback.Wrap((tmpObj as ILocalObject).GetObjectID);
end;
procedure TCamDevStateCallback.onClosed(camera: JCameraDevice);
begin
if Assigned(FOnClosed) then
FOnClosed(camera);
end;
procedure TCamDevStateCallback.onDisconnected(camera: JCameraDevice);
begin
if Assigned(FOnDisconnected) then
FOnDisconnected(camera);
end;
procedure TCamDevStateCallback.onError(camera: JCameraDevice; error: Integer);
begin
if Assigned(FOnError) then
FOnError(camera, error);
end;
procedure TCamDevStateCallback.onOpened(camera: JCameraDevice);
begin
if Assigned(FOnOpen) then
FOnOpen(camera);
end;
end.
,equals
与之前的声明和toString
,getClass
,notify
和{{1}不同失踪。
android.hardware.camera2.pas 由Java2OP创建,包含notifyAll
及更多的接口定义。
答案 0 :(得分:2)
这里的潜在问题,正如@DaveNottage所暗示的那样,CameraDevice.StateCallback
不是Java监听器接口,而是Java回调类(尽管是一个抽象类,在某些方面因此就像一个接口,但不是是一个界面。
因为它不是Java接口,所以不是用TJavaLocal
实现的候选者,尽管在Delphi中它是由接口表示的事实(因为所有Java接口和类都是。)。
TJavaLocal
用于实现Java接口,通常是侦听器接口。它没有提供从Java类(甚至是抽象的Java类)继承的方法。
线索是Java2OP创建的重要接口继承自JObject
,表明它是Java基于Object
的类层次结构的一部分。导入时的侦听器接口继承自IJavaInstance
。
Delphi不支持从Android Java类继承(与其支持Delphi 支持从Objective-C类继承的iOS支持相比,这要归功于Objective-C是一个与Android管理不同的本机系统Java VM系统)