用opencv java打开视频文件 [英] open video file with opencv java

查看:225
本文介绍了用opencv java打开视频文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以现在有 Java的OpenCV ......
有人能告诉我如何用它打开Videofiles吗?

so there is OpenCV for Java now...! Can anyone tell me how to open Videofiles with it ?

我试着看一遍互联网,但一无所获。 VideoCapture类的文档不是很有帮助,因为它给出了一个C#示例并展示了如何从网络摄像头捕获。

I tryed and look all over the internet, but found nothing. The documentation of the VideoCapture class is not very helpfull, becaus it gives a C# example and show how to capture from a webcam.

OpenCV的Q& A 也没有帮助,因为没有(公共)方法,您可以给它一个文件名字符串。

the Q&A of OpenCV doesnt help either, because there is no (public) method to whom you can give a filename string.

但是按照API中的说明工作。但它没有
但VideoCapture类中有一个带有sting参数的私有方法。

BUT it should work as written in the API. But it doesn't There is however a privte method in the VideoCapture class with a sting parameter.

如果有解决方案,请回答,或者即使你有同样的问题。
garyee

please answer if have a solution, or even if you have the same problem. garyee

更新:( 2017年5月)

自版本3.0.0 VideoCapture类有一个构造函数,它接受一个字符串参数。所以现在有一个简单的解决方案来解决这个问题!

since Version 3.0.0 There is a constructor for the VideoCapture class that takes a string argument. So there is a easy solution to this Problem now!

推荐答案

对我而言,为什么所谓的自动生成的java包装器为opencv缺乏此功能。我首先使用VideoCapture(String filename)构造函数创建了一个新的VideoCapture类,并调用了私有本机方法。这导致了一个不满意的链接错误:

To me its a mystery why the so called automatically generated java wrapper for opencv is lacking this functionality. I first created a new VideoCapture class with a VideoCapture(String filename) constructor and called the private native method. This lead to an 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表示缺少相应的JNIEXPORT。
幸运的是,这可以修复。

This indicates that the corresponding JNIEXPORT is missing. Luckily this can be fixed.

令人惊讶的是,所需的c构造函数已在 opencv-2.4.6 / modules / highgui / include /中定义opencv2 / highgui / highgui.cpp

Surprisingly 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);

我们将构造函数添加到 opencv-2.4.6 / modules中的VideoCapture类中/java/generator/src/java/highgui+VideoCapture.java:

We add the constructor we long 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;
}

关键且棘手的一步是添加jni导出。特别是为J​​NICALL找到合适的方法名称证明是具有挑战性的,因为构造函数被重载并且将java类作为参数。另外,我们需要将java sting转换为c-string。其余的是从其他构造函数复制的。

The crucial and tricky step was to add the jni export. Especially finding the right method name for the JNICALL proved to be challenging, since the constructor is overloaded and takes a java class as argument. Additionally we need to convert the java sting into a c-string. The rest is copied from the other constructors.

opencv-2.4.6 / modules / java / generator / src / cpp / VideoCapture.cpp 我们添加这个新的JNIEXPORT:

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;
    }
}

重新编译OpenCV,它应该可以工作。

Recompile OpenCV and it should work.

这篇关于用opencv java打开视频文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