使用qt的QCameraInfo::availableCameras()一直获取到的摄像头个数都是0(本机上共有5个摄像头),使用qt自带的例子获取摄像头列表又获取得到,不知哪里出了问题。无奈只能网上查找。以下网址使用directshow获取摄像头个数和摄像头名字:里面的代码可下载来使用,只是里面有个包含的头文件#include <cxcore.h>必须工程包含opencv的目录才能打开,否则编译不通过。但是单单获取摄像头个数和名称不需要用到这里面的这么多信息,因此单独取了这2个函数出来,如下关键代码处。
关键代码为:
#pragma comment(lib,"Strmiids.lib")
int CameraCount()
{
int count = 0;
CoInitialize(NULL);
// enumerate all video capture devices
CComPtr<ICreateDevEnum> pCreateDevEnum;
HRESULT hr = CoCreateInstance(CLSID_SystemDeviceEnum,