I need to pass from Java
List< List > points;
over jni to C++ and convert to
std::vector< std::vec
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;
}
I solved this problem with standard tools.
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
You can also use this project. It will allow to use java classes ower JNI like a native one.
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