The following forum(s) have migrated to Microsoft Q&A (Preview): Developing Universal Windows apps!
Visit Microsoft Q&A (Preview) to post new questions.

Learn More

 locked
[UWP]Select a Camera on windows phone for capturing video RRS feed

  • Question

  • Hi there,

    I´m trying to capture a Video on a Smartphone Windows10 (Lumia 950xl). This I do by using VB. Getting a live-stream is working so far. But choosing the front or the back camera is not possible. I appreciate any help... 

    Does anybody have an idea how to solve this. Above the Code of the live-Streaming:

         Dim videocams As DeviceInformationCollection = Await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture)
            If videocams.Count = 0 Then
                lbl_Status.Text = "Status: keine Kamera gefunden!"
                Await function_NoCamera()    ' only if no camera is present, tehen message to user
                Exit Sub
            Else
                lbl_Status.Text = "Status: " + videocams.Count.ToString + " Kameras gefunden!" + videocams.Item(1).Name + videocams.Item(1).Name
            End If
            Dim mcis As New MediaCaptureInitializationSettings()
            ' Nutzen der Gerätekamera für ein Livestreaming als Video
            ' es kann derzeit nur die vordere Kamera genutzt werden
            If videocams.Count > 0 Then
                Medcap = New MediaCapture()
                mcis.VideoDeviceId = 1 'videocams(1).EnclosureLocation.Panel.Back
                Await Medcap.InitializeAsync()
                CaptureElement1.Source = Medcap
            Else
                Exit Sub
            End If
            lbl_Status.Text = "es wird die Kamera verwendet: " + videocams(1).EnclosureLocation.Panel.ToString
            lbl_Status.UpdateLayout()
            Try
                Await Medcap.StartPreviewAsync()
                Medcap.SetPreviewRotation(VideoRotation.Clockwise270Degrees)
            Catch ex As Exception
                lbl_Status.Text = ex.ToString
                lbl_Status.UpdateLayout()
            End Try


    • Edited by Krunal Parekh Thursday, June 2, 2016 5:51 AM title tag
    Wednesday, June 1, 2016 5:22 PM

