The mysterious reason for me is why the so-called automatically generated Java shell for opencv does not have this functionality. First, I created a new VideoCapture class with the VideoCapture constructor (String filename) and called a private native method. This results in an unsatisfied link error:
Exception in thread "main" java.lang.UnsatisfiedLinkError: org.opencv.highgui.VideoCapture.n_VideoCapture(Ljava/lang/String;)J at org.opencv.highgui.VideoCapture.n_VideoCapture(Native Method) at org.opencv.highgui.VideoCapture.<init>(VideoCapture.java:90) at Tester.main(Tester.java:30)
This means that the corresponding JNIEXPORT is missing. Fortunately, this can be fixed.
Surprisingly, the required c-constructor is already defined in opencv-2.4.6 / modules / highgui / include / opencv2 / highgui / highgui.cpp
CV_WRAP VideoCapture(const string& filename);
We add a constructor, which we have been adding for a long time to the VideoCapture class in opencv-2.4.6 / modules / java / generator / src / java / highgui + VideoCapture.java:
// // C++: VideoCapture::VideoCapture(const string& filename) // // javadoc: VideoCapture::VideoCapture(String filename) public VideoCapture(String filename) { nativeObj = n_VideoCapture(filename); return; }
The key and difficult step was to add jni export. Especially finding the right method name for JNICALL proved to be difficult because the constructor is overloaded and takes the java class as an argument. In addition, we need to convert java sting to c-string. The rest is copied from other constructors.
In opencv-2.4.6 / modules / java / generator / src / cpp / VideoCapture.cpp we add this new JNIEXPORT:
// // 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__Ljava_lang_String_2()}"); return 0; } }
Recompile OpenCV and it should work.