Reputation: 1959
I'm trying to create a basic camera application (for my first application targeted towards WP). And I want to, of course, preview the camera data to the screen before taking a shot.
But the only samples I see online from MSDN etc are too old (many objects have been removed, ie the libraries are being updated frequently making the articles obsolete)
I'm having a problem just getting started with the preview-stream. It would be greatly appreciated if someone with knowledge could give me some help in this matter.
Thank you.
Upvotes: 1
Views: 999
Reputation: 721
I suggest using the CaptureElement control
On your XAML add this:
<CaptureElement x:Name="Capture"
Stretch="UniformToFill"
FlowDirection="LeftToRight" />
I don't know if you want to use front or back webcam so I'll show code for both.
In your codeBehind (or your ViewModel if your using MVVM) add this code:
// First need to find all webcams
DeviceInformationCollection webcamList = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture)
// Then I do a query to find the front webcam
DeviceInformation frontWebcam = (from webcam in webcamList
where webcam.EnclosureLocation != null
&& webcam.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Front
select webcam).FirstOrDefault();
// Same for the back webcam
DeviceInformation backWebcam = (from webcam in webcamList
where webcam.EnclosureLocation != null
&& webcam.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Back
select webcam).FirstOrDefault();
// Then you need to initialize your MediaCapture
var newCapture = new MediaCapture();
await newCapture.InitializeAsync(new MediaCaptureInitializationSettings
{
// Choose the webcam you want (backWebcam or frontWebcam)
VideoDeviceId = backWebcam.Id,
AudioDeviceId = "",
StreamingCaptureMode = StreamingCaptureMode.Video,
PhotoCaptureSource = PhotoCaptureSource.VideoPreview
});
// Set the source of the CaptureElement to your MediaCapture
Capture.Source = newCapture;
// Start the preview
await newCapture.StartPreviewAsync();
This will show the stream of the chosen webcam in your CaptureElement control, and works on both Windows Phone 8.1 and Windows 8.1
Upvotes: 1