Answers

  • When Await Medcap.InitializeAsync() you can pass in a MediaCaptureInitializationSettings setting object, by there you could set correct camera device id to use the front or back camera.

    • Edited by Jamles Hez Thursday, June 2, 2016 2:20 AM
    • Marked as answer by PeLuTPro Tuesday, June 7, 2016 3:21 PM
    Thursday, June 2, 2016 2:17 AM
  • Hello PeLuTPro,

    Please see: Basic camera app sample

    See Scenario 2. Acquire a camera located on a specific side of the device. In this case, the sample attempts to get the rear camera.

    See the below function in code.

             Private Shared Async Function FindCameraDeviceByPanelAsync(desiredPanel As Windows.Devices.Enumeration.Panel) As Task(Of DeviceInformation) 
                 ' Get available devices for capturing pictures 
                 Dim allVideoDevices = Await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture) 
                 ' Get the desired camera by panel 
                 Dim desiredDevice As DeviceInformation = allVideoDevices.FirstOrDefault(Function(x) x.EnclosureLocation IsNot Nothing AndAlso x.EnclosureLocation.Panel = desiredPanel) 
                 Return If(desiredDevice, allVideoDevices.FirstOrDefault()) 
             End Function 

    With Regards,

    Krunal Parekh


    Thanks MSDN Community Support Please remember to Mark as Answer the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    • Marked as answer by PeLuTPro Tuesday, June 7, 2016 3:20 PM
    Thursday, June 2, 2016 5:49 AM
  • Hi PeluTPro ,

    The code I will show you is only for demo just to give you a clue on what is going on.

    In my XAML page I have a Combobox which I load all the VideoCapture devices.

    With the below I call and find the cameras that my device supports.

    The EnumedDeviceList2 is the Combobox

     private DeviceInformationCollection m_devInfoCollection;

     private async void EnumerateWebcamsAsync()
            {
                try
                {
                    m_devInfoCollection = null;
                    EnumedDeviceList2.Items.Clear();
                    m_devInfoCollection = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);

                    if (m_devInfoCollection.Count == 0)
                    {
                      //  app.MessageBox1(app.AppTitle, "No Devices Found!", 0);
                      //  this.DisplaySettingsScreen();
                        return;
                    }
                    else
                    {
                        for (int i = 0; i < m_devInfoCollection.Count; i++)
                        {
                            var devInfo = m_devInfoCollection[i];
                            EnumedDeviceList2.Items.Add(devInfo.Name + "-" + i.ToString());
                        }

                        if (m_devInfoCollection.Count == 2)
                        {

                              //Activate BackCamera

                             EnumedDeviceList2.SelectedIndex = 0;

                        }
                        else
                        { 

                          //Activate Front Camera

                            EnumedDeviceList2.SelectedIndex = 1;
                        }

                                                                  
                    }
                }
                catch (Exception e)
                {
                    app.MessageBox1(app.AppTitle, e.Message, 0);
                }
            }

    As you can see from above the API returns the total cameras that the device supports. I'm talking about the embedded ones. The zero is the back camera , the One is the front camera. If you have attached more cameras in your device debug the for...loop code and check out the device information. Since now you know that zero is back camera and 1 is the front and according to the basic sample you can run the below and initialize the camera.

    For example

    Put somewhere in your code that if your combo is filled with data when you find the cameras start then the device.

    if(EnumedDeviceList2.SelectedIndex >= 0)

    {

    btnStartDevice_Click(null, null);

    }

    and in your XAML the below. FYI the below code is taken from the Sample with some modifications by me.

    <ComboBoxx:Name="EnumedDeviceList2"SelectionChanged="lstEnumedDevices_SelectionChanged"Visibility="Collapsed"Height="40"Margin="1040,125,-461,10"/>

    if(m_bPreviewing)

    {

    awaitm_mediaCaptureMgr.StopPreviewAsync();

    m_bPreviewing = false;

    }

    m_bRecording = false;

    previewElement2.Source = null;

    if(EnumedDeviceList2.SelectedIndex >= 0)

    {

    btnStartDevice_Click(null, null);

    }

    else

    {

    app.MessageBox1(app.AppTitle, "No Devices were found!", 0);

    this.DisplaySettingsScreen();

    return;

    }

    And finally the start device method

    m_mediaCaptureMgr = newWindows.Media.Capture.MediaCapture();

    varsettings = newWindows.Media.Capture.MediaCaptureInitializationSettings();

    varchosenDevInfo = m_devInfoCollection[EnumedDeviceList2.SelectedIndex];

    settings.VideoDeviceId = chosenDevInfo.Id;

    //added below two lines

    settings.StreamingCaptureMode = StreamingCaptureMode.Video;

    settings.PhotoCaptureSource = PhotoCaptureSource.VideoPreview;

    awaitm_mediaCaptureMgr.InitializeAsync(settings);

    previewCanvas2.Visibility = Windows.UI.Xaml.Visibility.Visible;

    previewElement2.Source = m_mediaCaptureMgr;

    awaitm_mediaCaptureMgr.StartPreviewAsync();

    m_bPreviewing = true;

    So what we did so far is.

    1. We Found all the devices and load combobox with the values.

    2.We placed the Combo in our XAML page and added some code in the SelectionChanged event just to give the user the option to change the camera

    3. Since we know from the enumeration that Zero is the back camera, one is the front now we can write the method which initializes the device passing the deviceid we want. That is why you must pay attention before the Initialize the device id. The critical job to do the job is the below

    varsettings = newWindows.Media.Capture.MediaCaptureInitializationSettings();

    varchosenDevInfo = m_devInfoCollection[EnumedDeviceList2.SelectedIndex];//we select from the combo the device we want and we place in the settings the DeviceId.

    settings.VideoDeviceId = chosenDevInfo.Id;

     settings.StreamingCaptureMode = StreamingCaptureMode.Video;

    settings.PhotoCaptureSource = PhotoCaptureSource.VideoPreview;

    awaitm_mediaCaptureMgr.InitializeAsync(settings);

    Don't forget the rest of the code to show the preview etc. Take a look at the Basic Camera Sample for the rest.

    If you have one camera you will see it from the Count that the FindAllAsync returns. So you might want to check on how many cameras a device has (perhaps you have attached a usb camera). That is why you need the Combo filled with the values (deviceid) so you can give the option to the user to select the camera he wants.

    By default and if you first check the system if Camera Devices are found you can open the first one. Meaning by this the zero based camera which is usually the Back Camera.

    Hope I helped you.

    Thank you

     

    • Proposed as answer by zakkar Tuesday, June 7, 2016 11:14 AM
    • Marked as answer by PeLuTPro Tuesday, June 7, 2016 3:19 PM
    Thursday, June 2, 2016 8:34 AM

