0
有人能幫助我返回一個從本地c到java的對象數組嗎? 我做了很大的一部分,只需要設置Mat字段。從jni返回一個Mat對象到java
getFaces()
函數應返回一個Face[]
數組。 oject包含opencv.core.Rect
臉,opencv.core.Mat
,int
,int
,int fields
JNIEXPORT jobjectArray JNICALL Java_com_faceprocessing_PersonDetector_nativeGetFaces (JNIEnv * jenv, jclass) {
if (faceContainer.size()==0) return NULL;
jclass faceClass = jenv->FindClass("com/faceprocessing/Face");
jmethodID faceCtorID = jenv->GetMethodID(faceClass, "<init>", "()V");
jfieldID facePosID = jenv->GetFieldID(faceClass, "Position", "Lorg/opencv/core/Rect;");
jfieldID faceIDID = jenv->GetFieldID(faceClass, "PredictedID", "I");
jfieldID faceGenderID = jenv->GetFieldID(faceClass, "PredictedGender", "I");
jfieldID faceAgeID = jenv->GetFieldID(faceClass, "PredictedAge", "I");
jclass rectClass = jenv->FindClass("org/opencv/core/Rect");
jmethodID rectCtorID = jenv->GetMethodID(rectClass, "<init>", "(IIII)V");
jclass matClass = jenv->FindClass("org/opencv/core/Mat");
int correctCount = 0;
for (int i=0; i<faceContainer.size(); i++) {
Face* pFace = faceContainer.at(i);
if (!pFace->isError) correctCount++;
}
jobjectArray jFaceArray = jenv->NewObjectArray(correctCount, faceClass, NULL);
int j=0;
for (int i=0; i<faceContainer.size(); i++) {
Face* pFace = faceContainer.at(i);
if (pFace->isError) continue;
jobject jFace = jenv->NewObject(faceClass, faceCtorID);
jobject jRect = jenv->NewObject(rectClass, rectCtorID, pFace->facePosition->x, pFace->facePosition->y, pFace->facePosition->width, pFace->facePosition->height);
jenv->SetObjectField(jFace, facePosID, jRect);
jenv->SetIntField(jFace, faceIDID, pFace->predictedID);
jenv->SetIntField(jFace, faceGenderID ,pFace->predictedGender);
jenv->SetIntField(jFace, faceAgeID, pFace->predictedAge);
jenv->SetObjectArrayElement(jFaceArray,j,jFace);
j++;
}
return jFaceArray;
}