how to detect front and back camera in window tablet in winforms C#

2.2k views Asked by At

I'm using aforge in winforms for video recording. I wanna to record video both (front and back camera) in window tablet. I have able to find list of Video Capture Device but not identify which is front or rear camera.

Name space whose I use:

using AForge.Video;
using AForge.Video.DirectShow;

I have sawed you my finding video capture device snippet of code:

public VideoCaptureDevice cam = null;
public FilterInfoCollection usbCams;

...

usbCams = new FilterInfoCollection(FilterCategory.VideoInputDevice);

if (usbCams.Count == 1)
{
    cam = new VideoCaptureDevice(usbCams[0].MonikerString);
}
else if (usbCams.Count == 2)
{
    cam = new VideoCaptureDevice(usbCams[1].MonikerString);
}

cam.NewFrame += new NewFrameEventHandler(cam_NewFrame);

cam.Start();

...

private void cam_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
    Bitmap bitmap = (Bitmap)eventArgs.Frame.Clone();
    ImgContainer.Image = bitmap;
}

I have tested in different devices like iball and lenovo yoga 8. I have found one reason is that in Iball tablet return first front camera name and second rear camera but in lenovo tablet return first rear camera and second front camera. I have total confused. How to identify front and rear camera?

2

There are 2 answers

0
Stan Wijckmans On

A hack is to check the capabilities and set the camera with the highest resolution as back camera. If there is a difference in quality than the back camera is usually the better one.

This won't work always, for example the Surface Pro 1 has 2 cameras with the same resolution.

0
Mulflar On

usbCams is a list with all the available camera devices in your machine. With this:

if (usbCams.Count == 1)
{
    cam = new VideoCaptureDevice(usbCams[0].MonikerString);
}
else if (usbCams.Count == 2)
{
    cam = new VideoCaptureDevice(usbCams[1].MonikerString);
}

What you are doing is, if there is only one device use the first one, and if there are two devices use the second one, but it's not sure that the second one will be the rear or the front. What I've done in my app is use a selector and fill it with all the available devices, and let the user choose. Something like:

List<string> camaras = new List<string>();
foreach (FilterInfo item in usbCams)
{
    camaras.Add(item.Name);
}
comboBox.ItemsSource = camaras;

And when the combobox change the selection

cam.Stop();
cam= new VideoCaptureDevice(LoaclWebCamsCollection[comboBox.SelectedIndex].MonikerString);
cam.NewFrame += new NewFrameEventHandler(cam_NewFrame);
cam.Start();

I don't have enough devices now to test the order of the cams, but it can be that ther are ordered by name device, or by driver installation priority or whatever.