This sample demonstrates how to build a robust and feature-rich Universal Windows Platform (UWP) application that leverages the device's camera capabilities. It covers common scenarios like capturing photos, recording videos, accessing camera settings, and applying basic image effects.
The UWP Camera App sample provides a comprehensive example of integrating camera functionality into your Windows applications. You will learn how to:
The core of camera interaction in UWP applications relies on the Windows.Media.Capture
namespace, specifically the MediaCapture
class.
async Task InitializeCameraAsync()
{
var cameraDevice = await FindCameraDeviceByPanelAsync(Windows.Devices.Enumeration.Panel.Back);
if (cameraDevice == null)
{
// Handle case where no camera is found
return;
}
mediaCapture = new MediaCapture();
await mediaCapture.InitializeAsync(new MediaCaptureInitializationSettings
{
AudioDeviceId = "", // No audio needed for this photo sample
VideoDeviceId = cameraDevice.Id,
StreamingCaptureMode = StreamingCaptureMode.Video
});
// Set the preview source to a CaptureElement
var previewElement = new CaptureElement();
previewElement.Source = mediaCapture;
await mediaCapture.StartPreviewAsync();
}
Capturing a photo involves using the MediaCapture.CapturePhotoToStorageFileAsync
method.
async Task CapturePhotoAsync()
{
if (mediaCapture.CameraStreamState == Windows.Media.Devices.CameraStreamState.Streaming)
{
var photoFile = await KnownFolders.PicturesLibrary.CreateFileAsync("SamplePhoto.jpg", CreationCollisionOption.GenerateUniqueName);
await mediaCapture.CapturePhotoToStorageFileAsync(Windows.Media.Imaging.ImageFormat.Jpeg, photoFile);
// Notify user or display photo
}
}
This sample requires the following:
For more detailed information on UWP camera APIs, refer to the official Microsoft documentation.