Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update OpenCVFrameRecorder to use C++ API #370

Merged
merged 1 commit into from
Mar 26, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 20 additions & 11 deletions src/main/java/org/bytedeco/javacv/OpenCVFrameRecorder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand All @@ -74,26 +74,35 @@ 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 {
release();
}

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?)");
}
Expand Down