Its not too hard to add flash capability to the Universal Windows SDK Camera sample. Here’s how.
The Windows 10 SDK Samples are on GitHub. The zipped version is here: Download Zip
The sample app I am working with here is the Basic camera app sample:
This sample applies an end-to-end approach to demonstrate how to write a camera application using the Windows.Media.Capture API in conjunction with orientation sensors to cover the functions that most camera apps will require.
Specifically, the sample will covers how to:
I took the sample code (C#) and morphed it into a separate XAML page for an app that I was developing. The CameraPage is called from another XAML page. I then wanted to add two things:
This blog covers the first issue. The next blog will cover the latter.
This adds a set of Radio Button to choose the type of Flash:
<Grid> ..... ..... <RadioButton Grid.Row="2" Grid.Column="0" x:Name="EnableFlash" GroupName="Flash" Content="Flash" Checked="EnableFlash_Checked" /> <RadioButton Grid.Row="2" Grid.Column="1" x:Name="AutoFlash" GroupName="Flash" Content="Auto" Checked="EnableFlash_Checked" /> <RadioButton Grid.Row="2" Grid.Column="2" x:Name="NoFlash" GroupName="Flash" Content="No Flash" IsChecked="True" Checked="EnableFlash_Checked"/> ..... ..... </Grid>
In MainPage.xaml
public enum FlashState { Enable, Auto, None } public static FlashState _FlashState { get; set; } = FlashState.None; private void EnableFlash_Checked(object sender, RoutedEventArgs e) { RadioButton rb = (RadioButton)sender; if (rb != null) { switch (rb.Name) { case "EnableFlash": _FlashState = FlashState.Enable; break; case "AutoFlash": _FlashState = FlashState.Auto; break; case "NoFlash": _FlashState = FlashState.None; break; } } }
The Camera page can then get the value of _FlashState.
InitializeCameraAsync() { var cameraDevice = await FindCameraDeviceByPanelAsync(Windows.Devices.Enumeration.Panel.Back) _mediaCapture = new MediaCapture(); var settings = new MediaCaptureInitializationSettings { VideoDeviceId = cameraDevice.Id }; // Initialize MediaCapture try { await _mediaCapture.InitializeAsync(settings); _isInitialized = true; } catch (Exception ex) { return; } if (_isInitialized) { if (_mediaCapture.VideoDeviceController.FlashControl.Supported) { switch (MainPage._FlashState) { case MainPage.FlashState.Enable: _mediaCapture.VideoDeviceController.FlashControl.Enabled = true; _mediaCapture.VideoDeviceController.FlashControl.Auto = false; break; case MainPage.FlashState.Auto: _mediaCapture.VideoDeviceController.FlashControl.Enabled = true; _mediaCapture.VideoDeviceController.FlashControl.Auto = true; break; case MainPage.FlashState.None: _mediaCapture.VideoDeviceController.FlashControl.Enabled = false; break; } } ..... ..... }
In CameraPage.xaml.csThe code above has had some checks and balances removed to simplfy it.