Pass an array of Mats to native code

asked 2013-09-28 10:13:24 -0500

sp_mic88 gravatar image

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

berak gravatar image

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?

edit retag flag offensive close merge delete

Comments

uh, that won't work at all, due to a couple of reasons:

  • having a fixed buffer Mat m[1000] is a bad idea. it can and will overflow. use a vector or similar
  • a single function to slurp in a whole movie ? ouch. this will block your main activity, and thus make it stop, you'd need an additional thread for that, or split into several functions, that allow a bit more 'async' handling.
  • last, but most problematic: most of the VideoCapture functionality is disabled on android, due to the missing ffmpg backend. to make it work, you'd have to compile ffmpeg for android first, and then link your code to that / restore the VideoCapture functionality.
berak gravatar imageberak ( 2013-09-29 05:21:11 -0500 )edit