Android class not found exception

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 :frowning:

The MANUFACTURER field is a static field, hence you should access it like this:

AndroidJavaClass jc = new AndroidJavaClass("android.os.Build");
string manufacturer = jc.GetStatic<string>("MANUFACTURER");

Regarding the camera issues, you are trying to call this code:

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: android.hardware.camera2  |  Android Developers

Well my ultimate goal is to be able to alter CONTROL_AWB_MODE_TWILIGHT from unity somehow.
How should i proceed?

How is it done on Android? do you have any reference code ?

Its in the CameraCharacteristics class, first i need to get an instance for the;

And then get CameraCharacteristics with [quote]
getCameraCharacteristics(String cameraId)
[/quote]

1 Like

This will only get the characteristics. Do you want to set them as well ?

yep

This code does what you want:

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 ...
    }
}
1 Like

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):

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).

I see, now its all clear. Thank you :slight_smile:

Well, i tried the code. And the below line gives errors on logcat:
var cameraManager = activity.Call(“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): Camera API  |  Android media  |  Android Developers

Thanks for this code, it was very helpful!

1 Like

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 ();
}
}
}

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;
}

@
public void onSuccess(String result) {
switch (CURRENT_MODE) {
case STT:
SaidWord = result;
}
Toast.makeText(this, "Result " + result, Toast.LENGTH_SHORT).show();
}

@
public void onCompletion() {
Toast.makeText(this, "Done ", Toast.LENGTH_SHORT).show();
}

@
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.