Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Pass an array of Mats to native code

How can I write my c++ JNI function so that it returns an array of Mat to Java code? I am programming in Android environment, with the help of NDK to use also some functions of OpenCV.

My c++ function is:

 JNIEXPORT void JNICALL Java_com_micaela_myapp_MainActivity2_getFrames(JNIEnv* env, jobject object, jstring path)
{
    const char *str;
    str = env->GetStringUTFChars(path, NULL);   
    VideoCapture input_video;
    if(input_video.open(str)){
        cout<<"Video File Opened"<<endl;
    }else{
        cout<<"Video File Not Found"<<endl;
    }
    Mat image;
    Mat frameBuffer[1000];  
    int i=0;
    while(input_video.read(image)==true){
        image.copyTo(frameBuffer[i]);
        i++;
    }
}

In Java I have:

static{
    System.loadLibrary("myapp");
}
public static native void getFrames(String path);

This function now returns void and works properly. However, my purpose is to obtain the array frameBuffer from it, in order to use it in Java. How can I do this?

click to hide/show revision 2
retagged

updated 2013-09-29 05:04:27 -0500

berak gravatar image

Pass an array of Mats to native code

How can I write my c++ JNI function so that it returns an array of Mat to Java code? I am programming in Android environment, with the help of NDK to use also some functions of OpenCV.

My c++ function is:

 JNIEXPORT void JNICALL Java_com_micaela_myapp_MainActivity2_getFrames(JNIEnv* env, jobject object, jstring path)
{
    const char *str;
    str = env->GetStringUTFChars(path, NULL);   
    VideoCapture input_video;
    if(input_video.open(str)){
        cout<<"Video File Opened"<<endl;
    }else{
        cout<<"Video File Not Found"<<endl;
    }
    Mat image;
    Mat frameBuffer[1000];  
    int i=0;
    while(input_video.read(image)==true){
        image.copyTo(frameBuffer[i]);
        i++;
    }
}

In Java I have:

static{
    System.loadLibrary("myapp");
}
public static native void getFrames(String path);

This function now returns void and works properly. However, my purpose is to obtain the array frameBuffer from it, in order to use it in Java. How can I do this?