出处:利用摄像头拍照并保存——http://blog.csdn.net/shy_big_boy/article/details/60594444。
环境
Win10,VS2015+opencv3.3.1
代码:
#include<opencv2\opencv.hpp>
#include<opencv2\highgui\highgui.hpp>
#include<opencv2\imgproc\imgproc.hpp>
#include<iostream>
#include<stdio.h>
using namespace cv;
using namespace std;
int main(int argc, char* argv)
{
VideoCapture capture(0);
Mat frame;
if (!capture.isOpened())
{
cout << "摄像头打开失败!" << endl;
return -1;
}
char key;
char filename[200];
int count = 0;
namedWindow("【视频】", 1);
namedWindow("【图片】", 1);
while (1)
{
key = waitKey(50);
capture >> frame;
imshow("【视频】", frame);
if (key == 27)
break;//按ESC键退出程序
if (key == 32)//按空格键进行拍照
{
sprintf_s(filename, "Picture_%d.jpg", ++count);
imwrite(filename, frame);//图片保存到本工程目录中
imshow("【图片】", frame);
}
}
return 0;
}