Reading frames from video file using openCV and Na

2020-07-17 16:49发布

问题:

I'm trying to write an app that gets all the frames of a video and manipulating them, I found oout that the best way to extract frames on Android is using OpenCv lib.

I saw in the sample code that uses VideoCapture object that receives the video path and can grab frames out of it, so I wrote the following code but the capture.open() doen't really open the video file, the capture.isOpen() is always false.

Source code:

#include <jni.h>
//opencv

#include <opencv2/highgui/highgui.hpp>
#include <opencv2/video/background_segm.hpp>
#include <opencv2/core/mat.hpp>
//C
#include <stdio.h>
#include <string.h>
//C++
#include <iostream>
#include <sstream>
//Android
#include <android/log.h>

//#define  LOGI(TAG,INFO)  __android_log_print(ANDROID_LOG_INFO,INFO,__VA_ARGS__)

using namespace cv;

extern "C" {

JNIEXPORT void JNICALL Java_com_example_nativeopencvcheck_MainActivity_processVideo(JNIEnv* env,
    jobject thsObj,jstring fileName) {

const char * fileNameNative;
jboolean isCopy;
fileNameNative = env->GetStringUTFChars(fileName, &isCopy);

//create the capture object
__android_log_print(ANDROID_LOG_ERROR, "From_Native",
            "trying to open file: %s", fileNameNative);
VideoCapture capture(fileNameNative);
capture.open(fileNameNative);

if (!capture.isOpened()) {  //!!!!!! ALWAYS CLOSED !!!!!
    __android_log_write(ANDROID_LOG_ERROR, "From_Native",
            "capture isn't open. closing..");
    exit( EXIT_FAILURE);
}

Mat iplimage;
capture.retrieve(iplimage,0); 
if (iplimage.size > 0) {
    jclass cls = env->FindClass( "com/example/opencvframesext/MainActivity");
    if (cls == 0) {
        return;
    }

    jmethodID javamethod = env->GetMethodID(cls, "getCurrentFrameFromNative", "()V");
    if (javamethod == 0) {
    //  LOGI("From_Native","GetMethodID error");
        return;
    }

    jobject obj; // TODO
    env->CallVoidMethod(obj, javamethod);

   return;
}


/*bool gotFrame = capture.read(mat);
 while (gotFrame) {
 mats.addref(mat);
 capture.read(mat);
 }*/

//delete capture object
capture.release();
}
}

回答1:

yea, that wont work, unfortunately.

there's no ffmpeg backend for VideoCapture on android.