这里主要利用了Opencv打开摄像头的代码,以及写入视频的函数,只是这里要注意的是摄像头好像没有帧率,在cvCreateVideoWriter,时要自己设置
#include"cv.h"
#include"highgui.h" int main(int argc, char** argv)
{
cvNamedWindow("camera", CV_WINDOW_AUTOSIZE);
argc = ;
CvCapture* capture;
capture = cvCreateCameraCapture();//如果不行换成-1,0,1试试
assert(capture != NULL);
IplImage* frame;
CvSize size = cvSize((int)cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH),
(int)cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT)); double fps = cvGetCaptureProperty(
capture,
CV_CAP_PROP_FPS//帧率
);
CvVideoWriter* writer = cvCreateVideoWriter( // On linux Will only work if you've installed ffmpeg development files correctly,
"D:\\Opencv\\testData\\camera.avi", // otherwise segmentation fault. Windows probably better.
CV_FOURCC('D', 'X', '', ''),
,//摄像头好像帧率获取出来为0,故设为15
size
);
IplImage* imgWrite = cvCreateImage(size, , );
while (){
frame = cvQueryFrame(capture);
if (!frame)
break;
char c = cvWaitKey();
if (c == ) break;
cvCopy(frame, imgWrite);
cvWriteToAVI(writer, imgWrite);
cvShowImage("camera", frame);
} cvReleaseCapture(&capture);
cvReleaseVideoWriter(&writer);
cvDestroyWindow("camera");
return ;
}