1、其中C++获取多个摄像头的名称,然后根据名称对应其编号,从而可以使用opencv进行按编号打开特定的摄像头:
#include
#include "strmif.h"
#include
#include
#include
#pragma comment(lib, "setupapi.lib")
using namespace std;
#define VI_MAX_CAMERAS 20
DEFINE_GUID(CLSID_SystemDeviceEnum, 0x62be5d10, 0x60eb, 0x11d0, 0xbd, 0x3b, 0x00, 0xa0, 0xc9, 0x11, 0xce, 0x86);
DEFINE_GUID(CLSID_VideoInputDeviceCategory, 0x860bb310, 0x5d01, 0x11d0, 0xbd, 0x3b, 0x00, 0xa0, 0xc9, 0x11, 0xce, 0x86);
DEFINE_GUID(IID_ICreateDevEnum, 0x29840822, 0x5b84, 0x11d0, 0xbd, 0x3b, 0x00, 0xa0, 0xc9, 0x11, 0xce, 0x86);
//列出硬件设备
int listDevices(vector& list)
{
ICreateDevEnum *pDevEnum = NULL;
IEnumMoniker *pEnum = NULL;
int deviceCounter = 0;
CoInitialize(NULL);
HRESULT hr = CoCreateInstance(
CLSID_SystemDeviceEnum,
NULL,
CLSCTX_INPROC_SERVER,
IID_ICreateDevEnum,
reinterpret_cast(&pDevEnum)
);
if (SUCCEEDED(hr))
{
hr = pDevEnum->CreateClassEnumerator(CLSID_VideoInputDeviceCategory,&pEnum, 0);
if (hr == S_OK){
IMoniker *pMoniker = NULL;
while (pEnum->Next(1, &pMoniker, NULL) == S_OK)
{
IPropertyBag *pPropBag;
hr = pMoniker->BindToStorage(0, 0, IID_IPropertyBag,
(void**)(&pPropBag));
if (FAILED(hr)) {
pMoniker->Release();
continue; // Skip this one, maybe the next one will work.
}
VARIANT varName;
VariantInit(&varName);
hr = pPropBag->Read(L"Description", &varName, 0);
if (FAILED(hr))
{
hr = pPropBag->Read(L"FriendlyName", &varName, 0);
}
if (SUCCEEDED(hr))
{
hr = pPropBag->Read(L"FriendlyName", &varName, 0);
int count = 0;
char tmp[255] = { 0 };
while (varName.bstrVal[count] != 0x00 && count < 255)
{
tmp[count] = (char)varName.bstrVal[count];
count++;
}
list.push_back(tmp);
}
pPropBag->Release();
pPropBag = NULL;
pMoniker->Release();
pMoniker = NULL;
deviceCounter++;
}
pDevEnum->Release();
pDevEnum = NULL;
pEnum->Release();
pEnum = NULL;
}
}
return deviceCounter;
}
int main()
{
vector CameraName;//存储摄像头名称
int num = listDevices(CameraName);
cout << "摄像头个数:" << num << endl;
for (int i = 0; i < num; i++) {
cout << " ID: "<< i << " : " << " Name: "<
注意:经过多次测试,其获取相机的顺序跟opencv打开相机确定的顺序应当是一样的。
2、C#只能通过AForge.dll来获取摄像头信息,但是其存储相机的顺序跟opencv打开相机的顺序是不一样的。只能通过AForge.dll获取相机信息,并通过其获取的相机名称来打开相机。
1、检测电脑上的摄像头设备
#其中这个变量存储相机信息不是按照opencv打开相机的顺序存储的
FilterInfoCollection videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
//选择下拉框中的一个摄像头设备,其中MonikerString是对应相机的名称,
VideoCaptureDevice videoSource = new VideoCaptureDevice(videoDevices[tscbxCameras.SelectedIndex].MonikerString);