All replies

  • When Await Medcap.InitializeAsync() you can pass in a MediaCaptureInitializationSettings setting object, by there you could set correct camera device id to use the front or back camera.

    • Edited by Jamles Hez Thursday, June 2, 2016 2:20 AM
    • Marked as answer by PeLuTPro Tuesday, June 7, 2016 3:21 PM
    Thursday, June 2, 2016 2:17 AM
  • Hello PeLuTPro,

    Please see: Basic camera app sample

    See Scenario 2. Acquire a camera located on a specific side of the device. In this case, the sample attempts to get the rear camera.

    See the below function in code.

             Private Shared Async Function FindCameraDeviceByPanelAsync(desiredPanel As Windows.Devices.Enumeration.Panel) As Task(Of DeviceInformation) 
                 ' Get available devices for capturing pictures 
                 Dim allVideoDevices = Await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture) 
                 ' Get the desired camera by panel 
                 Dim desiredDevice As DeviceInformation = allVideoDevices.FirstOrDefault(Function(x) x.EnclosureLocation IsNot Nothing AndAlso x.EnclosureLocation.Panel = desiredPanel) 
                 Return If(desiredDevice, allVideoDevices.FirstOrDefault()) 
             End Function 

    With Regards,

    Krunal Parekh


    Thanks MSDN Community Support Please remember to Mark as Answer the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    • Marked as answer by PeLuTPro Tuesday, June 7, 2016 3:20 PM
    Thursday, June 2, 2016 5:49 AM
  • Hi PeluTPro ,

    The code I will show you is only for demo just to give you a clue on what is going on.

    In my XAML page I have a Combobox which I load all the VideoCapture devices.

    With the below I call and find the cameras that my device supports.

    The EnumedDeviceList2 is the Combobox

     private DeviceInformationCollection m_devInfoCollection;

     private async void EnumerateWebcamsAsync()
            {
                try
                {
                    m_devInfoCollection = null;
                    EnumedDeviceList2.Items.Clear();
                    m_devInfoCollection = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);

                    if (m_devInfoCollection.Count == 0)
                    {
                      //  app.MessageBox1(app.AppTitle, "No Devices Found!", 0);
                      //  this.DisplaySettingsScreen();
                        return;
                    }
                    else
                    {
                        for (int i = 0; i < m_devInfoCollection.Count; i++)
                        {
                            var devInfo = m_devInfoCollection[i];
                            EnumedDeviceList2.Items.Add(devInfo.Name + "-" + i.ToString());
                        }

                        if (m_devInfoCollection.Count == 2)
                        {

                              //Activate BackCamera

                             EnumedDeviceList2.SelectedIndex = 0;

                        }
                        else
                        { 

                          //Activate Front Camera

                            EnumedDeviceList2.SelectedIndex = 1;
                        }

                                                                  
                    }
                }
                catch (Exception e)
                {
                    app.MessageBox1(app.AppTitle, e.Message, 0);
                }
            }

    As you can see from above the API returns the total cameras that the device supports. I'm talking about the embedded ones. The zero is the back camera , the One is the front camera. If you have attached more cameras in your device debug the for...loop code and check out the device information. Since now you know that zero is back camera and 1 is the front and according to the basic sample you can run the below and initialize the camera.

    For example

    Put somewhere in your code that if your combo is filled with data when you find the cameras start then the device.

    if(EnumedDeviceList2.SelectedIndex >= 0)

    {

    btnStartDevice_Click(null, null);

    }

    and in your XAML the below. FYI the below code is taken from the Sample with some modifications by me.

    <ComboBoxx:Name="EnumedDeviceList2"SelectionChanged="lstEnumedDevices_SelectionChanged"Visibility="Collapsed"Height="40"Margin="1040,125,-461,10"/>

    if(m_bPreviewing)

    {

    awaitm_mediaCaptureMgr.StopPreviewAsync();

    m_bPreviewing = false;

    }

    m_bRecording = false;

    previewElement2.Source = null;

    if(EnumedDeviceList2.SelectedIndex >= 0)

    {

    btnStartDevice_Click(null, null);

    }

    else

    {

    app.MessageBox1(app.AppTitle, "No Devices were found!", 0);

    this.DisplaySettingsScreen();

    return;

    }

    And finally the start device method

    m_mediaCaptureMgr = newWindows.Media.Capture.MediaCapture();

    varsettings = newWindows.Media.Capture.MediaCaptureInitializationSettings();

    varchosenDevInfo = m_devInfoCollection[EnumedDeviceList2.SelectedIndex];

    settings.VideoDeviceId = chosenDevInfo.Id;

    //added below two lines

    settings.StreamingCaptureMode = StreamingCaptureMode.Video;

    settings.PhotoCaptureSource = PhotoCaptureSource.VideoPreview;

    awaitm_mediaCaptureMgr.InitializeAsync(settings);

    previewCanvas2.Visibility = Windows.UI.Xaml.Visibility.Visible;

    previewElement2.Source = m_mediaCaptureMgr;

    awaitm_mediaCaptureMgr.StartPreviewAsync();

    m_bPreviewing = true;

    So what we did so far is.

    1. We Found all the devices and load combobox with the values.

    2.We placed the Combo in our XAML page and added some code in the SelectionChanged event just to give the user the option to change the camera

    3. Since we know from the enumeration that Zero is the back camera, one is the front now we can write the method which initializes the device passing the deviceid we want. That is why you must pay attention before the Initialize the device id. The critical job to do the job is the below

    varsettings = newWindows.Media.Capture.MediaCaptureInitializationSettings();

    varchosenDevInfo = m_devInfoCollection[EnumedDeviceList2.SelectedIndex];//we select from the combo the device we want and we place in the settings the DeviceId.

    settings.VideoDeviceId = chosenDevInfo.Id;

     settings.StreamingCaptureMode = StreamingCaptureMode.Video;

    settings.PhotoCaptureSource = PhotoCaptureSource.VideoPreview;

    awaitm_mediaCaptureMgr.InitializeAsync(settings);

    Don't forget the rest of the code to show the preview etc. Take a look at the Basic Camera Sample for the rest.

    If you have one camera you will see it from the Count that the FindAllAsync returns. So you might want to check on how many cameras a device has (perhaps you have attached a usb camera). That is why you need the Combo filled with the values (deviceid) so you can give the option to the user to select the camera he wants.

    By default and if you first check the system if Camera Devices are found you can open the first one. Meaning by this the zero based camera which is usually the Back Camera.

    Hope I helped you.

    Thank you

     

    • Proposed as answer by zakkar Tuesday, June 7, 2016 11:14 AM
    • Marked as answer by PeLuTPro Tuesday, June 7, 2016 3:19 PM
    Thursday, June 2, 2016 8:34 AM