Setting CaptureElement source null exception











up vote
0
down vote

favorite












I'm trying to get a simple test working to learn how to setup and use multiple USB video devices at once in a Universal Windows project (later to be used on a raspberry Pi running windows 10 IoT core). I want the UI to display a preview of the selected camera. I have successfully followed the preview example on the MediaCapture site HERE



I have a 'TakePicture' class that will later be used for other things, but is currently used to find and initialize the video devices attached to the system as follows



public static class TakePicture
{
private static List<CaptureElement> _captureElements;
private static List<MediaCapture> _mediaCaptures;
public static DeviceInformationCollection devices;
private static int currentDevice = 0;

public static async Task InitializeDevice()
{
if (currentDevice >= _captureElements.Count)
{
_mediaCaptures.Add(new MediaCapture());
var captureElement = new CaptureElement{Stretch = Stretch.UniformToFill};
_captureElements.Add(captureElement);
await _mediaCaptures[currentDevice].InitializeAsync(
new MediaCaptureInitializationSettings
{
VideoDeviceId = devices[currentDevice].Id,
StreamingCaptureMode = StreamingCaptureMode.Video

});
_captureElements[currentDevice].Source = _mediaCaptures[currentDevice];
}
}

public static void IncrimentDevice()
{

currentDevice = (currentDevice + 1) % devices.Count;
}



public static async Task<MediaCapture> getPreviewElement()
{
CaptureElement element = new CaptureElement();
await FindDevices();
await InitializeDevice();
return _mediaCaptures[currentDevice];

}


public static async Task FindDevices()
{
if (devices == null)
{
devices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);
_captureElements = new List<CaptureElement>();
_mediaCaptures = new List<MediaCapture>();
}
}


}


I want to return a MedaCapture object related to a camera and use that to set the source of the CaptureElement in the GUI as follows:



private async Task StartPreviewAsync()
{
MediaCapture capture = await TakePicture.getPreviewElement();
await capture.StartPreviewAsync();
PreviewImage.Source = capture;
PreviewImage.Visibility = Visibility.Visible;
}


the 'PreviewImage' is a reference to a capture element in the UI:



<CaptureElement Name="PreviewImage" Stretch="Uniform" HorizontalAlignment="Center" Height="649" Margin="0,84,0,0" VerticalAlignment="Top" Width="1147"/>


BUT every time I try and set the source to the MediaCapture element I get a null reference exception. I have checked in the debugger that this object is not null and has the ID of a connected device, further to this when I run



await capture.StartPreviewAsync(); 


the green light below the camera lights up on the camera. If i try to re-initialize the MediaCapture object returned by GetPreviewElement it throws an exception that the device is already initialized too.



Am i approaching setting up multiple devices correctly? is passing a copy of the initialized MediaCapture a valid way of setting the source of the CaptureElement?



Many thanks!










