I'm using OpenCV 2.4.8 in Android using JNI.
I opening the camera with VideoCapture and I want to record it. I have the image in a cv::Mat, it appear in the screen correctly.
But, when I try to open VideoCapture, it returns false always.
// Camera resolution is 640x480 and its fine.
_camera_resolution = calc_optimal_camera_resolution(u.name, 640, 480);
// Store in sdcard, I have permission in AndroidManifest.xml
const char * videoName = "/sdcard/videoTest.avi";
// Segmentation fault 11! this method doesnt work in Android? so commented
//const int ex = static_cast<int>(_reader.get(CV_CAP_PROP_FOURCC));
// Segmentation fault 11! too!!! so commented
//const double fps = _reader.get(CV_CAP_PROP_FPS);
// Try to open
_isRecording = _writer.open(videoName, -1, 30, _camera_resolution, true);
// Return FALSE always
if(!_writer.isOpened())
{
LOGE("rec - Error opening video writer");
}
else
{
LOGD("rec - Video writer opened in startRecording");
}
I have tried to use as FOURCC:
CV_FOURCC('M', 'J', 'P', 'G') and CV_FOURCC('M', 'P', '4', 'V') // It doesn't works!
I have tried with different fps ratios, 15.0, 30.0...
Camera resolution seems work because when I print the value is correct.
Why is not opening correctly?
As I know OpenCV4adnroid doesn’t support video reading and writing. Try to rebuild your Opencv with an encoder option: (such: WITH_FFMPEG=YES or WITH_VFW=YES )
OR try save a sequence of images then encode a video from this sequence from java code I tried next proposition (ref)
You can download JCodec library from a project web site or via Maven, for this add the below snippet to your pom.xml:
Android: Android users can use something like below to convert Android Bitmap object to JCodec native format:
('M','J','P','G') is the only supported by android while using .avi ext. Most important of all is to #include stdio.h, without this you wont be able to open VideoWriter video