Passing Camera.MAIN in Unity3D to android java method

白昼怎懂夜的黑 提交于 2020-01-16 19:12:30

问题


i'm working on integrating image recognition app using Moodstocks SDK , to start the scanner in moodstocks i must use a surfaceview (Camera) , all works fine when i do it in eclipse , but i want to use unity3D cause i'm making it in a sort of a game ,

so i made my eclipse project as JAR and imported it in unity and i'm trying to call the method in my java class from the unity script and pass the camera.Main to it

so if you can give me any guidelines about that

Thanks,


回答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 definition
  jclass cls_JavaClass = jni_env->FindClass("com/your/java/Class"); 
  // find constructor method        
  jmethodID mid_JavaClass = jni_env->GetMethodID (cls_JavaClass, "<init>",  "()V");     
  // create object instance
  jobject obj_JavaClass = jni_env->NewObject(cls_JavaClass, mid_JavaClass);     
  // return object with a global reference
  return 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.




回答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



来源:https://stackoverflow.com/questions/15855983/passing-camera-main-in-unity3d-to-android-java-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!