How to Get a List of Video Capture Devices (Web Cameras) on Windows? (C++)

How to get a list of video capture devices (web cameras) on windows? (C++)

From the examples shown, copy the following code into dev.c. Then open the command line with all the SDK variables set. At the command line link to ole32.lib and oleaut32.lib. It will then show you all the devices.

cl dev.c ole32.lib oleaut32.lib

dev.exe will give out the list on the command line.

#include <windows.h>
#include <dshow.h>

#pragma comment(lib, "strmiids")

HRESULT EnumerateDevices(REFGUID category, IEnumMoniker **ppEnum)
{
// Create the System Device Enumerator.
ICreateDevEnum *pDevEnum;
HRESULT hr = CoCreateInstance(CLSID_SystemDeviceEnum, NULL,
CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pDevEnum));

if (SUCCEEDED(hr))
{
// Create an enumerator for the category.
hr = pDevEnum->CreateClassEnumerator(category, ppEnum, 0);
if (hr == S_FALSE)
{
hr = VFW_E_NOT_FOUND; // The category is empty. Treat as an error.
}
pDevEnum->Release();
}
return hr;
}

void DisplayDeviceInformation(IEnumMoniker *pEnum)
{
IMoniker *pMoniker = NULL;

while (pEnum->Next(1, &pMoniker, NULL) == S_OK)
{
IPropertyBag *pPropBag;
HRESULT hr = pMoniker->BindToStorage(0, 0, IID_PPV_ARGS(&pPropBag));
if (FAILED(hr))
{
pMoniker->Release();
continue;
}

VARIANT var;
VariantInit(&var);

// Get description or friendly name.
hr = pPropBag->Read(L"Description", &var, 0);
if (FAILED(hr))
{
hr = pPropBag->Read(L"FriendlyName", &var, 0);
}
if (SUCCEEDED(hr))
{
printf("%S\n", var.bstrVal);
VariantClear(&var);
}

hr = pPropBag->Write(L"FriendlyName", &var);

// WaveInID applies only to audio capture devices.
hr = pPropBag->Read(L"WaveInID", &var, 0);
if (SUCCEEDED(hr))
{
printf("WaveIn ID: %d\n", var.lVal);
VariantClear(&var);
}

hr = pPropBag->Read(L"DevicePath", &var, 0);
if (SUCCEEDED(hr))
{
// The device path is not intended for display.
printf("Device path: %S\n", var.bstrVal);
VariantClear(&var);
}

pPropBag->Release();
pMoniker->Release();
}
}

void main()
{
HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
if (SUCCEEDED(hr))
{
IEnumMoniker *pEnum;

hr = EnumerateDevices(CLSID_VideoInputDeviceCategory, &pEnum);
if (SUCCEEDED(hr))
{
DisplayDeviceInformation(pEnum);
pEnum->Release();
}
hr = EnumerateDevices(CLSID_AudioInputDeviceCategory, &pEnum);
if (SUCCEEDED(hr))
{
DisplayDeviceInformation(pEnum);
pEnum->Release();
}
CoUninitialize();
}
}

How To Get A List Of Available Video Capture Devices

DirectShow.NET sample \Samples\Capture\DxLogo\Capture.cs shows how to do it:

// Get the collection of video devices
capDevices = DsDevice.GetDevicesOfCat(FilterCategory.VideoInputDevice);

The keyword you need is FilterCategory.VideoInputDevice.

See also:

  • Is directshow.net video input device filter enumeration broken (in newer environments)?
  • AForge - Working with video card with multiple cameras

How to get a list of video capture devices (web cameras) on linux ( ubuntu )? (C/C++)

This is a code snippet I had laying around. Probably from a book. I guess you could just iterate over all /dev/videoN nodes and get the info.

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/videodev.h>

int main(){
int fd;
struct video_capability video_cap;
struct video_window video_win;
struct video_picture video_pic;

if((fd = open("/dev/video0", O_RDONLY)) == -1){
perror("cam_info: Can't open device");
return 1;
}

if(ioctl(fd, VIDIOCGCAP, &video_cap) == -1)
perror("cam_info: Can't get capabilities");
else {
printf("Name:\t\t '%s'\n", video_cap.name);
printf("Minimum size:\t%d x %d\n", video_cap.minwidth, video_cap.minheight);
printf("Maximum size:\t%d x %d\n", video_cap.maxwidth, video_cap.maxheight);
}

if(ioctl(fd, VIDIOCGWIN, &video_win) == -1)
perror("cam_info: Can't get window information");
else
printf("Current size:\t%d x %d\n", video_win.width, video_win.height);

if(ioctl(fd, VIDIOCGPICT, &video_pic) == -1)
perror("cam_info: Can't get picture information");
else
printf("Current depth:\t%d\n", video_pic.depth);

close(fd);
return 0;
}

How can I get a list of camera devices from my PC C#

I've done this before - use http://directshownet.sourceforge.net/ to give you a decent .net interface to DirectShow, then you can just use the following code:

  DsDevice[] captureDevices;

// Get the set of directshow devices that are video inputs.
captureDevices = DsDevice.GetDevicesOfCat(FilterCategory.VideoInputDevice);

for (int idx = 0; idx < captureDevices.Length; idx++)
{
// Do something with the device here...
}

C++ OpenCV 2.4.11: List all cameras

There is still no any functionality related to camera count in OpenCV at the current moment (3.0.0 version) - see corresponding ticket.

Proper camera handling seems like OpenCV internal problem (for example, described here or here). Usually it appears in capture code after physically disabling camera while it is still opened in OpenCV (when we try to read destroyed file descriptor).

Generally you can even implement your own handler for access violations (please look into this thread), but it's really dirty trick.

How to get a list video capture devices NAMES (web cameras) on linux ( ubuntu )? (C/C++)

You are using the V4L1 API which is deprecated - V4L2 is the preferred API for new code.

In any case, the VIDIOC_QUERYCAP ioctl() is probably what you are looking for. You will want to have a look at the .card field of the returned struct v4l2_capability structure.

EDIT:

You could have a look at the source code for the v4l-info utility, which does exactly what you want.

LibVLCSharp how to get list of webcams

Your MediaDiscoverer is empty because you never call md.Start().
For more info, I found this really helpful: /LibVLCSharp/MediaDiscoverer.cs

That being said, I had no success using MediaDiscoverer to look for webcams myself.

If you don't insist on using LibVLC, you can list all camera devices without any third party software: How can I get a list of camera devices from my PC C#

from Francesco Bonizzi:

public static List<string> GetAllConnectedCameras()
{
var cameraNames = new List<string>();
using (var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PnPEntity WHERE (PNPClass = 'Image' OR PNPClass = 'Camera')"))
{
foreach (var device in searcher.Get())
{
cameraNames.Add(device["Caption"].ToString());
}
}

return cameraNames;
}

How to let user select a video recording device (web-cam) with OpenCV?

Try using some OS functions to enumerate webcams. It might take some work, but this approach will guarantee that you get a list every time (unlike the OpenCV hack, which sometimes doesn't work, for some reason).



Related Topics



Leave a reply



Submit