http://blog.sina.com.cn/s/blog_3e4774e30100frwi.html
//使用OpenCV实现获取摄像头数据并1秒保存成一张jpg图片(保存的图片的大小被缩小了),jpg图片的大小可调,文件名为test.c
//编译方法为:g++ `pkg-config opencv --libs --cflags opencv` test.c -o test
#include<stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include<time.h>
#include<stdlib.h>
#include<cv.h>
#include<cxcore.h>
#include<highgui.h>
int main( int argc,char ** argv )
{
//声明IplImage指针
IplImage* pFrame=NULL;
IplImage* pSaveFrame=NULL;
CvCapture* pCapture=NULL;
static char filename[40];
struct tm * tm_ptr;
time_t the_time;
//创建窗口
cvNamedWindow("video",1);
//打开摄像头
if( !(pCapture=cvCaptureFromCAM(-1)) )
{
fprintf(stderr,"Can not open camera.\n");
return -1;
}
if(mkdir("./Alert",0755)==-1)
{
//创建时存在该目录会返回错误码,如不存在则创建它,但由于我们需要该目录,则出错也不处理
}
//逐帧读取视频
while(pFrame=cvQueryFrame(pCapture))
{
cvShowImage("video",pFrame);
if(cvWaitKey(2)>=0) break;
pSaveFrame=cvCreateImage(cvSize(320,240),pFrame->depth,pFrame->nChannels);
//get file name
(void) time(&the_time);
tm_ptr=localtime(&the_time);
snprintf(filename, 40, "./Alert/%04d%02d%02d%02d%02d%02d.jpg",tm_ptr->tm_year-100+2000, tm_ptr->tm_mon + 1, tm_ptr->tm_mday, tm_ptr->tm_hour,tm_ptr->tm_min, tm_ptr->tm_sec);
cvResize(pFrame,pSaveFrame,CV_INTER_LINEAR);
cvSaveImage(filename,pSaveFrame);
cvReleaseImage(&pSaveFrame);
}
cvDestroyWindow("video");
cvReleaseCapture(&pCapture);
return 0;
}