程序采用 opencv 接口获取笔记本摄像头,固需提前配置好 opencv 环境。
效果如下:
#include "cv.h" #include "highgui.h" #include "windows.h" using namespace std; int main(int argc, char** argv) { int f = 0, t = 0,n=0,c=0; cvNamedWindow("WebCam"); CvCapture* capture = cvCreateCameraCapture(0); //创建并获取摄像设备 IplImage* frame; IplImage* frame1; frame = cvQueryFrame(capture); //创建并获取一张摄像截图 frame1 = frame; while (1) { frame = cvQueryFrame(capture); //创建并获取一张摄像截图 if (!frame) { cvReleaseCapture(&capture); //释放摄像设备 CvCapture* capture = cvCreateCameraCapture(0); //创建并获取摄像设备 frame = cvQueryFrame(capture); //创建并获取一张摄像截图 if (!frame) { f = f + 1; cout << "对不起,获取摄像头失败..." << endl; } else { c = c + 1; cout << "恭喜,重新获取摄像头成功..." << endl; cvShowImage("WebCam", frame); } } else { t = t + 1; cout << "恭喜,获取摄像头成功..." << endl; cvShowImage("WebCam", frame); } cvShowImage("WebCam", frame); n = n + 1; cout << "----刷新 " << n << " 次----成功 " << t << " 次----重连成功 " << c << " 次----失败 " << f << " 次----" << endl; char c = cvWaitKey(50); if (c == 27) break; } cvReleaseCapture(&capture); //释放摄像设备 cvDestroyWindow("WebCam"); //释放窗口 return 0; }