Partilhar via


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

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

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.

Tip

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 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.

Implementing camera flash

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;
    
        ' Holds current flash mode.
        Private currentFlashMode As String
    
  4. In MainPage.xaml.cs, add the following code to the OnNavigatedTo method, just below the Disable UI comment.

    FlashButton.IsEnabled = false;
    
    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();
    
        ' 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;
            });
        }
    
        ' Activate a flash mode.
        ' Cycle through flash mode options when the flash button is pressed.
        Private Sub changeFlash_Clicked(ByVal sender As Object, ByVal e As RoutedEventArgs)
    
            Select Case cam.FlashMode
                Case FlashMode.Off
                    If cam.IsFlashModeSupported(FlashMode.On) Then
                        ' Specify that flash should be used.
                        cam.FlashMode = FlashMode.On
                        FlashButton.Content = "Fl:On"
                        currentFlashMode = "Flash mode: On"
                    End If
                Case FlashMode.On
                    If cam.IsFlashModeSupported(FlashMode.RedEyeReduction) Then
                        ' Specify that the red-eye reduction flash should be used.
                        cam.FlashMode = FlashMode.RedEyeReduction
                        FlashButton.Content = "Fl:RER"
                        currentFlashMode = "Flash mode: RedEyeReduction"
    
                    ElseIf cam.IsFlashModeSupported(FlashMode.Auto) Then
                        ' 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"
    
                    End If
                Case FlashMode.RedEyeReduction
                    If cam.IsFlashModeSupported(FlashMode.Auto) Then
                        ' 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"
    
                    End If
                Case FlashMode.Auto
                    If cam.IsFlashModeSupported(FlashMode.Off) Then
                        ' Specify that no flash should be used.
                        cam.FlashMode = FlashMode.Off
                        FlashButton.Content = "Fl:Off"
                        currentFlashMode = "Flash mode: Off"
                    End If
            End Select
    
            ' Display current flash mode.
            Me.Dispatcher.BeginInvoke(Sub() txtDebug.Text = currentFlashMode)
    
        End Sub
    
  7. On a device, run the app by selecting the Debug | Start Debugging menu command.

See Also

Other Resources

Advanced photo capture for Windows Phone 8

Capturing video for Windows Phone 8

Lenses for Windows Phone 8

How to use the camera capture task for Windows Phone 8