Introduction



Home Screen :

Name of Experiment : Camera Operation(Capture Event)

Exp No : IP2

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

Summary : This experiment is completely independent from previous experiment IP1, and it will help you to understand the camera operation and development of dedicated camera application.

Learning Objective : Student will learn the basic steps in order to access the camera hardware of the device.

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>
<!--TitlePanel contains the name of the application and page title-->

<Canvas x:Name="viewfinderCanvas" Width="640" Height="480" HorizontalAlignment="Left" >
<!--Camera viewfinder -->
<Canvas.Background>
<!--VideoBrush for Image drawing-->
<VideoBrush x:Name="viewfinderBrush" />
</Canvas.Background>
</Canvas>

<!--Button StackPanel to the right of viewfinder>
-->
<StackPanel Grid.Column="1" >
<!—Name= “ShutterButton” Content= “SH”-->
<Button x:Name="ShutterButton" Content="SH" Click="ShutterButton_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 System;
using Microsoft.Xna.Framework.Media;

namespace test_cap
{ public partial class MainPage : PhoneApplicationPage
{ PhotoCamera cam = null;
int savedPhoto=0;
MediaLibrary library = new MediaLibrary();

// Constructor
public MainPage()

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

}

}
public void cam_Initialized(object sender , CameraOperationCompletedEventArgs e) {
if (e.Succeeded)
{
this.Dispatcher.BeginInvoke(delegate()
{
txtDebug.Text = "camera is initialized";
});
}

}
public void cam_CaptureCompleted(object sender, CameraOperationCompletedEventArgs e) {
savedPhoto++;
}
public void cam_CaptureImageAvailable(object sender, ContentReadyEventArgs e) {
string filename = savedPhoto + ".jpg";
library.SavePictureToCameraRoll(filename, e.ImageStream);

Deployment.Current.Dispatcher.BeginInvoke(delegate()
{
this.txtDebug.Text = "Picture has been saved for you";
});
}

private void ShutterButton_Click(object sender, RoutedEventArgs e)
{
try{
if (cam != null) {
cam.CaptureImage();
} } catch(Exception ex){
this.Dispatcher.BeginInvoke(delegate()
{
txtDebug.Text = ex.Message;
});
}
}
}
}

Snapshots








Observation

Observation : It is observed that for accessing the basic functionality of the camera device these above steps must be performed.

Video Link

For Windows Phone

Download

Right Click Here And Select Save Target For Entire Download :

Camera Operation(Capture Event)