In this article we are going to see how to make use of the Camera Capture Chooser task with Windows phone 7 application development. With this task we can provide an option to the end user to trigger the in built camera application and take a photo that can be used with in the application by an event raised once the photo is taken. Let us see the steps on how to achieve this task in real time for a Windows phone application. [more]
Open Visual Studio 2010 IDE and create a new Silverlight for Windows Phone project with a valid project name as shown in the screen below. Once the project is created add some controls which are used to trigger the Camera Capture Task as shown in the screen below.
Now drag and drop few controls to the screen which is used to trigger event that launches the Camera Capture Chooser Task. Once we designed the screen with the controls we can see the screen looks like below. We have added a button control to trigger the event and image control to show the captured Image.
Now we need to write our code in the button click event to trigger the Camera Capture task on the user click event. To do that just go to the code behind and first add the USING handler code on top with the existing using statements as shown in the code below.
using Microsoft.Phone.Tasks;
Next is to add the below code to the button click event which basically creates an instance of the Camera capture Chooser task. Once the task instance is created we have written a code which handles the complete event of the task so that in the result we have the captured image is passed as an object which we can handle as per the requirement as shown in the code below.
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Tasks;
using System.Windows.Media.Imaging;
namespace F5debugHowto21
{
public partial class MainPage : PhoneApplicationPage
{
CameraCaptureTask ccTask;
// Constructor
public MainPage()
{
InitializeComponent();
ccTask = new CameraCaptureTask();
ccTask.Completed += new EventHandler<PhotoResult>(ccTask_Completed);
}
private void button1_Click(object sender, RoutedEventArgs e)
{
ccTask.Show();
}
void ccTask_Completed(object sender, PhotoResult objPhoto)
{
if (objPhoto.TaskResult == TaskResult.OK)
{
image1.Source = new BitmapImage(new Uri(objPhoto.OriginalFileName));
}
}
}
}
If the application that uses the Camera Capture task is targeting the devices with 256 MB ram then we no need to consider on the performance as the memory allocation for the Camera Capture task is not counted towards the application memory usage. Since when our application which holds the Camera Capture task triggers the Camera application for capturing the photo, our base application will be deactivated since the camera application is in foreground in those cases to handle the photo result we need to declare the object on to the class scope so that once the camera application completes its task the page is called again which triggers the constructor code to handle the photo result object.
Now we are done with our code, just run the application by pressing F5 directly from the keyboard or we can use the Build and execute the project option from the tool bar to run the application. Once the Build is successful we can see the Windows Phone emulator with the application and the expected outputs as shown in the screens below.
Output Screens:
Since we are trying to build and execute the application in a Windows Phone Emulator which don’t have a inbuilt camera we get some dummy image. With this application running on a device with the help of the inbuilt camera we will get the real image. That’s it from this tutorial on Windows Phone see you all in the next tutorial soon. Mean while Happy Programming!!!
No Comments
I do not even know how I ended up here, but I thought this post was good. I don't know who you are but certainly you are going to a famous blogger if you are not already 😉 Cheers!|
Greetings! Very useful advice within this article! It's the little changes that make the most significant changes. Many thanks for sharing!|
Howdy just wanted to give you a quick heads up and let you know a few of the images aren't loading correctly. I'm not sure why but I think its a linking issue. I've tried it in two different browsers and both show the same results.|
You ought to be a part of a contest for one of the best websites online. I most certainly will recommend this site!|