简体   繁体   中英

Getting error when trying to give parameters to constructor of JNI object while creating new object

I am working on an Android App in which i am using NDK, CPP, Opencv, and java. So, here i am getting error. In which i need to pass ArrayOf Mat objects from CPP to java using JNI method. My code is as follows.

jclass thisclass = env->GetObjectClass(instance);
jclass matclass = env->FindClass("org/opencv/core/Mat");
if (env->ExceptionOccurred())
    return NULL;

const char* constructorSignature = "(Lorg/opencv/core/Mat;Lorg/opencv/core/Rect;)V";
jmethodID constructor = env->GetMethodID(matclass, "<init>", constructorSignature);
if (env->ExceptionOccurred())
        return NULL;

As Shown above i have got Mat class and then created constructor signature for Mat class by Providing Mat and Rect as an argument as there is a constructor in Mat class which accepts Mat and Rect as an input and create Mat in return.

But after creating constructor when i try to create an object using that constructor i am getting error.

std::vector<Mat> selectedMats;
std::vector<Rect> selectedRects;
for(int h = 0; h < selectedRects.size(); h++){


        ls = &(selectedMats[h]);
        lsRect = &multiRectToCropOriginal;
        __android_log_print(ANDROID_LOG_ERROR, "TRACKERS", "%ld", (long)ls);
        // create new object
        jobject matObject = (jobject)env->NewObject(matclass, constructor, ls, lsRect);

        // put object into array
        env->SetObjectArrayElement(matArray , h, matObject);


    }

selectedMats is an vector of Mat which i have created and assigned data to.

Getting Error Like JNI DETECTED ERROR IN APPLICATION: use of invalid jobject 0xa32788f8

when passing ls and lsRect.

now, when i pass Mat and Rect as object it doesnt even compile and gives error as stated below.

code ->

Just changed from

jobject matObject = (jobject)env->NewObject(matclass, constructor, *ls, *lsRect);

Build command failed.
Error while executing process /home/karshsoni/Android/Sdk/cmake/3.6.4111459/bin/cmake with arguments {--build /home/karshsoni/Documents/CVTextDetection/app/.externalNativeBuild/cmake/debug/armeabi-v7a --target native-lib}
[1/2] Building CXX object CMakeFiles/native-lib.dir/native-lib.cpp.o
FAILED: /home/karshsoni/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++  --target=armv7-none-linux-androideabi21 --gcc-toolchain=/home/karshsoni/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64 --sysroot=/home/karshsoni/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/sysroot  -Dnative_lib_EXPORTS -I/home/karshsoni/Downloads/OpenCV/opencv-4.0.1-android-sdk/OpenCV-android-sdk/sdk/native/jni/include -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -mfpu=vfpv3-d16 -fno-addrsig -march=armv7-a -mthumb -Wa,--noexecstack -Wformat -Werror=format-security -stdlib=libc++  -std=gnu++11 -O0 -fno-limit-debug-info  -fPIC   -std=gnu++11 -MD -MT CMakeFiles/native-lib.dir/native-lib.cpp.o -MF CMakeFiles/native-lib.dir/native-lib.cpp.o.d -o CMakeFiles/native-lib.dir/native-lib.cpp.o -c /home/karshsoni/Documents/CVTextDetection/app/src/main/cpp/native-lib.cpp
/home/karshsoni/Documents/CVTextDetection/app/src/main/cpp/native-lib.cpp:268:76: error: cannot pass object of non-trivial type 'cv::Mat' through variadic method; call will abort at runtime [-Wnon-pod-varargs]
        jobject matObject = (jobject)env->NewObject(matclass, constructor, ls, lsRect);
                                                                           ^
/home/karshsoni/Documents/CVTextDetection/app/src/main/cpp/native-lib.cpp:268:81: error: cannot pass object of non-trivial type 'cv::Rect' (aka 'Rect_<int>') through variadic method; call will abort at runtime [-Wnon-pod-varargs]
        jobject matObject = (jobject)env->NewObject(matclass, constructor, ls, lsRect);
                                                                                ^
2 errors generated.
ninja: build stopped: subcommand failed.

Please do let me know if you have any idea about this or if found any error about this Thanks .

Your attempt to create the Mat object passes pointers to two C++ objects, where it should pass Java objects. The statement in question is:

jobject matObject = (jobject)env->NewObject(matclass, constructor, ls, lsRect);

Instead of passing the pointers ls and lsRect you need to use env->NewObject to create another Mat jobject and a Rect jobject; then pass those jobjects to your original call to NewObject.

You also need to set the fields of your Mat and Rect object. To do that, you'll have to get field IDs for them (see this StackOverflow answer) and use JNI functions like SetIntField, SetDoubleField, etc.

It's quite complicated, and I'd recommend getting a copy of The Java Native Interface by Sheng Liang, which has been very helpful to me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM