Because the project needs to use OPENCV to read multiple cameras, but can not determine the number of the camera. View OpenCV source, the camera's ID is mainly used listdevices this function, the function itself to extract out, according to the vector<> in order to get the camera ID.
int listdevices (vector<string>& list) {//com Library intialization//cominit ();
if (!silent) printf ("\nvideoinput SPY mode!\n\n");
Icreatedevenum *pdevenum = NULL;
IEnumMoniker *penum = NULL;
int devicecounter = 0;
CoInitialize (NULL); HRESULT hr = CoCreateInstance (Clsid_systemdeviceenum, NULL, Clsctx_inproc_server, Iid_icreatedevenum, Reinterpret_cas
T<void**> (&pdevenum));
if (SUCCEEDED (HR)) {//Create a enumerator for the video capture category.
hr = Pdevenum->createclassenumerator (clsid_videoinputdevicecategory, &penum, 0);
if (hr = S_OK) {//if (!silent) printf ("Setup:looking for Capture devices\n");
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 would worK.}//Find the description or friendly name.
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};
int maxlen = sizeof (devicenames[0])/sizeof (Devicenames[0][0])-2;
while (Varname.bstrval[count]! = 0x00 && Count < 255) {Tmp[count] = (char) varname.bstrval[count];
count++;
} list.push_back (TMP);
Devicenames[devicecounter][count] = 0;
if (!silent) printf ("SETUP:%i)%s \ n", Devicecounter, Devicenames[devicecounter]);
} ppropbag->release ();
Ppropbag = NULL;
Pmoniker->release ();
Pmoniker = NULL;
devicecounter++;
} pdevenum->release ();
Pdevenum = NULL;
Penum->release ();
Penum = NULL; }//iF (!silent) printf ("SETUP:%i Device (s) found\n\n", devicecounter);
}//comuninit ();
return devicecounter;
}
The results are as follows:
Based on this number, you can choose the camera more flexibly. method is more stupid, barely usable.
Test source