share|improve this question


























    up vote
    0
    down vote

    favorite












    I'm trying to get a simple test working to learn how to setup and use multiple USB video devices at once in a Universal Windows project (later to be used on a raspberry Pi running windows 10 IoT core). I want the UI to display a preview of the selected camera. I have successfully followed the preview example on the MediaCapture site HERE



    I have a 'TakePicture' class that will later be used for other things, but is currently used to find and initialize the video devices attached to the system as follows



    public static class TakePicture
    {
    private static List<CaptureElement> _captureElements;
    private static List<MediaCapture> _mediaCaptures;
    public static DeviceInformationCollection devices;
    private static int currentDevice = 0;

    public static async Task InitializeDevice()
    {
    if (currentDevice >= _captureElements.Count)
    {
    _mediaCaptures.Add(new MediaCapture());
    var captureElement = new CaptureElement{Stretch = Stretch.UniformToFill};
    _captureElements.Add(captureElement);
    await _mediaCaptures[currentDevice].InitializeAsync(
    new MediaCaptureInitializationSettings
    {
    VideoDeviceId = devices[currentDevice].Id,
    StreamingCaptureMode = StreamingCaptureMode.Video

    });
    _captureElements[currentDevice].Source = _mediaCaptures[currentDevice];
    }
    }

    public static void IncrimentDevice()
    {

    currentDevice = (currentDevice + 1) % devices.Count;
    }



    public static async Task<MediaCapture> getPreviewElement()
    {
    CaptureElement element = new CaptureElement();
    await FindDevices();
    await InitializeDevice();
    return _mediaCaptures[currentDevice];

    }


    public static async Task FindDevices()
    {
    if (devices == null)
    {
    devices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);
    _captureElements = new List<CaptureElement>();
    _mediaCaptures = new List<MediaCapture>();
    }
    }


    }


    I want to return a MedaCapture object related to a camera and use that to set the source of the CaptureElement in the GUI as follows:



    private async Task StartPreviewAsync()
    {
    MediaCapture capture = await TakePicture.getPreviewElement();
    await capture.StartPreviewAsync();
    PreviewImage.Source = capture;
    PreviewImage.Visibility = Visibility.Visible;
    }


    the 'PreviewImage' is a reference to a capture element in the UI:



    <CaptureElement Name="PreviewImage" Stretch="Uniform" HorizontalAlignment="Center" Height="649" Margin="0,84,0,0" VerticalAlignment="Top" Width="1147"/>


    BUT every time I try and set the source to the MediaCapture element I get a null reference exception. I have checked in the debugger that this object is not null and has the ID of a connected device, further to this when I run



    await capture.StartPreviewAsync(); 


    the green light below the camera lights up on the camera. If i try to re-initialize the MediaCapture object returned by GetPreviewElement it throws an exception that the device is already initialized too.



    Am i approaching setting up multiple devices correctly? is passing a copy of the initialized MediaCapture a valid way of setting the source of the CaptureElement?



    Many thanks!










    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I'm trying to get a simple test working to learn how to setup and use multiple USB video devices at once in a Universal Windows project (later to be used on a raspberry Pi running windows 10 IoT core). I want the UI to display a preview of the selected camera. I have successfully followed the preview example on the MediaCapture site HERE



      I have a 'TakePicture' class that will later be used for other things, but is currently used to find and initialize the video devices attached to the system as follows



      public static class TakePicture
      {
      private static List<CaptureElement> _captureElements;
      private static List<MediaCapture> _mediaCaptures;
      public static DeviceInformationCollection devices;
      private static int currentDevice = 0;

      public static async Task InitializeDevice()
      {
      if (currentDevice >= _captureElements.Count)
      {
      _mediaCaptures.Add(new MediaCapture());
      var captureElement = new CaptureElement{Stretch = Stretch.UniformToFill};
      _captureElements.Add(captureElement);
      await _mediaCaptures[currentDevice].InitializeAsync(
      new MediaCaptureInitializationSettings
      {
      VideoDeviceId = devices[currentDevice].Id,
      StreamingCaptureMode = StreamingCaptureMode.Video

      });
      _captureElements[currentDevice].Source = _mediaCaptures[currentDevice];
      }
      }

      public static void IncrimentDevice()
      {

      currentDevice = (currentDevice + 1) % devices.Count;
      }



      public static async Task<MediaCapture> getPreviewElement()
      {
      CaptureElement element = new CaptureElement();
      await FindDevices();
      await InitializeDevice();
      return _mediaCaptures[currentDevice];

      }


      public static async Task FindDevices()
      {
      if (devices == null)
      {
      devices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);
      _captureElements = new List<CaptureElement>();
      _mediaCaptures = new List<MediaCapture>();
      }
      }


      }


      I want to return a MedaCapture object related to a camera and use that to set the source of the CaptureElement in the GUI as follows:



      private async Task StartPreviewAsync()
      {
      MediaCapture capture = await TakePicture.getPreviewElement();
      await capture.StartPreviewAsync();
      PreviewImage.Source = capture;
      PreviewImage.Visibility = Visibility.Visible;
      }


      the 'PreviewImage' is a reference to a capture element in the UI:



      <CaptureElement Name="PreviewImage" Stretch="Uniform" HorizontalAlignment="Center" Height="649" Margin="0,84,0,0" VerticalAlignment="Top" Width="1147"/>


      BUT every time I try and set the source to the MediaCapture element I get a null reference exception. I have checked in the debugger that this object is not null and has the ID of a connected device, further to this when I run



      await capture.StartPreviewAsync(); 


      the green light below the camera lights up on the camera. If i try to re-initialize the MediaCapture object returned by GetPreviewElement it throws an exception that the device is already initialized too.



      Am i approaching setting up multiple devices correctly? is passing a copy of the initialized MediaCapture a valid way of setting the source of the CaptureElement?



      Many thanks!










      share|improve this question













      I'm trying to get a simple test working to learn how to setup and use multiple USB video devices at once in a Universal Windows project (later to be used on a raspberry Pi running windows 10 IoT core). I want the UI to display a preview of the selected camera. I have successfully followed the preview example on the MediaCapture site HERE



      I have a 'TakePicture' class that will later be used for other things, but is currently used to find and initialize the video devices attached to the system as follows



      public static class TakePicture
      {
      private static List<CaptureElement> _captureElements;
      private static List<MediaCapture> _mediaCaptures;
      public static DeviceInformationCollection devices;
      private static int currentDevice = 0;

      public static async Task InitializeDevice()
      {
      if (currentDevice >= _captureElements.Count)
      {
      _mediaCaptures.Add(new MediaCapture());
      var captureElement = new CaptureElement{Stretch = Stretch.UniformToFill};
      _captureElements.Add(captureElement);
      await _mediaCaptures[currentDevice].InitializeAsync(
      new MediaCaptureInitializationSettings
      {
      VideoDeviceId = devices[currentDevice].Id,
      StreamingCaptureMode = StreamingCaptureMode.Video

      });
      _captureElements[currentDevice].Source = _mediaCaptures[currentDevice];
      }
      }

      public static void IncrimentDevice()
      {

      currentDevice = (currentDevice + 1) % devices.Count;
      }



      public static async Task<MediaCapture> getPreviewElement()
      {
      CaptureElement element = new CaptureElement();
      await FindDevices();
      await InitializeDevice();
      return _mediaCaptures[currentDevice];

      }


      public static async Task FindDevices()
      {
      if (devices == null)
      {
      devices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);
      _captureElements = new List<CaptureElement>();
      _mediaCaptures = new List<MediaCapture>();
      }
      }


      }


      I want to return a MedaCapture object related to a camera and use that to set the source of the CaptureElement in the GUI as follows:



      private async Task StartPreviewAsync()
      {
      MediaCapture capture = await TakePicture.getPreviewElement();
      await capture.StartPreviewAsync();
      PreviewImage.Source = capture;
      PreviewImage.Visibility = Visibility.Visible;
      }


      the 'PreviewImage' is a reference to a capture element in the UI:



      <CaptureElement Name="PreviewImage" Stretch="Uniform" HorizontalAlignment="Center" Height="649" Margin="0,84,0,0" VerticalAlignment="Top" Width="1147"/>


      BUT every time I try and set the source to the MediaCapture element I get a null reference exception. I have checked in the debugger that this object is not null and has the ID of a connected device, further to this when I run



      await capture.StartPreviewAsync(); 


      the green light below the camera lights up on the camera. If i try to re-initialize the MediaCapture object returned by GetPreviewElement it throws an exception that the device is already initialized too.



      Am i approaching setting up multiple devices correctly? is passing a copy of the initialized MediaCapture a valid way of setting the source of the CaptureElement?



      Many thanks!







      camera webcam mediacapture






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 7 at 18:22









      Ben Allen

      12




      12





























          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53195522%2fsetting-captureelement-source-null-exception%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53195522%2fsetting-captureelement-source-null-exception%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          Xamarin.form Move up view when keyboard appear

          Post-Redirect-Get with Spring WebFlux and Thymeleaf

          Anylogic : not able to use stopDelay()