From 40617118a33719a2e3c800ad463d52de656f9340 Mon Sep 17 00:00:00 2001 From: Lloyd Date: Sat, 26 Mar 2016 17:28:37 +0900 Subject: [PATCH] Update OpenCVFrameRecorder to use C++ API --- .../bytedeco/javacv/OpenCVFrameRecorder.java | 31 ++++++++++++------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/bytedeco/javacv/OpenCVFrameRecorder.java b/src/main/java/org/bytedeco/javacv/OpenCVFrameRecorder.java index 97fdd4bd..c6c1ddde 100644 --- a/src/main/java/org/bytedeco/javacv/OpenCVFrameRecorder.java +++ b/src/main/java/org/bytedeco/javacv/OpenCVFrameRecorder.java @@ -63,7 +63,7 @@ public OpenCVFrameRecorder(String filename, int imageWidth, int imageHeight) { } public void release() throws Exception { if (writer != null) { - cvReleaseVideoWriter(writer); + writer.release(); writer = null; } } @@ -74,14 +74,25 @@ public void release() throws Exception { private static final boolean windows = Loader.getPlatform().startsWith("windows"); private String filename; - private CvVideoWriter writer = null; - private OpenCVFrameConverter.ToIplImage converter = new OpenCVFrameConverter.ToIplImage(); + private VideoWriter writer = null; + private OpenCVFrameConverter.ToMat converter = new OpenCVFrameConverter.ToMat(); public void start() throws Exception { - writer = cvCreateVideoWriter(filename, videoCodec, frameRate, cvSize(imageWidth, imageHeight), pixelFormat); - if (writer == null) { - throw new Exception("cvCreateVideoWriter(): Could not create a writer"); - } + writer = new VideoWriter(filename, fourCCCodec(), frameRate, new Size(imageWidth, imageHeight), isColour()); + } + + /** + * Pixel format is an int and maps to colour if != 0, greyscale otherwise. + */ + private boolean isColour() { + return pixelFormat != 0; + } + + /** + * VideoCodec in JavaCV jargon is the same as FourCC code in OpenCV speak + */ + private int fourCCCodec() { + return videoCodec; } public void stop() throws Exception { @@ -89,11 +100,9 @@ public void stop() throws Exception { } public void record(Frame frame) throws Exception { - IplImage image = converter.convert(frame); + Mat mat = converter.convert(frame); if (writer != null) { - if (cvWriteFrame(writer, image) == 0) { - throw new Exception("cvWriteFrame(): Could not record frame"); - } + writer.write(mat); } else { throw new Exception("Cannot record: There is no writer (Has start() been called?)"); }