Whenever i try this: AndroidJavaObject jo = new AndroidJavaObject("android.hardware.camera2"); or this; AndroidJavaClass jc = new AndroidJavaClass("android.hardware.camera2.CameraDevice"); I get a class not found exception error in logcat. How do i get direct Access to android camera and its parameters from Unity?
Well i decided to check one of the tiny scripts in the documents. I tried this: AndroidJavaClass jc = new AndroidJavaClass("android.os.Build"); string manufacturer = jc.Get<string>("MANUFACTURER"); and here is the logcat message; It seems to be there on the android documents. What might be wrong?
Oh the problem with the documents is that its poorly written. MANUFACTURER is a static variable, so it should be; string manufacturer = jc.GetStatic<string>("MANUFACTURER"); But the first problem with the camera is still there and cant solve it
The MANUFACTURER field is a static field, hence you should access it like this: Code (CSharp): AndroidJavaClass jc = new AndroidJavaClass("android.os.Build"); string manufacturer = jc.GetStatic<string>("MANUFACTURER");
Regarding the camera issues, you are trying to call this code: Code (CSharp): AndroidJavaObject jo = new AndroidJavaObject("android.hardware.camera2"); which is like the equivalent of calling the camera2 constructor with no arguments. This is a package, and not a class; that's why you're probably getting those errors. What would you like to achieve with the camera? see the documentation here: http://developer.android.com/reference/android/hardware/camera2/package-summary.html
Well my ultimate goal is to be able to alter CONTROL_AWB_MODE_TWILIGHT from unity somehow. How should i proceed?
Its in the CameraCharacteristics class, first i need to get an instance for the; And then get CameraCharacteristics with
This code does what you want: Code (CSharp): using (var player = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) { using (var activity = player.GetStatic<AndroidJavaObject>("currentActivity")) { var cameraManager = activity.Call<AndroidJavaObject>("getSystemService", "camera"); AndroidJavaObject cameraCharacteristics = cameraManager.Call<AndroidJavaObject>("getCameraCharacteristics", cameraId); // Do what you need here ... } }
I will try this code tonight to see how it works, thank you for the code. I just didnt understand the relation between currentActivity of Unity and the Context class of android.
On Android, Activity is derived from Context. Unity has a class called UnityPlayer that gets assigned the game's activity. When the game starts, something like this happens (really simplified code, just to illustrate the point): Code (CSharp): public class UnityPlayerActivity { @Override protected void onCreate(Bundle savedInstanceState) { // keep a static reference to the game's activity UnityPlayer.currentActivity = this; } } This makes it easier for you to grab the activity from C# code (like the code i showed above).
Well, i tried the code. And the below line gives errors on logcat: var cameraManager = activity.Call<AndroidJavaObject>("getSystemService", "camera"); Here is the error:
This may be related to permissions, you must declare these permissions in your AndroidManifest. See this link for more information (under Manifest Declarations): http://developer.android.com/guide/topics/media/camera.html
I have a problem using AndroidJavaClass It always Gives a JavaClassNotFoundException Here is my C# code: public class TryVoice : MonoBehaviour { public Text textMesh; // Use this for initialization public void GetTextFromVoice(){ try{ var plugin = new AndroidJavaClass ("thesis.plugin.voiceRecognition"); textMesh.text = plugin.Call<string> ("sendSaidWord"); } catch(AndroidJavaException ex){ textMesh.text = ex.ToString (); } } } And my AndroidJavaClass: import android.support.v7.app.AppCompatActivity; import thesis.plugin.utils.ConversionCallaback; import android.widget.Toast; public class voiceRecognition extends AppCompatActivity implements ConversionCallaback { public static String SaidWord = ""; private static final int STT = 1; private static int CURRENT_MODE = -1; public String sendSaidWord() { doSendSome(); return SaidWord; } public void doSendSome() { Toast.makeText(this, "Looking For Voice" , Toast.LENGTH_SHORT).show(); TranslatorFactory.getInstance().getTranslator(TranslatorFactory.TRANSLATOR_TYPE.SPEECH_TO_TEXT, voiceRecognition.this) .initialize("Hello There", voiceRecognition.this); CURRENT_MODE = STT; } @override public void onSuccess(String result) { switch (CURRENT_MODE) { case STT: SaidWord = result; } Toast.makeText(this, "Result " + result, Toast.LENGTH_SHORT).show(); } @override public void onCompletion() { Toast.makeText(this, "Done ", Toast.LENGTH_SHORT).show(); } @override public void onErrorOccured(String errorMessage) { Toast.makeText(this, "Error " + errorMessage, Toast.LENGTH_SHORT).show(); } }
Hi, were you able to figure out about this issue?? Please let me know if you've resolved it by any chance.