//使用OpenCV實(shí)現(xiàn)獲取攝像頭數(shù)據(jù)并毫秒級(jí)(在我的機(jī)上70ms左右保存一張,連續(xù)保存)保存成一張jpg圖片,jpg圖片的大小可調(diào)。
#include<stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include<time.h>
#include <sys/timeb.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;
struct timeb tp;
//創(chuàng)建窗口
cvNamedWindow("video",1);
//打開(kāi)攝像頭
if( !(pCapture=cvCaptureFromCAM(-1)) )
{
fprintf(stderr,"Can not open camera.\n");
return -1;
}
if(mkdir("./Alert",0755)==-1)
{
//創(chuàng)建時(shí)存在該目錄會(huì)返回錯(cuò)誤碼,如不存在則創(chuàng)建它,但由于我們需要該目錄,則出錯(cuò)也不處理
}
//逐幀讀取視頻
while(pFrame=cvQueryFrame(pCapture))
{
cvShowImage("video",pFrame);
if(cvWaitKey(2)>=0) break;
pSaveFrame=cvCreateImage(cvSize(320,240),pFrame->depth,pFrame->nChannels);
//get file name
ftime(&tp);
tm_ptr=localtime(&tp.time);
snprintf(filename, 40, "./Alert/%04d%02d%02d%02d%02d%02d%04d.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,tp.millitm);
cvResize(pFrame,pSaveFrame,CV_INTER_LINEAR);
cvSaveImage(filename,pSaveFrame);
cvReleaseImage(&pSaveFrame);
}
cvDestroyWindow("video");
cvReleaseCapture(&pCapture);
return 0;
} |