Introduction



Home Screen :

Name of Experiment : Camera Operation(Flash Setting)

Exp No : IP4

Background : Student should have basic knowledge of C#.

Summary : This experiment is again based on the camera operation, in this experiment user can set the Flash of the camera as well as he/she can set for Auto Flash.

Learning Objective : To learn the camera feature Flash (ON/OFF/AUTO) during the capturing process.

Target

Target Platform :This application is tested on Windows Phone Emulator and Windows Phone(Lumia 800).

Procedure

Source Code


MainPage.xaml

<!--LayoutRoot is the root grid where all page content is placed-->

<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="640" />
<ColumnDefinition Width="160" />
</Grid.ColumnDefinitions>

<Canvas x:Name="viewfinderCanvas" Width="640" Height="480" HorizontalAlignment="Left" >

<!--Camera viewfinder -->
<Canvas.Background>
<VideoBrush x:Name="viewfinderBrush" />
</Canvas.Background>
</Canvas>

<!--Button StackPanel to the right of viewfinder>
-->
<StackPanel Grid.Column="1" >
<Button x:Name="ShutterButton" Content="CAPTURE" Click="ShutterButton_Click" FontSize="26" FontWeight="ExtraBold" Height="75" />
<Button x:Name="FlashButton" Content="FLASH" Click="FlashButton_Click" FontSize="26" FontWeight="ExtraBold" Height="75" />
</StackPanel>

<!--Used for debugging >
-->
<TextBlock Height="40" HorizontalAlignment="Left" Margin="8,428,0,0" Name="txtDebug" VerticalAlignment="Top" Width="626" FontSize="24" FontWeight="ExtraBold" />
</Grid>

MainPage.xaml.cs
using System.Windows;
using Microsoft.Phone.Controls;
using Microsoft.Devices;
using Microsoft.Xna.Framework.Media;
using System;

namespace WP7
{
public partial class MainPage : PhoneApplicationPage
{
PhotoCamera cam;
MediaLibrary library = new MediaLibrary();
int savedCounter = 0;
private string currentFlashMode;
// Constructor
public MainPage()
{
InitializeComponent();
}

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
//base.OnNavigatedTo(e);
if (PhotoCamera.IsCameraTypeSupported(CameraType.Primary) == true)
{
cam = new PhotoCamera();
viewfinderBrush.SetSource(cam);
cam.Initialized += new System.EventHandler(cam_Initialized);
cam.CaptureCompleted += new System.EventHandler(cam_CaptureCompleted);
cam.CaptureImageAvailable += new System.EventHandler(cam_CaptureImageAvailable);
}
else
{
Deployment.Current.Dispatcher.BeginInvoke(delegate()
{
txtDebug.Text = "Camera is not available";
});
// ResButton.IsEnabled = false;
ShutterButton.IsEnabled = false;
FlashButton.IsEnabled = false;
}
}

protected void OnNavigatedFrom(object sender, NavigatingEventArgs e)
{
if (cam != null)
{
cam.Dispose();
cam.Initialized -= cam_Initialized;
cam.CaptureCompleted -= cam_CaptureCompleted;
cam.CaptureImageAvailable -= cam_CaptureImageAvailable;
}
}
public void cam_Initialized(object sender, Microsoft.Devices.CameraOperationCompletedEventArgs e)
{
try{
if (e.Succeeded)
{
Deployment.Current.Dispatcher.BeginInvoke(delegate()
{
txtDebug.Text = "Camera is Initialised";
FlashButton.Content = "Fl:" + cam.FlashMode.ToString();
});
}
}
catch(Exception ex){
txtDebug.Text=ex.Message;
}

}
public void cam_CaptureCompleted(object sender, CameraOperationCompletedEventArgs e)
{
savedCounter++;
Deployment.Current.Dispatcher.BeginInvoke(delegate()
{
txtDebug.Text = "Picture is saved for you";
});
}
public void cam_CaptureImageAvailable(object sender, Microsoft.Devices.ContentReadyEventArgs e)
{
string filename = savedCounter + ".jpg";
library.SavePictureToCameraRoll(filename, e.ImageStream);
}

private void ShutterButton_Click(object sender, System.Windows.RoutedEventArgs e)
{
if (cam != null)
{
cam.CaptureImage();
//savedCounter++;
}
else
{
Deployment.Current.Dispatcher.BeginInvoke(delegate()
{
txtDebug.Text = "Camera device is not available";
});
}

}
private void FlashButton_Click(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))
{
cam.FlashMode = FlashMode.Auto;
FlashButton.Content = "Fl:Auto";
currentFlashMode = "Flash mode: Auto";
}
else
{ cam.FlashMode = FlashMode.Off;
FlashButton.Content = "Fl:Off";
currentFlashMode = "Flash mode: Off";
}
break;
case FlashMode.RedEyeReduction:
if (cam.IsFlashModeSupported(FlashMode.Auto))
{
cam.FlashMode = FlashMode.Auto;
FlashButton.Content = "Fl:Auto";
currentFlashMode = "Flash mode: Auto";
}
else
{ am.FlashMode = FlashMode.Off;
FlashButton.Content = "Fl:Off";
currentFlashMode = "Flash mode: Off";
}
break;
case FlashMode.Auto:
if (cam.IsFlashModeSupported(FlashMode.Off))
{
cam.FlashMode = FlashMode.Off;
FlashButton.Content = "Fl:Off";
currentFlashMode = "Flash mode: Off";
}
break;
}

this.Dispatcher.BeginInvoke(delegate()
{
txtDebug.Text = currentFlashMode;
});
}
}
}

Snapshots







Observation

Observation : It is observed that Flash Mode is very important in capturing the image, where the source of light is not enough for a good quality of Image.

Video Link

For Windows Phone

Download

Right Click Here And Select Save Target For Entire Download :

Camera Operation(Flash Setting)