다음을 통해 공유


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

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

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.

Tip

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.

Note

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.

Implementing the hardware shutter button

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;
    
        ' The event is fired when the shutter button receives a half press.
        AddHandler CameraButtons.ShutterKeyHalfPressed, AddressOf OnButtonHalfPress
    
        ' The event is fired when the shutter button receives a full press.
        AddHandler CameraButtons.ShutterKeyPressed, AddressOf OnButtonFullPress
    
        ' The event is fired when the shutter button is released.
        AddHandler CameraButtons.ShutterKeyReleased, AddressOf 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;
    
        RemoveHandler CameraButtons.ShutterKeyHalfPressed, AddressOf OnButtonHalfPress
        RemoveHandler CameraButtons.ShutterKeyPressed, AddressOf OnButtonFullPress
        RemoveHandler CameraButtons.ShutterKeyReleased, AddressOf 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();
                }
            }
    
        ' Provide auto-focus with a half button press using the hardware shutter button.
        Private Sub OnButtonHalfPress(ByVal sender As Object, ByVal e As EventArgs)
            If cam IsNot Nothing Then
                ' Focus when a capture is not in progress.
                Try
                    Me.Dispatcher.BeginInvoke(Sub() txtDebug.Text = "Half Button Press: Auto Focus")
                    cam.Focus()
    
                Catch focusError As Exception
    
                    'Cannot focus when a capture is in progress.
                    Me.Dispatcher.BeginInvoke(Sub() txtDebug.Text = focusError.Message)
    
                End Try
            End If
        End Sub
    
        ' Capture the image with a full button press using the hardware shutter button.
        Private Sub OnButtonFullPress(ByVal sender As Object, ByVal e As EventArgs)
            If cam IsNot Nothing Then
                cam.CaptureImage()
            End If
        End Sub
    
        ' Cancel the focus if the half button press is released using the hardware shutter button.
        Private Sub OnButtonRelease(ByVal sender As Object, ByVal e As EventArgs)
            If cam IsNot Nothing Then
                cam.CancelFocus()
            End If
        End Sub
    

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

Note

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

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

See Also

Other Resources

Advanced photo capture for Windows Phone 8

Capturing video for Windows Phone 8

Lenses for Windows Phone 8

How to use the camera capture task for Windows Phone 8