Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Currently a method for loading video files is missing in the VideoCapture class. Although the VideoCapture class defines a native method of this kind, calling it leads to a unsatisfied link error:

// From VideoCapture 
// C++: VideoCapture::VideoCapture(string filename)
private static native long n_VideoCapture(java.lang.String filename);

I dug up the corresponding source files and got it to work. Here is how:

The needed c-constructor is all ready defined in opencv-2.4.6/modules/highgui/include/opencv2/highgui/highgui.cpp

CV_WRAP VideoCapture(const string& filename);

All we need to do is to export this constructor via JNI and add it to the Java class

opencv-2.4.6/modules/java/generator/src/java/highgui+VideoCapture.java:

add the constructor we long for:

//
// C++: VideoCapture::VideoCapture(const string& filename)
//

// javadoc: VideoCapture::VideoCapture(String filename)
public VideoCapture(String filename)
{
    nativeObj = n_VideoCapture(filename);

    return;
}

opencv-2.4.6/modules/java/generator/src/cpp/VideoCapture.cpp here we add the jni export:

//
//   VideoCapture::VideoCapture(const string& filename)
//

JNIEXPORT jlong JNICALL Java_org_opencv_highgui_VideoCapture_n_1VideoCapture__Ljava_lang_String_2
(JNIEnv* env, jclass, jstring filename);

JNIEXPORT jlong JNICALL Java_org_opencv_highgui_VideoCapture_n_1VideoCapture__Ljava_lang_String_2
(JNIEnv* env, jclass, jstring filename)
{
    try {
        LOGD("highgui::VideoCapture_n_1VideoCapture__Ljava_lang_String_2()");
        const char* jnamestr = env->GetStringUTFChars(filename, NULL);
        string stdFileName(jnamestr);
        VideoCapture* _retval_ = new VideoCapture( jnamestr );

        return (jlong) _retval_;
    } catch(cv::Exception e) {
        LOGD("highgui::VideoCapture_n_1VideoCapture__Ljava_lang_String_2() catched cv::Exception: %s", e.what());
        jclass je = env->FindClass("org/opencv/core/CvException");
        if(!je) je = env->FindClass("java/lang/Exception");
        env->ThrowNew(je, e.what());
        return 0;
    } catch (...) {
        LOGD("highgui::VideoCapture_n_1VideoCapture__Ljava_lang_String_2() catched unknown exception (...)");
        jclass je = env->FindClass("java/lang/Exception");
        env->ThrowNew(je, "Unknown exception in JNI code {highgui::VideoCapture_n_1VideoCapture__S()}");
        return 0;
    }
}

Recompile and it should work.

Currently a method for loading video files is missing in the VideoCapture class. Although the VideoCapture class defines a native method of this kind, calling it leads to a unsatisfied link error:

// From VideoCapture 
// C++: VideoCapture::VideoCapture(string filename)
private static native long n_VideoCapture(java.lang.String filename);

I dug up the corresponding source files and got it to work. Here is how:

The needed c-constructor is all ready already defined in opencv-2.4.6/modules/highgui/include/opencv2/highgui/highgui.cpp

CV_WRAP VideoCapture(const string& filename);

All we need to do is to export this constructor via JNI and add it to the Java class

opencv-2.4.6/modules/java/generator/src/java/highgui+VideoCapture.java:

add the constructor we long for:

//
// C++: VideoCapture::VideoCapture(const string& filename)
//

// javadoc: VideoCapture::VideoCapture(String filename)
public VideoCapture(String filename)
{
    nativeObj = n_VideoCapture(filename);

    return;
}

opencv-2.4.6/modules/java/generator/src/cpp/VideoCapture.cpp here we add the jni export:

//
//   VideoCapture::VideoCapture(const string& filename)
//

JNIEXPORT jlong JNICALL Java_org_opencv_highgui_VideoCapture_n_1VideoCapture__Ljava_lang_String_2
(JNIEnv* env, jclass, jstring filename);

JNIEXPORT jlong JNICALL Java_org_opencv_highgui_VideoCapture_n_1VideoCapture__Ljava_lang_String_2
(JNIEnv* env, jclass, jstring filename)
{
    try {
        LOGD("highgui::VideoCapture_n_1VideoCapture__Ljava_lang_String_2()");
        const char* jnamestr = env->GetStringUTFChars(filename, NULL);
        string stdFileName(jnamestr);
        VideoCapture* _retval_ = new VideoCapture( jnamestr );

        return (jlong) _retval_;
    } catch(cv::Exception e) {
        LOGD("highgui::VideoCapture_n_1VideoCapture__Ljava_lang_String_2() catched cv::Exception: %s", e.what());
        jclass je = env->FindClass("org/opencv/core/CvException");
        if(!je) je = env->FindClass("java/lang/Exception");
        env->ThrowNew(je, e.what());
        return 0;
    } catch (...) {
        LOGD("highgui::VideoCapture_n_1VideoCapture__Ljava_lang_String_2() catched unknown exception (...)");
        jclass je = env->FindClass("java/lang/Exception");
        env->ThrowNew(je, "Unknown exception in JNI code {highgui::VideoCapture_n_1VideoCapture__S()}");
        return 0;
    }
}

Recompile and it should work.