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

╄→гoц情女王★ 提交于 2019-11-30 12:44:47

问题


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?

回答1:


I solved this problem with standard tools.

  1. Create in Java class as objects (O) container (C)
  2. Pass array of objects (O) from Java code to native part
  3. Create from array vector in C++ code
  4. Calculate new vectors
  5. Build array of containers (C) and insert into objects (O)
  6. Return array of containers (C)

Code implement:

On java part:

1 - Create array from list of points

On c++ part:

2 - build input vector

std::vector<CurvePoint> src_line;

jclass java_points_cls = env->FindClass("myPointClass");
jmethodID java_mid = env->GetMethodID(java_points_cls, "<init>", "(II)V");    
jfieldID fidX = env->GetFieldID(java_points_cls, "x", "I");
jfieldID fidY = env->GetFieldID(java_points_cls, "y", "I");

int srcCount = env->GetArrayLength(srcLines);

for (int i=0; i < srcCount; i++) 
{
    jobject cur_pnt =  env->GetObjectArrayElement(srcLines, i); 

    LinePoint src_point;        

    src_point.x = env->GetIntField(cur_pnt, fidX); 
    src_point.y = env->GetIntField(cur_pnt, fidY);    

    src_line.push_back(src_point);
}

3 - calculation lines

4 - build output array

jclass java_line_cls = env->FindClass("myLinesClass");

jmethodID java_line_add = env->GetMethodID(java_line_cls, "addPoint", "(II)V");  
jmethodID java_line_init = env->GetMethodID(java_line_cls, "<init>", "()V");

jobjectArray resLines = (jobjectArray) env->NewObjectArray(lines.size(),     java_line_cls, 0); 

for(int i = 0; i < lines.size(); ++i)
{
    jobject cur_line =  env->NewObject(java_line_cls, java_line_init);
    for(int j = 0; j < lines[i].size(); ++j)
        env->CallVoidMethod(cur_line, java_line_add, 
                                lines[i][j].x,
                                lines[i][j].y);
    env->SetObjectArrayElement(resLines, i, cur_line);
}

return resLines;

Java part

5 - Create list of lines from returned array




回答2:


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;    

}



回答3:


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




回答4:


You can also use this project. It will allow to use java classes ower JNI like a native one.



来源:https://stackoverflow.com/questions/10813346/pass-return-and-convert-to-vectors-list-of-lists-over-jni

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