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(“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(“MANUFACTURER”);
But the first problem with the camera is still there and cant solve it
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.
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):
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).
This may be related to permissions, you must declare these permissions in your AndroidManifest.
See this link for more information (under Manifest Declarations): Camera API | Android media | Android Developers
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 (“sendSaidWord”);
}
catch(AndroidJavaException ex){
textMesh.text = ex.ToString ();
}
}
}
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;
}