Floating FB popout byF5debug

Learn Windows Phone 7 Development in 31 Days – Day 8 – Working with Camera in WP7

 

Introduction:

In this article we are going to see how to use the Camera options in developing applications with Windows Phone 7. In order to access the camera features we are going to use the Launchers and Choosers which has the inbuilt API’s to perform each task based on the requirement. In order to manipulate the camera photos and storage with the device, we are going to use the CameraCaptureTask. This task is used to launch the default camera application to take some snap shot for any third party application which is trying to use the camera to save images locally or to an isolated storage.     [more]

Here in this article we will see how to take picture using the camera and save it locally. We need to have some understanding on the Launchers and Choosers task since we need to select the task based on our requirement. Let us use the CameraCaptureTask and complete the process step by step using Visual Studio for Windows Phone .

Steps:

First let us Open Visual Studio 2010 and create a new Silverlight for Windows Phone project with a valid project name as shown in the screen below.

clip_image002

Now to invoke the cameracapture task and save the image locally we can add 2 controls a button and a image viewer control. Button control is to invoke the cameracapture task and get the camera image and the image viewer control is to display the image which we took using the camera application. Drag and drop the images from the toolbox or write the XAML code as shown below.


XAML Code:

1

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">

2

<Button Content="Invoke Camera" Height="72" HorizontalAlignment="Left" Margin="36,30,0,0" Name="btnCamera" VerticalAlignment="Top" Width="389" Click="btnCamera_Click" />

3

<Image Height="377" HorizontalAlignment="Left" Margin="36,187,0,0" Name="imgCaptured" Stretch="Fill" VerticalAlignment="Top" Width="389" />

4

<TextBlock Height="39" HorizontalAlignment="Left" Margin="36,129,0,0" Name="txtResult" Text="" VerticalAlignment="Top" Width="389" />

5

</Grid>

clip_image004

Now we are done with our design, our task is to invoke the camera application and capture an image. Once captured we are going to assign the image to the Image Viewer control. To invoke the CameraCaptureTask we need to include the below 2 namespaces

C# Code:

1

using Microsoft.Phone.Tasks;

2

using Microsoft.Phone;

Now let us add the code bind with the required code. First we add class level variables which are used to invoke the camera task. Add the below class level variables with in the class scope. We are just creating an object of the CameraCaptureTask using the below code.

C# Code:

1

private CameraCaptureTask ccTask;

Now we need to invoke the Camera on the button click event, to do that first let us complete the object creating in the MainPage constructor and in the Button Event handler invoke the camera as shown in the code below.

C# Code:

01

public MainPage()

02

{

03

InitializeComponent();

04

 

05

ccTask = new CameraCaptureTask();

06

}

07

 

08

private void btnCamera_Click(object sender, RoutedEventArgs e)

09

{

10

ccTask.Show();

11

}

Now practically the camera application is invoked and with the inbuilt options we can capture the picture, now to save the picture we need to have a mechanism which can be done with the help of CameraCaptureTask Completed args. To do that first let us write some code to get the captured image and assign it to the image viewer control and then we will see on when to trigger this option. Write the below code in a separate method which normally gets the Photoresult and decode it to a byte array to show in on the image viewer control.

C# Code:

01

private void ccTaskCompleted(object sender, PhotoResult pr)

02

{

03

byte[] imgLocal;

04

if (pr.ChosenPhoto != null)

05

{

06

imgLocal = new byte[(int)pr.ChosenPhoto.Length];

07

pr.ChosenPhoto.Read(imgLocal, 0, imgLocal.Length);

08

pr.ChosenPhoto.Seek(0, System.IO.SeekOrigin.Begin);

09

 

10

var bitmapImage = PictureDecoder.DecodeJpeg(pr.ChosenPhoto);

11

this.imgCaptured.Source = bitmapImage;

12

 

13

}

14

}

Now we are done with our code to show the captured image to the imageviewer control (imgCaptured). Now when to trigger this code so that the image captured can be retrieved. We need to do this at the CameraCapturedTask completed event to do that we need to go back to the MainPage constructor at the top and add the below code.


C# Code:

1

public MainPage()

2

{

3

InitializeComponent();

4

 

5

ccTask = new CameraCaptureTask();

6

ccTask.Completed += ccTaskCompleted;

7

}

So the CameraCaptureTask completed will trigger the method to do the necessary to show the image in the image control. Now we are ready to build and check the output of the application. To do that Press F5 directly from the keyboard or by pressing build and execute option from the Visual Studio Tool Bar. Once the application is build and executed successfully we can see the output in the Windows Phone 7 Emulator as shown in the screen below.

clip_image006

Now to invoke the camera click on the InvokeCamera button and we can see the Camera Application is invoked. We will see an empty screen with the camera in built buttons as shown in the screen below.

clip_image008

Now click on the button at the top right corner so that the we can capture some image using the windows phone 7 emulator. Since we are using the emulator we will see a white screen with a rectangle to it. We can click on the Accept button at the bottom to capture the image as shown in the screen below. If we want to take a new image we can click on the Retake to capture again.

clip_image010

Now click on the Accept and we can see the image captured is assigned to the image viewer control on the main page as shown in the screen below. We can see a dull image since we are using the Windows Phone 7 Emulator, if we deploy the application on to the real device we can get a very good resolution of the picture.

clip_image012

Conclusion:

So in this article we have seen how to use the Camera Application to capture the images and use in the development of our application as per the requirements. Also we have seen the Launchers and Choosers CameraCaptureTask to build the application to capture the picture.

About Author: Karthikeyan Anbarasan, Microsoft MVP (Most Valuable Professional) in ASP.NET/IIS Architecture. He is the Founder and Chief Editor of the www.f5debug.net website and has authored books on Windows Phone and Business Intelligence(SSIS). He is also a Passionate Speaker and a Blogger on Microsoft Technologies.

You can Join Me On: Facebook, Twitter, Google Plus, LinkedIn

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading

Recent Posts

Random Posts

Most Viewed

URL Shortner

Create your own short urls!!!

Site Counter

free counters

Disclaimer

This is a personal weblog. The opinions expressed here represent my own and not those of my employer. For accuracy and official reference refer to MSDN/ TechNet. I have documented my personal experience on this blog.

Protected by Copyscape Web Plagiarism Finder

Site Meter