WIN7系统使用C++opencv4.5.5调用USB摄像头,USB摄像头灯亮一下,但无法开启摄像头,程序在其它电脑上能正常运行,使用QQ、微信、amcap能正常打开摄像头。请各位指点,是什么原因不能开启摄像头,解决方法?
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/video.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char* argv[])
{
//读取摄像头
VideoCapture capture(0,CAP_DSHOW);
// 设置参数
capture.set(CAP_PROP_FRAME_WIDTH, 680); // 宽度
capture.set(CAP_PROP_FRAME_HEIGHT, 480); // 高度
capture.set(CAP_PROP_FPS, 30);
capture.set(CAP_PROP_FOURCC, cv::VideoWriter::fourcc('M', 'J', 'P', 'G'));
cout << "width:" << capture.get(CAP_PROP_FRAME_WIDTH) << endl;
cout << "height:" << capture.get(CAP_PROP_FRAME_HEIGHT) << endl;
//是否读取成功进行判断
if(!capture.isOpened())
{
cout << "未打开摄像头!" << endl;
return -1;
}
while(true)
{
Mat frame;
capture>>frame;
if(frame.empty())break;
imshow("frame", frame);
int key = waitKey(10);
//cout << "key:" << key <<endl;
if(key == 27)break;
}
return 0;
}
运行结果:
width:0
height:0
未打开摄像头!
Process returned -1 (0xFFFFFFFF) execution time : 0.457 s
Press any key to continue.