Introduction



Home Screen :

Name of Experiment : Use of dedicated button for Camera Operation.

Exp No : IP5

Background : Student should have basic knowledge of C#..

Summary : This experiment is based on camera operation using dedicated button. We can see that how this single dedicated button can be used for firing three different events.

Learning Objective : Student should now understand the camera operation using the dedicated button.

Target

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

Procedure

Source Code


MainPage.xaml
<phone:PhoneApplicationPage
x:Class="WP11.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Landscape" Orientation="LandscapeLeft"
shell:SystemTray.IsVisible="False" d:DesignHeight="480" d:DesignWidth="800">

<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="640"/>
<ColumnDefinition Width="150"/>
</Grid.ColumnDefinitions>
<Canvas x:Name="viewfinderCanvas" >
<Canvas.Background>
<VideoBrush x:Name="videoBrush"/>
</Canvas.Background>
<TextBlock Canvas.Left="0" Canvas.Top="414" Height="60" Name="txtDebug" FontSize="22" FontWeight="ExtraBold" Width="513" />

</Canvas>
<!--TitlePanel contains the name of the application and page title-->

<!--ContentPanel - place additional content here-->

</Grid>

</phone:PhoneApplicationPage>

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

namespace WP11
{
public partial class MainPage : PhoneApplicationPage
{
PhotoCamera mycamera;
// Constructor
public MainPage()

{
InitializeComponent();
}
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
//base.OnNavigatedTo(e); if (PhotoCamera.IsCameraTypeSupported(CameraType.Primary) == true)
{
mycamera = new Microsoft.Devices.PhotoCamera(CameraType.Primary);
CameraButtons.ShutterKeyHalfPressed+=new System.EventHandler(CameraButtons_ShutterKeyHalfPressed);
CameraButtons.ShutterKeyPressed+=new System.EventHandler(CameraButtons_ShutterKeyPressed);
CameraButtons.ShutterKeyReleased+=new System.EventHandler(CameraButtons_ShutterKeyReleased);
mycamera.Initialized += new System.EventHandler(mycamera_Initialized);
videoBrush.SetSource(mycamera);
}
else {
this.Dispatcher.BeginInvoke(delegate()
{
this.txtDebug.Text = "camera is not supported in the device"; });
}
}
protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
{ //base.OnNavigatedFrom(e);
if (mycamera != null) {
mycamera.Dispose();
mycamera.Initialized -= mycamera_Initialized;
CameraButtons.ShutterKeyHalfPressed -= CameraButtons_ShutterKeyHalfPressed;
CameraButtons.ShutterKeyPressed -= CameraButtons_ShutterKeyPressed;
CameraButtons.ShutterKeyReleased -= CameraButtons_ShutterKeyReleased;
}
}
public void mycamera_Initialized(object sender, CameraOperationCompletedEventArgs e) {
if (mycamera != null) {
this.Dispatcher.BeginInvoke(delegate()
{
this.txtDebug.Text = "camera is Initialised";
});
}
}
public void CameraButtons_ShutterKeyHalfPressed(object sender, EventArgs e) {
if (mycamera != null) {
try
{
this.Dispatcher.BeginInvoke(delegate()
{
this.txtDebug.Text = "Half Key Pressed:Focus";
});
mycamera.Focus();
}
catch (Exception focuserror) {
this.Dispatcher.BeginInvoke(delegate() { this.txtDebug.Text = focuserror.Message; });
}
}
}
public void CameraButtons_ShutterKeyPressed(object sender, EventArgs e) {
if (mycamera != null) {
mycamera.CaptureImage();
}
}
public void CameraButtons_ShutterKeyReleased(object sender,EventArgs e) { if (mycamera != null) {
mycamera.CancelFocus();
}
}
}
}

Snapshots




Observation

Observation : It is observed that using the single dedicated button, we can call the three basic camera operation events.

Video Link

For Windows Phone

Download

Right Click Here And Select Save Target For Entire Download :

Use of dedicated button for Camera Operation