How to use camera flash in an app for Windows Phone 8

Applies to: Windows Phone 8 and Windows Phone Silverlight 8.1 | Windows Phone OS 7.1

Starting with Windows Phone OS 7.1, you can programmatically access the phone’s camera with the Microsoft.Devices.PhotoCamera class. This topic describes how to programmatically set the camera flash mode. It is a continuation of How to create a base camera app for Windows Phone 8 and assumes you have already created the base camera project in that topic.

TipTip:

Starting with Windows Phone 8, apps can use the PhotoCaptureDevice class for advanced capture scenarios. PhotoCaptureDevice allows apps to control photo properties such as ISO, exposure compensation, and manual focus position (when available on the phone). This topic doesn’t demonstrate PhotoCaptureDevice; for more info about using this class, see Advanced photo capture for Windows Phone 8.

This topic corresponds to the Basic Camera Sample.

Windows Phone supports the following flash modes in the FlashMode enumeration.

Flash mode

Description

Off

The camera flash is disabled.

On

The camera flash is enabled.

RedEyeReduction

Red-eye reduction is enabled.

Auto

Camera flash is used in automatic mode.

Important noteImportant Note:

Not all flash modes are supported on all devices; it is important to call the IsFlashModeSupported(FlashMode) method to confirm that a mode is available before using it.

This section provides the code necessary to implement flash mode configuration in your app.

To implement camera flash

  1. Open your base camera project created in How to create a base camera app for Windows Phone 8.

  2. In the main page XAML file, MainPage.xaml, add the following code in the StackPanel element, below the Button element named ShutterButton. This code is the button for the camera flash.

    <Button Name="FlashButton" Content="Fl:TBD" Click="changeFlash_Clicked" 
      FontSize="26" FontWeight="ExtraBold" Height="75"/>
    
    
  3. Open the code-behind file for the main page, MainPage.xaml.cs, and add the following variable declarations above the MainPage class constructor:

        // Holds current flash mode.
        private string currentFlashMode;
    
    
  4. In MainPage.xaml.cs, add the following code to the OnNavigatedTo method, just below the Disable UI comment.

    FlashButton.IsEnabled = false;
    

    This code disables the flash button. It is used when a camera is not available on the device.

  5. In MainPage.xaml.cs, add the following code to the cam_Initialized method, just below the txtDebug statement:

        // Set flash button text.
        FlashButton.Content = "Fl:" + cam.FlashMode.ToString();
    
    

    This code displays the current flash mode on the FlashButton button.

  6. In MainPage.xaml.cs, add the following code to the MainPage class. This code implements the event handler for changeFlash_Clicked by switching to a different flash mode each time the button is pressed.

        // Activate a flash mode.
        // Cycle through flash mode options when the flash button is pressed.
        private void changeFlash_Clicked(object sender, RoutedEventArgs e)
        {
    
            switch (cam.FlashMode)
            {
                case FlashMode.Off:
                    if (cam.IsFlashModeSupported(FlashMode.On))
                    {
                        // Specify that flash should be used.
                        cam.FlashMode = FlashMode.On;
                        FlashButton.Content = "Fl:On";
                        currentFlashMode = "Flash mode: On";
                    }
                    break;
                case FlashMode.On:
                    if (cam.IsFlashModeSupported(FlashMode.RedEyeReduction))
                    {
                        // Specify that the red-eye reduction flash should be used.
                        cam.FlashMode = FlashMode.RedEyeReduction;
                        FlashButton.Content = "Fl:RER";
                        currentFlashMode = "Flash mode: RedEyeReduction";
                    }
                    else if (cam.IsFlashModeSupported(FlashMode.Auto))
                    {
                        // If red-eye reduction is not supported, specify automatic mode.
                        cam.FlashMode = FlashMode.Auto;
                        FlashButton.Content = "Fl:Auto";
                        currentFlashMode = "Flash mode: Auto";
                    }
                    else 
                    {
                        // If automatic is not supported, specify that no flash should be used.
                        cam.FlashMode = FlashMode.Off;
                        FlashButton.Content = "Fl:Off";
                        currentFlashMode = "Flash mode: Off";
                    }
                    break;
                case FlashMode.RedEyeReduction:
                    if (cam.IsFlashModeSupported(FlashMode.Auto))
                    {
                        // Specify that the flash should be used in the automatic mode.
                        cam.FlashMode = FlashMode.Auto;
                        FlashButton.Content = "Fl:Auto";
                        currentFlashMode = "Flash mode: Auto";
                    }
                    else
                    {
                        // If automatic is not supported, specify that no flash should be used.
                        cam.FlashMode = FlashMode.Off;
                        FlashButton.Content = "Fl:Off";
                        currentFlashMode = "Flash mode: Off";
                    }
                    break;
                case FlashMode.Auto:
                    if (cam.IsFlashModeSupported(FlashMode.Off))
                    {
                        // Specify that no flash should be used.
                        cam.FlashMode = FlashMode.Off;
                        FlashButton.Content = "Fl:Off";
                        currentFlashMode = "Flash mode: Off";
                    }
                    break;
            }
    
            // Display current flash mode.
            this.Dispatcher.BeginInvoke(delegate()
            {
                txtDebug.Text = currentFlashMode;
            });
        }
    
    
  7. On a device, run the app by selecting the Debug | Start Debugging menu command.

Show:
© 2014 Microsoft