Taking a Photo with the CameraCaptureTask

,

The camera capture task is a chooser that is used to launch the built-in Camera app. If the user completes the task by taking and confirming a photo, the Completed event is raised and the event handler receives a PhotoResult object, which exposes a stream containing the image data.

For information on working with photo image streams, see Chapter 20, “Extending the Windows Phone Picture Viewer.”

The CameraCaptureTask should be defined as a field in your class, like so:

readonly CameraCaptureTask cameraCaptureTask
                                     = new CameraCaptureTask();

Subscribe to the CameraCaptureTask.Completed event within your class constructor, as shown:

cameraCaptureTask.Completed
    += new EventHandler<PhotoResult>(HandleCameraCaptureTaskCompleted);

Use the Show method of the CameraCaptureTask to launch the built-in Camera app. When the task completes the handler receives a PhotoResult object, as shown:

void HandleCameraCaptureTaskCompleted(object sender, PhotoResult e)
{
    if (e.TaskResult == TaskResult.OK)
    {
        BitmapImage bitmapImage = new BitmapImage();
        bitmapImage.SetSource(e.ChosenPhoto);
        CapturedImage = bitmapImage;
    }
    else if (e.Error != null)
    {
        MessageService.ShowError("An error occurred. " + e.ToString());
    }
    else if (e.TaskResult == TaskResult.Cancel)
    {
        /* Cancelled. */
    }
}

Sample Overview

Example code for the CameraCaptureTask is located in the CameraCaptureViewModel in the downloadable sample code.

The view contains a button that is bound to a viewmodel ICommand property named CaptureCommand.

The CameraCaptureView allows the user to launch the CameraCaptureTask when she taps the button. When the captureCommand is executed, the Show method of the cameraCaptureTask is called, as shown in the following excerpt:

public CameraCaptureViewModel() : base("capture image")
{
    cameraCaptureTask.Completed += HandleCameraCaptureTaskCompleted;
    captureCommand = new DelegateCommand(obj => cameraCaptureTask.Show());
}

Calling the task’s Show method launches the built-in Camera application (see Figure 14.26).

Image

FIGURE 14.26 The built-in Camera application.

After the photo has been taken and accepted by the user, the task’s Completed event handler is called, and a new BitmapImage is created using the e.ChosenPhoto image Stream. It is then assigned to the CapturedImage property of the viewmodel, as shown:

BitmapImage bitmapImage = new BitmapImage();
bitmapImage.SetSource(e.ChosenPhoto);
CapturedImage = bitmapImage;

An Image control in the view is used to display the image, as shown:

<Image Source="{Binding CapturedImage}" />

When the CapturedImage property is updated, the image is displayed in the view (see Figure 14.27).

Image

FIGURE 14.27 The captured image is displayed on the CameraCaptureView page.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.208.200