In this article we are going to see how to programmatically control the Camera flash options from the Windows Phone application. Camera Flash is applicable to devices which has the flash hardware enabled, there are certain devices where there is no flash supported of which the application feature will not be supported. We can control that using the Capability that is available right on the application manifest file in the solution. In order to use this option in your application we need to first check with IsFlashModeSupported method which is available in Windows.Phone.Media.Capture.PhotoCaptureDevice class instance which will tell that the devices is supported of using this option which is mandatory before you write any other code on your requirement. [more]
Let us see the steps on how to achieve this task in our Windows Phone application development. Open Visual Studio 2012 IDE and create a new Windows Phone project with a valid project name as shown in the screen below.
Clicking on OK will create the project and the solution with the list of default files and folders that are required to run the application. It will take some time to create these files based on your system configuration, so once everything is ready we can see the Visual Studio IDE with the project as shown in the screen below.
Now let us add some controls basically to help us trigger the camera flash and capture some result. We will add a button control which on event click we will initiate the flash and show some user output on to a text box as shown in the code below.
XAML Code:
[code:C#]<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock x:Name="ApplicationTitle" Text="F5debug How to Series" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock x:Name="PageTitle" Text="Cam Flash" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>
<!–ContentPanel – place additional content here–>
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Button Content="Trigger Flash" Height="72" HorizontalAlignment="Left" Margin="9,6,0,0" Name="button1" VerticalAlignment="Top" Width="441" />
<TextBlock Height="277" HorizontalAlignment="Left" TextWrapping="Wrap" Margin="29,108,0,0" Name="textBlock1" Text="" VerticalAlignment="Top" Width="407" />
</Grid>
Now in the button click event let us add the below code which will initiate the Photocamera class and check if the camera flash is on or in the other state. Basically this Camera Flash has a number of modes like Off, On, RedEyeReduction, Auto which can be checked here and apply the respective requirement as shown in the code below.
C# Code:
[code:c#]using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using F5debugHowto58.Resources;
using Microsoft.Devices;
using System.IO;
using System.IO.IsolatedStorage;
using Microsoft.Xna.Framework.Media;
namespace F5debugHowto58
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
}
PhotoCamera cam;
private void button1_Click(object sender, RoutedEventArgs e)
{
if (cam.IsFlashModeSupported(FlashMode.On))
{
cam.FlashMode = FlashMode.On;
textBlock1.Text = "Flash Mode is ON";
}
else if (cam.IsFlashModeSupported(FlashMode.Off))
{
cam.FlashMode = FlashMode.On;
textBlock1.Text = "Flash Mode is OFF";
}
else if (cam.IsFlashModeSupported(FlashMode.Auto))
{
cam.FlashMode = FlashMode.RedEyeReduction;
textBlock1.Text = "Flash Mode is Auto";
}
else if (cam.IsFlashModeSupported(FlashMode.RedEyeReduction))
{
cam.FlashMode = FlashMode.Auto;
textBlock1.Text = "Flash Mode is RedEye";
}
}
}
}
Now we are done with our configuration, just run the application by pressing F5 directly from the keyboard or we can use the Build and execute the project option from the tool bar to run the application. Once the Build is successful we can see the Windows Phone emulator with the application and the expected outputs.