Ask Your Question

mirel7c3's profile - activity

2013-10-14 01:44:14 -0600 received badge  Nice Answer (source)
2013-10-10 03:53:25 -0600 received badge  Supporter (source)
2013-10-10 02:22:44 -0600 received badge  Editor (source)
2013-10-09 16:33:31 -0600 received badge  Teacher (source)
2013-10-09 16:01:32 -0600 received badge  Necromancer (source)
2013-10-09 14:44:15 -0600 answered a question Desktop Java - how to load video file?

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 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.