Passing Camera.main In Unity3d To Android Java Method
Solution 1:
There are several ways to create a Java plugin but the result in each case is that you end up with a .jar file containing the .class files for your plugin. One approach is to download the JDK, then compile your .java files from the command line with javac. This will create .class files which you can then package into a .jar with the jar command line tool. Another option is to use the Eclipse IDE together with the ADT.
Once you have built your Java plugin (.jar) you should copy it to the Assets->Plugins->Android
folder in the Unity project. Unity will package your .class files together with the rest of the Java code and then access the code using the Java Native Interface (JNI). JNI is used both when calling native code from Java and when interacting with Java (or the JavaVM) from native code.
To find your Java code from the native side you need access to the Java VM. Fortunately, that access can be obtained easily by adding a function like this to your C/C++ code:
jint JNI_OnLoad(JavaVM* vm, void* reserved) {
JNIEnv* jni_env = 0;
vm->AttachCurrentThread(&jni_env, 0);
}
This is all that is needed to start using Java from C/C++. It is beyond the scope of this document to explain JNI completely. However, using it usually involves finding the class definition, resolving the constructor () method and creating a new object instance, as shown in this example:-
jobject createJavaObject(JNIEnv* jni_env) {
// find class definitionjclasscls_JavaClass= jni_env->FindClass("com/your/java/Class");
// find constructor method jmethodIDmid_JavaClass= jni_env->GetMethodID (cls_JavaClass, "<init>", "()V");
// create object instancejobjectobj_JavaClass= jni_env->NewObject(cls_JavaClass, mid_JavaClass);
// return object with a global referencereturn jni_env->NewGlobalRef(obj_JavaClass);
}
This explanation comes from this information page, where a few examples are written as well. You should take a look over here! This may be worth a read as well.
Solution 2:
Disclaimer: I work for Moodstocks.
The ScannerSession
object in the Moodstocks SDK for Android is designed to be a high-level, easy-to-use wrapper that takes care of many "technical" difficulties by itself, in the context of a classic, Java app. In particular, it initializes the camera for you, previews it on the provided SurfaceView
and dispatches camera frames to the Moodstocks SDK.
I've never used Unity myself so I can't dive into the details, but I think that in your context, given the fact that Unity has its own way of initializing and using the camera, you'll have to bypass this ScannerSession
object and hit lower-level functions of the Moodstocks SDK. Find how to get the camera frames using Unity, and feed them manually to the Moodstocks SDK Scanner
object. You can take inspiration from what is done in the ScannerSession
to see how to do that!
Hope this helps! If you're looking for more advice, you can ask us questions on the Moodstocks Help Center
Post a Comment for "Passing Camera.main In Unity3d To Android Java Method"