user60456
user60456

Reputation:

Turn on camera flash in WP7

Is is possible to turn on the camera's flash in WP7?

Upvotes: 2

Views: 4683

Answers (3)

Raghav Manikandan
Raghav Manikandan

Reputation: 679

1) 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"/>

2) 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;

3) In MainPage.xaml.cs, add the following code to the OnNavigatedTo method, just below the Disable UI comment.

FlashButton.IsEnabled = false;

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

5) 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;
    });
}

Upvotes: 0

Chris Ching
Chris Ching

Reputation: 1444

This is now possible in the Windows Phone OS 7.1 SDK.

Here's the link to the MSDN article: Access Camera API on WP7

PhotoCamera cam = new Microsoft.Devices.PhotoCamera(CameraType.Primary);
cam.FlashMode = FlashMode.On;

Upvotes: 13

Mick N
Mick N

Reputation: 14882

This can't be done programatically in the current SDK.

The user is in exclusive control of this feature on their phone.

Upvotes: 2

Related Questions