#include<iostream>
#include<opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
VideoCapture cam(0);//到开摄像头
if (!cam.isOpened())
{
cout << "cam open failed !" << endl;
getchar();
return -1;
}
cout << "cam open success !" << endl;
namedWindow("cam");
Mat img;
VideoWriter vw;//生成视频流对象
int fps = cam.get(CAP_PROP_FPS);
if (fps <= 0)fps = 25;
vw.open("D:/tupian/1out.avi", VideoWriter::fourcc('X', '2', '6', '4'), fps,
Size(cam.get(CAP_PROP_FRAME_WIDTH), cam.get(CAP_PROP_FRAME_HEIGHT)));//创建视频文件
if (!vw.isOpened())
{
cout << "VideoWriter open failed !" << endl;
getchar();
return -1;
}
cout << "VideoWriter open failed !" << endl;
for (;;)
{
cam.read(img);
if(img.empty())break;
imshow("cam", img);
vw.write(img);//将画面写入该文件下
if(waitKey(5)=='q')break;
}
waitKey(0);
return 0;
}