Export (0) Print
Expand All
Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

How to access the hardware camera shutter button in Windows Phone 8

Applies to: Windows Phone 8 and Windows Phone Silverlight 8.1 | Windows Phone OS 7.1

Starting with Windows Phone OS 7.1, your app can programmatically access the hardware shutter button on the phone. This topic shows you how to implement this feature in your app. The hardware shutter button can be used in camera apps that use the Microsoft.Devices.PhotoCamera or PhotoCaptureDevice classes to access the camera sensor. This topic is a continuation of How to create a base camera app for Windows Phone 8 and assumes you have already created the base camera project in that topic.

TipTip:

Starting with Windows Phone 8, apps can use the PhotoCaptureDevice class for advanced capture scenarios. PhotoCaptureDevice allows apps to control photo properties such as ISO, exposure compensation, and manual focus position (when available on the phone). This topic doesn’t demonstrate PhotoCaptureDevice; for more info on this class, see Advanced photo capture for Windows Phone 8.

This topic corresponds to the Basic Camera Sample.

Hardware shutter button access is managed through a series of events in the CameraButtons class, as described in the following table.

Event

Description

ShutterKeyHalfPressed

When the shutter button is pressed and held for about 800 milliseconds. A half-press faster than that will not trigger this event.

ShutterKeyPressed

When the shutter button receives a full press.

ShutterKeyReleased

When the shutter button is released.

These events can be used for capturing photos or video. When capturing video, the Start method must be called prior to the hardware shutter button press. If the capture source has not started, these events will not fire. For more information about capturing video in your Windows Phone app, see How to record video in a camera app for Windows Phone 8.

NoteNote:

In earlier exercises, a software shutter and auto-focus button was used for demonstrating programmatic access to the PhotoCamera API. To optimize the experience for the end user, we recommend that your app uses the hardware shutter button on the camera for auto-focus and shutter operations.

In this section, you implement these events to achieve the following app behavior:

  • A full press of the shutter button captures an image and saves it to the media library.

  • A half press of the shutter button initiates camera auto-focus.

  • A release of the shutter button cancels auto-focus.

To implement the hardware shutter button

  1. Open your base camera project created in How to create a base camera app for Windows Phone 8.

  2. Open the code-behind file for the main page, MainPage.xaml.cs, and add the following code to the OnNavigatedTo(NavigationEventArgs) method.

        // The event is fired when the shutter button receives a half press.
        CameraButtons.ShutterKeyHalfPressed += OnButtonHalfPress;
    
        // The event is fired when the shutter button receives a full press.
        CameraButtons.ShutterKeyPressed += OnButtonFullPress;
    
        // The event is fired when the shutter button is released.
        CameraButtons.ShutterKeyReleased += OnButtonRelease;
    
    
  3. In MainPage.xaml.cs, add the following code to the OnNavigatingFrom(NavigatingCancelEventArgs) method after calling the Dispose method. This code helps release memory related to the camera.

        CameraButtons.ShutterKeyHalfPressed -= OnButtonHalfPress;
        CameraButtons.ShutterKeyPressed -= OnButtonFullPress;
        CameraButtons.ShutterKeyReleased -= OnButtonRelease;
    
    
  4. In MainPage.xaml.cs, add the following code to the MainPage class.

            // Provide auto-focus with a half button press using the hardware shutter button.
            private void OnButtonHalfPress(object sender, EventArgs e)
            {
                if (cam != null)
                {
                    // Focus when a capture is not in progress.
                    try
                    {
                        this.Dispatcher.BeginInvoke(delegate()
                        {
                            txtDebug.Text = "Half Button Press: Auto Focus";
                        });
    
                        cam.Focus();
                    }
                    catch (Exception focusError)
                    {
                        // Cannot focus when a capture is in progress.
                        this.Dispatcher.BeginInvoke(delegate()
                        {
                            txtDebug.Text = focusError.Message;
                        });
                    }
                }
            }
    
            // Capture the image with a full button press using the hardware shutter button.
            private void OnButtonFullPress(object sender, EventArgs e)
            {
                if (cam != null)
                {
                    cam.CaptureImage();
                }
            }
    
            // Cancel the focus if the half button press is released using the hardware shutter button.
            private void OnButtonRelease(object sender, EventArgs e)
            {
    
                if (cam != null)
                {
                    cam.CancelFocus();
                }
            }
    
    

    This code uses these event handlers to call the Focus(), CaptureImage(), and CancelFocus() methods of the PhotoCamera class.

    NoteNote:

    When enabled on the hardware shutter button, auto-focus is delayed for a short interval of time to match the built-in camera experience.

  5. On a phone, run the app by selecting the Debug | Start Debugging menu command.

Show:
© 2015 Microsoft