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!
camera webcam mediacapture
add a comment |
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!
camera webcam mediacapture
add a comment |
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!
camera webcam mediacapture
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
camera webcam mediacapture
asked Nov 7 at 18:22
Ben Allen
12
12
add a comment |
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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