Call the external camera in the c language, and you can save photos and record video

Due to project requirements, just started using opencv, have any questions please advise.

The following example relates to the goto statement.

Verify that the camera is a few numbers, simply modify capture (0);

See related articles on specific compiler environment to build a blog.

If the video does not appear correctly, make sure if it is a permissions issue, add sudo.

Please add or delay.

Just as taking pictures or recording video, your own crop.

 

 

#include <stdio.h>
#include <string>
#include <iostream>
#include <cstdio>
#include <fstream>
#include <vector>
#include <opencv2/opencv.hpp>
 
using namespace std;
using namespace cv;
 
int main()
{
	VideoCapture capture(0);

	if(!capture.isOpened())
	{
		return -1;
	}

	cout<<"********************"<<endl;
	cout<<"******拍照:a ******"<<endl;
	cout<<"***錄視頻開始:b ***"<<endl;
	cout<<"***錄視頻結束:c ***"<<endl;
	cout<<"******退出:q ******"<<endl;
	cout<<"********************"<<endl;

	//获得帧的宽高  
	int w = static_cast<int>(capture.get(CV_CAP_PROP_FRAME_WIDTH));
	int h = static_cast<int>(capture.get(CV_CAP_PROP_FRAME_HEIGHT));

	Mat frame,frame_write;
	
	int i = 0,j = 0;

	char image_name[20];
	while (1)
	{

		aa:if(!capture.read(frame))
			break;

		//capture >> frame;
		imshow("Video", frame);

		char key = (char)waitKey(10);
		//拍照
		//cout<<"4444444444444444"<<endl; 
		if(key=='a')
		{
			cout<<"開始拍照"<<endl;
			sprintf(image_name, "%s%d%s", "image", i, ".jpg");
			std::string str(image_name);
			cv::imwrite("./image/" +str, frame);
			waitKey(30);
			i++;
			cout<<"拍照ok"<<endl;			
			goto aa;	
		}
		//录视频
		else if(key=='b')
		{
			cout<<"開始錄視頻"<<endl;
			//保存视频名
			sprintf(image_name, "%s%d%s", "video", j, ".avi");
			std::string str(image_name);

			//视频写入对象
			VideoWriter outputVideo("./image/" +str, CV_FOURCC('X', 'V', 'I', 'D'), 30.0, Size(w, h));

			while(1)
			{
				//读取当前帧 
				if(!capture.read(frame))
					break;
				//capture >> frame;
	
				imshow("Video", frame);
				//写入文件
				outputVideo.write(frame); 

				char key1 = waitKey(10);
				if (key1 == 'q')
					return 0;
				else if (key1 == 'c')
					goto bb;
			}

			bb:j++;
			cout<<"錄視頻ok"<<endl;
			outputVideo.release();
			cvDestroyWindow("Video1"); 
			//goto cc;
			
		}
		//退出
		else if(key=='q')
		{
			cout<<"退出"<<endl;
			break;
		}
	}
	return 0;
}








/*
#include <stdio.h>
#include <string>
#include <iostream>
#include <cstdio>
#include <fstream>
#include <vector>
#include <opencv2/opencv.hpp>
 
using namespace std;
using namespace cv;
 
int main()
{
	VideoCapture capture(0);
	int i = 0;
	//std::string image_name;
	char image_name[20];
	while (1)
	{
		Mat frame;
		capture >> frame;
		imshow("dqsp", frame);
 


		//转换格式
		sprintf(image_name, "%s%d%s", "image", i, ".jpg");
		std::string str(image_name);
		
		cv::imwrite("./image/" +str, frame);
		waitKey(30);
		i++;
	}
	return 0;
}
*/




/*
#include "highgui.h"   
  
int main()  
{  
    CvCapture* pCap = cvCreateCameraCapture( 0 );//这里-1也可以,不过我的电脑装的有CyberLink YouCam软件,   
                                                 //OpenCV会默认调用该摄像头,而不调用系统的驱动   
    IplImage *frame = NULL;  
  
    if (cvCreateCameraCapture == NULL)  
    {  
        return(0);  
    }  
  
    cvNamedWindow("Camera",CV_WINDOW_FULLSCREEN);  
  
    while ((frame = cvQueryFrame(pCap)) != 0 &&  cvWaitKey(20) != 27)    
    {  
        frame = cvQueryFrame(pCap);  
        cvShowImage("Camera", frame);
	char key = (char)cvwaitKey(10);
	switch(key)
	{
		case 'a':imwrite("image",frame);
			break;
		default:
			break;
	}   
    }  
  
    cvReleaseCapture(&pCap);    
    cvDestroyWindow("Camera");    
    return (0);  
}  
*/

 

 

Published 26 original articles · won praise 34 · views 90000 +

Guess you like

Origin blog.csdn.net/qq_36662437/article/details/97628996