这篇文章将为大家详细讲解有关使用opencv怎么实现定时录像功能,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
#include "cv.h"
#include "cxcore.h"
#include "highgui.h"
#include <iostream>
#include <time.h>
#include <Windows.h>
#include <Mmsystem.h>
#include "stdio.h"
int timea=100000;
using namespace std;
/*void times()
{
SYSTEMTIME sys_time;
//将变量值设置为本地时间
GetLocalTime( &sys_time );
//输出时间
printf( "%4d/%02d/%02d %02d:%02d:%02d.%03d 星期%1d\n",sys_time.wYear,
sys_time.wMonth,
sys_time.wDay,
sys_time.wHour,
sys_time.wMinute,
sys_time.wSecond,
sys_time.wMilliseconds,
sys_time.wDayOfWeek);
// system("time");
//
// system("pause");
return 0;
} */
int main()
{
CvCapture* capture=cvCaptureFromCAM(0);
CvVideoWriter* video=NULL;
IplImage* frame=NULL;
int n;
if(!capture) //如果不能打开摄像头给出警告
{
cout<<"Can not open the camera."<<endl;
return -1;
}
else
{
frame=cvQueryFrame(capture); //首先取得摄像头中的一帧
int c=0;
SYSTEMTIME sys_time;
//将变量值设置为本地时间
GetLocalTime( &sys_time );
char buf[1024];
sprintf(buf,"camera-%4d-%2d-%02d-%02d-%02d-%02d.avi",sys_time.wYear,sys_time.wMonth,sys_time.wDay,
sys_time.wHour,sys_time.wMinute, sys_time.wSecond);
video=cvCreateVideoWriter(buf, CV_FOURCC('X', 'V', 'I', 'D'), 25,
cvSize(frame->width,frame->height)); //创建CvVideoWriter对象并分配空间
//保存的文件名为camera.avi,编码要在运行程序时选择,大小就是摄像头视频的大小,帧频率是32
if(video) //如果能创建CvVideoWriter对象则表明成功
{
cout<<"VideoWriter has created."<<endl;
}
cout<<"set the record time\n"<<endl;//设置录像时间
cin>>timea;
int ti=timea*25;
cvNamedWindow("Camera Video",1); //新建一个窗口
int i = 0;
while(i <= ti) // 让它循环ti次自动停止录取
{
frame=cvQueryFrame(capture); //从CvCapture中获得一帧
if(!frame)
{
cout<<"Can not get frame from the capture."<<endl;
break;
}
n=cvWriteFrame(video,frame); //判断是否写入成功,如果返回的是1,表示写入成功
// cout<<n<<endl;
cvShowImage("Camera Video",frame); //显示视频内容的图片
i++;
if(cvWaitKey(2)>0)
break; //有其他键盘响应,则退出
}
cvReleaseVideoWriter(&video); //如果不释放则春不上
cvReleaseCapture(&capture);
cvDestroyWindow("Camera Video");
}
return 0;
}
关于使用opencv怎么实现定时录像功能就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。