OpenCV get multiple camera names and numbers

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.