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: 

Basic camera app sample features

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:

  1. Manage the MediaCapture object throughout the lifecycle of the app and through navigation events.
  2. Acquire a camera located on a specific side of the device. In this case, the sample attempts to get the rear camera.
  3. Start and stop the preview to a UI element, including mirroring for front-facing cameras.
  4. Take a picture to a file, and disable the video capture button if the app is running on a device that doesn't support concurrent capturing of photos and video.
  5. Record a video to a file, and disable the photo capture button if the app is running on a device that doesn't support concurrent capturing of photos and video.
  6. Handle rotation events for both, the device moving in space and the page orientation changing on the screen. Also apply any necessary corrections to the preview stream rotation and to captured photos and videos.
  7. Handle MediaCapture RecordLimitationExceeded and Failed events to be notified that video recording needs to be stopped due to a video being too long, or clean up the MediaCapture instance when an error occurs.

 

About

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:

  1. Optional Camera Flash
  2. Embedded GPS Coordinates into photos

This blog covers the first issue. The next blog will cover the latter.

 

MainPage Code.

This adds a set of Radio Button to choose the type of Flash:

  • Flash
  • Automatic
  • No 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;
                }
            }
        }

In MainPage.xmal.cs

 

The Camera page can then get the value of  _FlashState.

 

CameraPage Code

 

         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.cs
The code above has had some checks and balances removed  to simplfy it.

In Summary:

  • You get a handle on the Camera device
  • From that you get its MediaCapture device which you initialize.
  • You then can get a handle on the FlashControl via the VideoDeviceController.
  • You can then set the Flash mode.