我无法录制网络摄像头视频(即捕获和保存 .avi或.mp4文件)使用JavaCV / OpenCV / FFMPEG ,我做错了什么?
使用的版本(所有64位)
Win 7,NetBeans8.0.2,jdk1.7.0_10,JavaCV 1.0,OpenCV 3.0.0,ffmpeg-2.1.1-win64-shared。
我的系统变量设置为
C:\ Program Files \ Java \ jdk1.7.0_10 ;%SystemRoot%\ system32;%SystemRoot%;%SystemRoot%\ System32 \ Wbem;%SYSTEMROOT%\ System32 \ WindowsPowerShell \ v1 .0 \; C:\ Program Files \ Intel \ WiFi \ bin \; C:\ Program Files \ Common Files \ Intel \ WirelessCommon \; C:\ Program Files(x86)\ Intel \ OpenCL SDK \ 2.0 \ bin \ x86 ; C:\ Program Files(x86)\ Intel \ OpenCL SDK \ 2.0 \ bin \ x64; C:\ Program Files(x86)\ MySQL \ MySQL Fabric 1.5.4& MySQL Utilities 1.5.4 1.5 \; C:\ Program Files(x86)\ MySQL \ MySQL Fabric 1.5.4& MySQL Utilities 1.5.4 1.5 \ PHP的学说扩展\; C:\ opencv \ build \ x64 \ vc11 \ bin; C:\ ffmpeg \ bin
下载并设置路径变量后,我将jar文件添加到Netbeans项目
C:\的OpenCV \构建\ java中\的OpenCV-300.jar C:\ javacv-1.0-BIN \ javacv斌\ videoinput.jar C:\ javacv-1.0-BIN \ javacv斌\ videoinput - 窗口x86_64.jar C:\ javacv-1.0-BIN \ javacv斌\ videoinput - 窗口x86.jar C:\ javacv-1.0-BIN \ javacv斌\ opencv.jar C:\ javacv-1.0-BIN \ javacv斌\ OpenCV的 - 窗口x86_64.jar C:\ javacv-1.0-BIN \ javacv斌\ OpenCV的 - 窗口x86.jar C:\ javacv-1.0-BIN \ javacv斌\ libfreenect.jar C:\ javacv-1.0-BIN \ javacv斌\ libfreenect - 窗口x86_64.jar C:\ javacv-1.0-BIN \ javacv斌\ libfreenect - 窗口x86.jar C:\ javacv-1.0-BIN \ javacv斌\ libdc1394.jar C:\ javacv-1.0-BIN \ javacv斌\的junit.jar C:\ javacv-1.0-BIN \ javacv斌\ javacv.jar C:\ javacv-1.0-BIN \ javacv斌\ javacpp.jar C:\ javacv-1.0-BIN \ javacv斌\ hamcrest-core.jar添加 C:\ javacv-1.0-BIN \ javacv斌\ flycapture.jar C:\ javacv-1.0-BIN \ javacv斌\ flycapture - 窗口x86_64.jar C:\ javacv-1.0-BIN \ javacv斌\ flycapture - 窗口x86.jar C:\ javacv-1.0-BIN \ javacv斌\ flandmark.jar C:\ javacv-1.0-BIN \ javacv斌\ flandmark - 窗口x86_64.jar C:\ javacv-1.0-BIN \ javacv斌\ flandmark - 窗口x86.jar C:\ javacv-1.0-BIN \ javacv斌\ ffmpeg.jar C:\ javacv-1.0-BIN \ javacv斌\ ffmpeg的 - 窗口x86_64.jar C:\ javacv-1.0-BIN \ javacv斌\ ffmpeg的 - 窗口x86.jar C:\ javacv-1.0-BIN \ javacv斌\ artoolkitplus.jar C:\ javacv-1.0-BIN \ javacv斌\ artoolkitplus - 窗口x86_64.jar C:\ javacv-1.0-BIN \ javacv斌\ artoolkitplus - 窗口x86.jar
问题1:
第一个捕获网络摄像头视频的程序(显示并保存到output.avi文件)如下所示。
显示网络摄像头并创建output.avi。但是当我在Media Player中打开文件output.avi时终止程序后它没有显示任何内容:)
不起作用
import java.io.File;
import java.net.URL;
import org.bytedeco.javacv.*;
import org.bytedeco.javacpp.*;
import org.bytedeco.javacpp.indexer.*;
import static org.bytedeco.javacpp.opencv_core.*;
import static org.bytedeco.javacpp.opencv_imgproc.*;
import static org.bytedeco.javacpp.opencv_calib3d.*;
import static org.bytedeco.javacpp.opencv_objdetect.*;
public class JCVdemo3 {
public static void main(String[] args) throws Exception {
// Preload the opencv_objdetect module to work around a known bug.
Loader.load(opencv_objdetect.class);
// The available FrameGrabber classes include OpenCVFrameGrabber (opencv_videoio),
// DC1394FrameGrabber, FlyCaptureFrameGrabber, OpenKinectFrameGrabber,
// PS3EyeFrameGrabber, VideoInputFrameGrabber, and FFmpegFrameGrabber.
FrameGrabber grabber = FrameGrabber.createDefault(0);
grabber.start();
// CanvasFrame, FrameGrabber, and FrameRecorder use Frame objects to communicate image data.
// We need a FrameConverter to interface with other APIs (Android, Java 2D, or OpenCV).
OpenCVFrameConverter.ToIplImage converter = new OpenCVFrameConverter.ToIplImage();
IplImage grabbedImage = converter.convert(grabber.grab());
int width = grabbedImage.width();
int height = grabbedImage.height();
FrameRecorder recorder = FrameRecorder.createDefault("output.avi", width, height);
recorder.start();
CanvasFrame frame = new CanvasFrame("Some Title");
while (frame.isVisible() && (grabbedImage = converter.convert(grabber.grab())) != null) {
// cvWarpPerspective(grabbedImage, rotatedImage, randomR);
Frame rotatedFrame = converter.convert(grabbedImage);
//opencv_core.IplImage grabbedImage = grabber.grab();
frame.showImage(rotatedFrame);
recorder.record(rotatedFrame);
}
frame.dispose();
recorder.stop();
grabber.stop();
}
}
问题2:当我运行以下代码时
opencv_core.IplImage grabbedImage = grabber.grab();
不兼容的类型:框架无法转换为IplImage 消息出现
import java.io.File;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.bytedeco.javacv.*;
import org.bytedeco.javacpp.*;
import org.bytedeco.javacpp.indexer.*;
import static org.bytedeco.javacpp.opencv_core.*;
import static org.bytedeco.javacpp.opencv_imgproc.*;
import static org.bytedeco.javacpp.opencv_calib3d.*;
import static org.bytedeco.javacpp.opencv_objdetect.*;
public class Demo {
public static void main(String[] args) {
try {
OpenCVFrameGrabber grabber = new OpenCVFrameGrabber(0);
grabber.start();
opencv_core.IplImage grabbedImage = grabber.grab();
CanvasFrame canvasFrame = new CanvasFrame("Video with JavaCV");
canvasFrame.setCanvasSize(grabbedImage.width(), grabbedImage.height());
grabber.setFrameRate(grabber.getFrameRate());
FFmpegFrameRecorder recorder = new FFmpegFrameRecorder("mytestvideo.mp4", grabber.getImageWidth(), grabber.getImageHeight());
recorder.setFormat("mp4");
recorder.setFrameRate(30);
recorder.setVideoBitrate(10 * 1024 * 1024);
recorder.start();
while (canvasFrame.isVisible() && (grabbedImage = grabber.grab()) != null) {
canvasFrame.showImage(grabbedImage);
recorder.record(grabbedImage);
}
recorder.stop();
grabber.stop();
canvasFrame.dispose();
} catch (FrameGrabber.Exception ex) {
Logger.getLogger(JCVdemo.class.getName()).log(Level.SEVERE, null, ex);
} catch (FrameRecorder.Exception ex) {
Logger.getLogger(JCVdemo.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
问题是:我做错了什么?
我无法录制任何类型的视频;无论我使用什么版本的JavaCV / OPenCv。
请告诉我一些工作示例,记录来自网络摄像头的视频以及工作的JavaCV / OpenCV / FFmpeg兼容版本。
答案 0 :(得分:2)
使用:OpenCVFrameConverter
OpenCVFrameConverter.ToIplImage converter = new OpenCVFrameConverter.ToIplImage();
img = converter.convert(grabber.grab());