Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass, return and convert to vectors list of lists over JNI

I need to pass from Java

List< List<MyPoint> > points;

over jni to C++ and convert to

std::vector< std::vector<MyPoint> >

Process this vectors and return

List< List<MyPoint> >
  1. How correct pass and return list of lists?
  2. How convert list of lists of objects in vector of vectors of objects and backward?
like image 787
George Avatar asked May 30 '12 09:05

George


2 Answers

As i understand it from the reference the JNI, JNI can only work with one-dimensional arrays of primitive types or objects.

Because on the side of Java, had to translate the list into an array. Then, in the native part the array passed and the number of elements. There's going to the desired vector and processed. Returns as a result of two arrays (array with points all contours and the array with the number of points in each contour) and the number of contours. The resulting array is collected in a list of lists on the side of Java.

While the problem is not solved completely, because the JNI can not allocate memory for an existing item in the native part. Therefore it is necessary to extract the data in part, to allocate memory for them on the side of Java, and fill in the native.

A possible resolve may be the use of binders such as SWIG or JavaCpp

like image 82
George Avatar answered Nov 07 '22 00:11

George


JNIEXPORT jobjectArray JNICALL Java_ProcessInformation_getAllProcessPid  (JNIEnv*env,jobject obj) {

    vector<string>vec;

    vec.push_back("Ranjan.B.M");

    vec.push_back("Mithun.V");

    vec.push_back("Preetham.S.N");

    vec.push_back("Karthik.S.G");

    cout<<vec[0];

    cout<<vec[0];

    jclass clazz = (env)->FindClass("java/lang/String");

    jobjectArray objarray = (env)->NewObjectArray(vec.size() ,clazz ,0);

    for(int i = 0; i < vec.size(); i++) {

        string s = vec[i]; 

         cout<<vec[i]<<endl;

         jstring js = (env)->NewStringUTF(s.c_str());

        (env)->SetObjectArrayElement(objarray , i , js);

    }

    return objarray;    

}
like image 6
Mithun Sadananda Avatar answered Nov 07 '22 00:11

Mithun Sadananda