问题描述
我正在尝试在 Unity 中使用 Android 手机的摄像头.我需要将 CameraDevice.StateCallback
传递给 CameraManager.openCamera
方法,我是这样构造的:
I'm trying to use an Android phone's camera in Unity. I need to pass a CameraDevice.StateCallback
to the CameraManager.openCamera
method, which I have constructed as such:
public class OpenCameraCallbackProxy : AndroidJavaProxy
{
public OpenCameraCallbackProxy() : base("android.hardware.camera2.CameraDevice$StateCallback")
{
}
public void onOpened(AndroidJavaObject cameraDevice)
{
// etc...
}
}
调用openCamera方法时(cameraManager
是AndroidJavaObject
的一个实例):
When calling the openCamera method (cameraManager
is an instance of AndroidJavaObject
):
cameraManager.Call("openCamera", cameraId, new OpenCameraCallbackProxy(), null);
我收到以下异常:
java.lang.IllegalArgumentException: class android.hardware.camera2.CameraDevice$StateCallback 不是接口
.
我还在我的应用程序的其他地方使用了一个专有的 Java 库,它使用了回调,并且这个方法(使用 AndroidJavaProxy
)可以正常工作.我猜必须检查 openCamera
如果不是 callback.getClass().isInterface()
或类似的东西.有什么办法可以解决这个问题吗?
I am also using a proprietary Java library elsewhere in my application which uses callbacks, and this method (using AndroidJavaProxy
) works properly. I'm guessing that there must be a check in openCamera
to throw if not callback.getClass().isInterface()
or something like that. Is there any way I could get around this?
提前致谢.
推荐答案
AndroidJavaProxy 的构造函数需要为回调实现接口的完全限定名称字符串.想必这是因为传回给你的对象是运行时构造的类,所以它的签名在编译时是不可用的,但是构造运行时对象的代码可以实现给定的接口(如何?我不知道,除了它会使用 ASM,但这就像说我不"不知道他们是如何制作那个梳妆台的,但可能涉及到锯子").
AndroidJavaProxy's constructor requires the fully qualified name string of the interface to be implemented for the callback. Presumably this is because the object passed back to you is a runtime constructed class, so its signature is not available at compile time, but that the code that constructs the runtime object can implement a given interface (how? I have no idea, other than it would use ASM, but that's like saying "I don't know how they made that dresser, but a saw was probably involved").
但是,CameraDevice.StateCallback 不是一个接口,它是一个抽象类.
However, CameraDevice.StateCallback is not an interface, it is an abstract class.
使用 AndroidJavaProxy 的 Unity 示例使用 DatePickerDialog$OnDateSetListener 回调接口,我们可以看到其实是一个接口.
The Unity example for using the AndroidJavaProxy uses a DatePickerDialog$OnDateSetListener callback interface, which we can see is actually an interface.
我不确定你应该做什么(可能子类化 CameraDevice.StateCallback
?这就是 这个人 做到了),因为我从来没有尝试过这样做,但这就是为什么你会得到你所得到的错误.
I am not sure what you should be doing instead (possibly subclassing CameraDevice.StateCallback
? That's what this guy did) as I've never tried to do this, but that's why you're getting the error your getting.
这篇关于AndroidJavaProxy 不是接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!