2014-01-29 20 views
0

有人能幫助我返回一個從本地c到java的對象數組嗎? 我做了很大的一部分,只需要設置Mat字段。從jni返回一個Mat對象到java

getFaces()函數應返回一個Face[]數組。 oject包含opencv.core.Rect臉,opencv.core.Matintintint 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; 
} 

回答

0

如果我是你,我就不會返回cv::Mat陣列。我會每次返回一個單獨的cv::Mat,並通過根據需要多次調用函數在Java端創建數組。

基本上,我會得到一個函數來獲取數組的大小。另一個功能是從臉部容器獲取臉部圖像。如果你不知道如何退貨cv::Mat請告訴我。

希望這會有所幫助。