#include<stdio.h>#include<opencv2/opencv.hpp>#include<iostream>
using namespace std;
using namespace cv;intmain(int argc,char** argv){
Mat frame;
VideoCapture capture(0);namedWindow("My_Camera");while(1){if(capture.isOpened()){
capture>>frame;imshow("My_Camera",frame);char c=waitKey(30);if(c==27)break;}else{
cout<<"No Camera Input!"<<endl;break;}}return0;}
2、调用摄像头+空格键截图保存图片
#include<opencv2\core\core.hpp>#include<opencv2\highgui\highgui.hpp>#include<opencv2\imgproc\imgproc.hpp>#include<iostream>
using namespace std;
using namespace cv;#define camera_name "摄像头"#define picwindow_name "截图图片"intmain(){
VideoCapture capture(0);if(!capture.isOpened()){
cout <<"摄像头打开失败!"<< endl;return0;}
Mat frame;char key;//按键char picture_name[200];//用于存放保存图片的文件名int i =0;//计数namedWindow(camera_name);namedWindow(picwindow_name);while(1){
key =waitKey(30);
capture >> frame;imshow(camera_name, frame);if(key ==27){break;//ESC键退出}if(key ==32){sprintf(picture_name,"C:\\DZH\\pic%d.jpg", i++);//sprintf(picture_name, "pic%d.jpg", i++);//若保存在根目录,这样即可imwrite(picture_name, frame);imshow(picwindow_name, frame);}}return0;}