- Home /
Unity to Android: passing CameraCharacteristics.Key values from Unity to Android cameraCharacteristics.get?
I'm trying to use the camera2 api from my Unity app via the safe (but slow, I know) AndroidJavaObject method.
I've worked out how to get to the camera manager, and how to get to the camera characteristics object/class, but to use the functionality you have to pass a Key, namely a CameraCharacteristics.NAME OF KEY HERE...
I learned that you can use $ on the end when trying to get classes that are enums, but that doesn't seem to work here. I'd rather not try to just fudge it and pass some int value that I scrounge up from the java source (if it's even possible to get that value), because for one thing, I'm not sure that would even work since the java side is expecting a CameraCharacteristics.Key
Does anyone have any ideas how to pass something like this?
Thanks,
Cynthia
//camera2 (21+ version) class retrieval method (first get unity activity, as currentactivity, then...
AndroidJavaObject camera2Mgr = currentActivity.Call<AndroidJavaObject>("getSystemService", "camera");
if (camera2Mgr != null) { //use try catch instead, but for testing, check nullity
//get cameracharacteristics on chosen camera device (id) using the manager
//camera id already chosen earlier and passed in as parameter
String[] cameraList = camera2Mgr.Call<String[]>("getCameraIdList");
AndroidJavaObject cameraCharacteristics = camera2Mgr.Call<AndroidJavaObject>("getCameraCharacteristics", cameraList[0]); //cameraId);
// now need to call... cameraCharacteristics.Call<float[]>("get",
CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL);
//how do I pass this key?
//tried creating the key as a class, with both $KEY and $INFO_SUPPORTED_HARDWARE_LEVEL
//but get class not found exception...so that doesn't seem to be the way to get at the right key needed
AndroidJavaClass cchl = new AndroidJavaClass("android.hardware.camera2.CameraCharacteristics$KEY");
Still interested if anyone has found a way to do this. I tried every combo I could think of, but you must pass a $$anonymous$$ey to the call. And no, you can't just getstatic on the fields (the keys), since the class apparently has disallowed that (nosuchfield error).
The workaround I'm using is writing some Java (pure class .JAR) methods of a custom class that takes the cameraCharacteristics as a parameter then makes the .get call. Only problem is that you have to put a try/catch at the Unity level in case of failure, since a no such field error on a key will happen at the cameraCharacteristics class level, not my class which only calls the .get on that class.
Yes, another option would be to put all the java code into a javaclass, so that the cameraCharacteristics class is obtained within my custom class. That might make the exception happen at the custom class level but then again, I haven't tried it, so it still might happen at the Unity JNI/Object level. I only include it as an idea to those that want to contain the entire set of java calls into their own class and avoid a lot of use of the AndroidJavaClass.
I want to do most things within C#, so that's why I'm keeping most of my code in C#. This code isn't called much, just setup info, so it doesn't really affect performance that much.
Answer by rpasquini · Feb 15, 2018 at 10:27 PM
I was able to get this to work in C#. Here's a sample:
AndroidJavaObject key = cameraCharacteristics.GetStatic<AndroidJavaObject>("SENSOR_INFO_PHYSICAL_SIZE");
AndroidJavaObject sizef = cameraCharacteristics.Call<AndroidJavaObject>("get", key);
float height = sizef.Call<float>("getHeight");
float width = sizef.Call<float>("getWidth");
Hope this helps!
Your answer
Follow this Question
Related Questions
I want to know how to access an external USB camera connected to my Android smartphone. 0 Answers
Unity can't detect SDK tools thougt I've installed them! 0 Answers
How to get Android 10 (API Level 29) 1 Answer
AndroidApiLevel30 ? 0 Answers
unity Exception:JNI:Init'd AndroidJavaObject with null prt! 0 Answers