opencv for java videowriter无法打开

时间:2016-03-16 16:38:46

标签: java opencv

我在java中使用opencv3.1.0。我想将视频保存到文件中,但是视频打印机无法打开。我的代码如下:

Size size = new Size(capture.get(Videoio.CAP_PROP_FRAME_WIDTH), capture.get(Videoio.CAP_PROP_FRAME_HEIGHT));
double fps = capture.get(Videoio.CAP_PROP_FPS);
VideoWriter vw = new VideoWriter("/home/sify/1.mp4", VideoWriter.fourcc('X', '2', '6', '4'), fps, size, true);

vw.isOpened()返回false。没有创建文件。

我怀疑四卦有问题。

我尝试使用H264 / XVID / FMP4 / MPEG,并尝试用(int)capture.get(Videoio.CAP_PROP_FOURCC)替换第二个参数,也无法正常工作。

2 个答案:

答案 0 :(得分:0)

问题是编解码器,试试这个。

int fourcc = VideoWriter.fourcc('m','j','p','g');
Size frameSize = new Size((int) videoCapture.get(Videoio.CAP_PROP_FRAME_WIDTH),(int) videoCapture.get(Videoio.CAP_PROP_FRAME_HEIGHT));
VideoWriter videoWriter = new VideoWriter("testfile.avi", fourcc, 20, frameSize, true);
 if (videoCapture.read(frame))
      videoWriter.write(frame);

答案 1 :(得分:0)

openCV下载包含动态链接库文件opencv_ffmpeg343_64.dll中的mp4编解码器。要使该内容对JVM可见,请在打开VideoWriter之前运行如下代码。

System.setProperty("java.library.path", "C:\pathToFolderContainingDLL")
val fieldSysPath = ClassLoader::class.java.getDeclaredField("sys_paths")
fieldSysPath.isAccessible = true
fieldSysPath.set(null, null)
//next time path is accessed, the new path will be imported

System.loadLibrary("opencv_ffmpeg343_64")