Programmatically turn On-off camera flash app in windows phone 8

Programmatically turn On-off camera flash app in windows phone 8

This article describes you how to turn ON/OFF Camera flashlight pro grammatically. Micosoft.Devices.Camera class gives the feasibility to use this functionality.

 Applies to : Windows phone 8, Windows phone Silverlight 8.1 

Windows phone supports the below FlashMode Enumerations.

 

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.

 

To create a camera app, the camera capability must be declared in the app manifest file. Without it, the app won’t function. Open WMAppManifest.xml and confirm that the following capabilities element is present.

Go to Solution à PropertiesàWMAppManifest.xml


Example:

Mainpage.XAML

In the main page XAML file, MainPage.xaml, add the following Button element named FlashButton. This code is the button for the camera flash.

<Button Name="FlashButton" Content="Flashlight" Click="FlashButton_Clicked"   FontSize="26" FontWeight="ExtraBold" Height="75"/>

 

Mainpage.XAML.cs

Declare below global properties in the Mainpage.xaml.cs

private PhotoCamera _photoCamera;

private VideoBrush _videoBrush;

 

private void FlashButton_Clicked (object sender, RoutedEventArgs e)

  {

     if (PhotoCamera.IsCameraTypeSupported(CameraType.Primary))

        {

           // Use standard camera on back of device.

            _photoCamera = new PhotoCamera(CameraType.Primary);

           // Event is fired when the PhotoCamera object has been initialized.

            _photoCamera.Initialized += _photoCamera_Initialized;

            _photoCamera.AutoFocusCompleted += _photoCamera_AutoFocusCompleted;

            _videoBrush = new VideoBrush();

            _videoBrush.SetSource(_photoCamera);

         }

 

            switch (_photoCamera.FlashMode)

        {

            case FlashMode.Off:

                if (_photoCamera.IsFlashModeSupported(FlashMode.On))

                {

                    // Specify that flash should be used.

                    _photoCamera.FlashMode = FlashMode.On;

                    FlashButton.Content = "Flash On";

                   

                }

                break;

            case FlashMode.On:

                if (_photoCamera.IsFlashModeSupported(FlashMode.RedEyeReduction))

                {

                    // Specify that the red-eye reduction flash should be used.

                    _photoCamera.FlashMode = FlashMode.RedEyeReduction;

                    FlashButton.Content = "Flash Red Eye";

                   

                }

                else if (_photoCamera.IsFlashModeSupported(FlashMode.Auto))

                {

                    // If red-eye reduction is not supported, specify automatic mode.

                    _photoCamera.FlashMode = FlashMode.Auto;

                    FlashButton.Content = "Flash Auto";

                   

                }

                else

                {

                    // If automatic is not supported, specify that no flash should be used.

                    _photoCamera.FlashMode = FlashMode.Off;

                    FlashButton.Content = "Flash Off";

                  

                }

                break;

 

        }

     }

 

 

void _photoCamera_Initialized(object sender, CameraOperationCompletedEventArgs e)

        {

             FlashButton.Content = "Flash" + cam.FlashMode.ToString()

              _photoCamera.Focus();

           

        }

 

Summary

This article explains about how to access Camera hardware of phone.


Happy Coding :)

comments powered by Disqus

Related Articles