這裡主要利用了Opencv打開攝像頭的代碼,以及寫入視頻的函數,只是這裡要注意的是攝像頭好像沒有幀率,在cvCreateVideoWriter,時要自己設置
1 #include"cv.h" 2 #include"highgui.h" 3 4 int main(int argc, char** argv) 5 { 6 cvNamedWindow("camera", CV_WINDOW_AUTOSIZE); 7 argc = 1; 8 CvCapture* capture; 9 capture = cvCreateCameraCapture(1);//如果不行換成-1,0,1試試 10 assert(capture != NULL); 11 IplImage* frame; 12 CvSize size = cvSize((int)cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH), 13 (int)cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT)); 14 15 double fps = cvGetCaptureProperty( 16 capture, 17 CV_CAP_PROP_FPS//幀率 18 ); 19 CvVideoWriter* writer = cvCreateVideoWriter( // On linux Will only work if you've installed ffmpeg development files correctly, 20 "D:\\Opencv\\testData\\camera.avi", // otherwise segmentation fault. Windows probably better. 21 CV_FOURCC('D', 'X', '5', '0'), 22 15,//攝像頭好像幀率獲取出來為0,故設為15 23 size 24 ); 25 IplImage* imgWrite = cvCreateImage(size, 8, 3); 26 while (1){ 27 frame = cvQueryFrame(capture); 28 if (!frame) 29 break; 30 char c = cvWaitKey(10); 31 if (c == 27) break; 32 cvCopy(frame, imgWrite); 33 cvWriteToAVI(writer, imgWrite); 34 cvShowImage("camera", frame); 35 } 36 37 cvReleaseCapture(&capture); 38 cvReleaseVideoWriter(&writer); 39 cvDestroyWindow("camera"); 40 return 0; 41 }