Introduction



Home Screen :

Name of Experiment : Camera Operation (Focus Setting)

Exp No : IP3

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 Focus of the camera lens at his desired location on the mobile screen as well as he/she can set for Auto Focusing.

Learning Objective : Student now able to set the focus of the camera lens in three modes such as auto focusing, focusing at centre and focusing on the desired location on the mobile screen by simply tapping on the mobile screen.

Target

Target Platform :The 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>
<TextBlock x:Name="focusBracket" FontSize="40" Text="[ ]" HorizontalAlignment="Center" VerticalAlignment="Center" FontWeight="ExtraBold" Visibility="Collapsed" Canvas.Left="296" Canvas.Top="192" />
</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="FocusButton" Content="FOCUS" Click="FocusButton_Click" FontSize="26" FontWeight="ExtraBold" Height="75" /> <Button x:Name="AFButton" Content="AUTOFS" Click="AFButton_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;
using System.Windows.Input;
using System.Windows.Controls;

namespace Cam_Test
{
public partial class MainPage : PhoneApplicationPage
{ PhotoCamera cam = null;
int savedCounter = 0;
bool _isfocus = true;
MediaLibrary library = new MediaLibrary();
// Constructor
public MainPage()
{
InitializeComponent();
}
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
//base.OnNavigatedTo(e);
cam = new PhotoCamera();
viewfinderBrush.SetSource(cam);
cam.Initialized += new EventHandler(cam_Initialized);
cam.CaptureImageAvailable+=new EventHandler(cam_CaptureImageAvailable);
cam.CaptureCompleted+=new EventHandler(cam_CaptureCompleted);
viewfinderCanvas.Tap+=new EventHandler(focus_Tapped);
}
protected void OnNavigatedFrom(System.Windows.Navigation.NavigatingCancelEventArgs e)
{
//base.OnNavigatedFrom(e);
if (cam != null) { cam.Dispose();
cam.Initialized -= cam_Initialized;
cam.CaptureCompleted -= cam_CaptureCompleted;
cam.CaptureImageAvailable -= cam_CaptureImageAvailable;
cam.AutoFocusCompleted -= cam_AutoFocusCompleted;
}
}

public void cam_Initialized(object sender, Microsoft.Devices.CameraOperationCompletedEventArgs e)
{

if (e.Succeeded) {
Deployment.Current.Dispatcher.BeginInvoke(delegate()
{
txtDebug.Text = "Camera is Initialised";
});
}
}
public void cam_CaptureCompleted(object sender,CameraOperationCompletedEventArgs e) {
savedCounter++;
Deployment.Current.Dispatcher.BeginInvoke(delegate()
{
txtDebug.Text = "Picture is saved for you";
});
}
public void cam_AutoFocusCompleted(object sender,CameraOperationCompletedEventArgs e) { _isfocus = true;
Deployment.Current.Dispatcher.BeginInvoke(delegate()

{
txtDebug.Text = "Auto Focusing Completed";
focusBracket.Visibility = Visibility.Collapsed;
});
}
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, RoutedEventArgs e)
{
if (cam != null&& _isfocus)
{
cam.CaptureImage();
}
else
{
Deployment.Current.Dispatcher.BeginInvoke(delegate()
{
txtDebug.Text = "Camera device is not available";
});
}
}

private void FocusButton_Click(object sender, RoutedEventArgs e)
{
if (cam.IsFocusAtPointSupported) {
focusBracket.Visibility = Visibility.Visible;
cam.FocusAtPoint(0.5,0.5);

}
Deployment.Current.Dispatcher.BeginInvoke(delegate()
{
txtDebug.Text = "Focussing at center";
});
}

private void AFButton_Click(object sender, RoutedEventArgs e)
{
cam.AutoFocusCompleted += new EventHandler(cam_AutoFocusCompleted);
if (cam.IsFocusSupported) {
_isfocus = false;
focusBracket.Visibility = Visibility.Visible;
cam.Focus();

}
}
public void focus_Tapped(object sender, GestureEventArgs e)
{
if (cam != null)
{
if (cam.IsFocusAtPointSupported == true)
{
try
{ // Determine the location of the tap.
Point tapLocation = e.GetPosition(viewfinderCanvas);

// Position the focus brackets with the estimated offsets.
focusBracket.SetValue(Canvas.LeftProperty, tapLocation.X - 30);
focusBracket.SetValue(Canvas.TopProperty, tapLocation.Y - 28);

// Determine the focus point.
double focusXPercentage = tapLocation.X / viewfinderCanvas.Width;
double focusYPercentage = tapLocation.Y / viewfinderCanvas.Height;

// Show the focus brackets and focus at point.
focusBracket.Visibility = Visibility.Visible;
cam.FocusAtPoint(focusXPercentage, focusYPercentage);

// Write a message to the UI.
this.Dispatcher.BeginInvoke(delegate()
{
txtDebug.Text = String.Format("Camera focusing at point: {0:N2} , {1:N2}", focusXPercentage, focusYPercentage);
});
}
catch (Exception focusError)
{
// Cannot focus when a capture is in progress.
this.Dispatcher.BeginInvoke(delegate()
{
// Write a message to the UI.
txtDebug.Text = focusError.Message;
// Hide focus brackets.
focusBracket.Visibility = Visibility.Collapsed;
});
}
}
else {
// Write a message to the UI.
this.Dispatcher.BeginInvoke(delegate()
{ txtDebug.Text = "Camera does not support FocusAtPoint().";});
}
}
}
}
}

Snapshots








Observation

Observation : It is observed that focusing is also an important feature of the camera device and must be enabled in order to capture the desired object.

Video Link

For Windows Phone

Download

Right Click Here And Select Save Target For Entire Download :

Camera Operation(Capture Event)