MOBILE

Windows Phone 8 : Working with the Camera (part 1) - Using the PhotoCamera Class

9/21/2013 4:49:03 AM

For the most basic usage of the camera on the phone, the CameraCaptureTask  will enable you to launch the camera and return an image. In many cases, it is more useful to have full control over the camera for real-time access to the camera hardware. For building your own camera applications, the PhotoCamera class represents access to the photo hardware. When you need real-time access to the hardware, Windows Phone also exposes raw hardware access APIs (including CaptureSource, AudioSink, and VideoSink).

1. Using the PhotoCamera Class

The PhotoCamera class represents the phone’s camera and allows you to easily build photo-taking applications with it. To get started, you will need an instance of the PhotoCamera class. You can use that class as the source of a VideoBrush to paint the “viewfinder” in your application, like so:

public partial class MainPage : PhoneApplicationPage
{
  PhotoCamera theCamera = null;

  protected override void OnNavigatedTo(NavigationEventArgs e)
  {
    theCamera = new PhotoCamera();

    // Set the Camera as the source for the VideoBrush
    previewBrush.SetSource(theCamera);

    ...

Using the PhotoCamera as the source of the VideoBrush causes a real-time image to be painted wherever you’re using a VideoBrush. After you have the camera created and showing up on the page, you need to wire up the camera’s functionality to use it. The first thing to do in wiring the camera is to handle the Initialized event. This event is important to handle, as many of the camera settings (for example, setting the focus or the flash) are not available until this event is fired. For example, to set the flash for red-eye reduction, handle the Initialized event and then set the flash like so:

// Some settings require camera to be initialized first
theCamera.Initialized += (s, a) =>
  {
    if (a.Succeeded)
    {
      // Taking Portraits
      if (theCamera.IsFlashModeSupported(FlashMode.RedEyeReduction))
      {
        theCamera.FlashMode = FlashMode.RedEyeReduction;
      }
    }
  };

The Initialized event argument includes a property to tell you whether it was successful (the Succeeded property). You should check this first to be certain that you can actually modify the PhotoCamera’s properties. In this case, we ask the camera if the red-eye reduction flash is supported on this phone, and if so, we can specify that is the flash mode we want.

To take a photo, you can use the PhotoCamera’s CaptureImage method. This tells the camera to take the photo and then fires events for both a full-size and a thumbnail version of the image. Typically, this is in response to some event such as a button click:

private void shutter_Click(object sender, RoutedEventArgs e)
{
  theCamera.CaptureImage();
}

After the image has been captured, the PhotoCamera class can raise the CaptureImageAvailable and CaptureThumbnailAvailable events. These events include the actual stream that contains the image, so you can handle these events and manipulate the resultant photo. For example, you can take the full image and save it to the media library:

// Photo was captured
theCamera.CaptureImageAvailable += (s,a) =>
  {
    // Save picture to the device media library.
    MediaLibrary library = new MediaLibrary();
    library.SavePictureToCameraRoll("SomeFile.jpg", a.ImageStream);
  };

The CaptureImageAvailable event argument includes an ImageStream property that contains the raw photo. You can manipulate this stream in any way you want, although saving it to the phone is more typical of what a user might expect.

Like you saw earlier, the PhotoCamera class offers several settings, including

• Flash mode

• Picture resolution

In addition, you can control the focus by using the Focus and Focus-AtPoint methods. These methods enable you to start the auto-focus functionality. You need to test to see whether these capabilities are available:

private void focus_Click(object sender, RoutedEventArgs e)
{
  // AutoFocus the Camera
  if (theCamera.IsFocusSupported)
  {
    theCamera.Focus();
  }
}

Focusing at a specified point works the same way, but you need to specify a place to center on. The values should be between 0.0 and 1.0, so focus at whatever is in the center of the viewfinder:

private void focusAtPoint_Click(object sender, RoutedEventArgs e)
{
  // Focus in the center of the image
  if (theCamera.IsFocusAtPointSupported)
  {
    theCamera.FocusAtPoint(.5, .5);
  }
}

Before taking a photo, you will need to be sure the focusing is complete. To do this, you can handle the AutoFocusComplete event:

bool _isFocusComplete = true;
...
theCamera.AutoFocusCompleted += (s, a) =>
  {
    _isFocusComplete = true;
  };

By keeping the state of whether your application is currently focusing, you can control when a picture is taken by flipping this flag during focus and shutter operations:

private void focus_Click(object sender, RoutedEventArgs e)
{
  // AutoFocus the Camera
  if (theCamera.IsFocusSupported)
  {
    _isFocusComplete = false;
    theCamera.Focus();
  }
}

private void shutter_Click(object sender, RoutedEventArgs e)
{
  if (_isFocusComplete) theCamera.CaptureImage();
}

You might want to use the hardware camera shutter to handle focus and shutter functions like the built-in camera application. The CameraButtons class gives you that capability. This class supports three events.

ShutterKeyPressed: This occurs when the user fully presses the phone’s dedicated camera button.

ShutterKeyHalfPressed: This occurs when the user partially presses the phone’s dedicated camera button. (It is usually used to start a focus operation.)

ShutterKeyReleased: This occurs when the user releases the phone’s dedicated camera button after fully pressing the button. (For example, it does not occur when the user half-presses the camera button.)

To allow the hardware button to control the camera app, you could handle these events as necessary:

// Wire up shutter button too (instead of UI control)
CameraButtons.ShutterKeyPressed += (s, a) => TakePicture();
CameraButtons.ShutterKeyHalfPressed += (s, a) => theCamera.Focus();

Finally, the PhotoCamera class gives you raw access to the preview buffer if you need to manipulate what the user sees in the virtual viewfinder. You would do this via the GetPreviewBufferXXX methods. You can get the buffer as 32-bit ARGB values, YCbCr, or just luminance data. You can see how this works here:

int[] buffer = new int[640 * 480];
theCamera.GetPreviewBufferArgb32(buffer);
// Manipulate the preview and show on the screen

The buffer you receive from these methods would have to be used to create a bitmap to show to the user. Often it is easier to just use the raw camera APIs (shown next) to accomplish this sort of real-time manipulation of the camera’s feed.


Camera Capabilities

To use the PhotoCamera class (and other associated classes), you will need to ensure that your application includes the ID_CAP_ISV_CAMERA capability in the WMAppManifest.xml file. You can see this capability in the WMAppManifest.xml editor, as shown in Figure 1.


Image

FIGURE 1 Setting the camera capability

Other  
  •  BlackBerry Development : Pushing Data to External Users - Web Signals (part 6) - Building a Web Signal - Unsubscribing from a Subscription
  •  BlackBerry Development : Pushing Data to External Users - Web Signals (part 5) - Building a Web Signal - Requesting the Status of a Subscription
  •  BlackBerry Development : Pushing Data to External Users - Web Signals (part 4) - Building a Web Signal - Pushing Data to Subscribers
  •  BlackBerry Development : Pushing Data to External Users - Web Signals (part 3) - Building a Web Signal - Web Signal Domains, Web Signal Subscriber Registration
  •  BlackBerry Development : Pushing Data to External Users - Web Signals (part 2) - Signing Up for Web Signals
  •  BlackBerry Development : Pushing Data to External Users - Web Signals (part 1) - How Web Signals Work
  •  Holiday Gift Guide – Smartphones – Aug 2013
  •  Holiday Gift Guide – Tablets – Aug 2013
  •  Wacom Cintiq 13HD - A Space-Saving Pen Display For Designers (Part 2)
  •  Wacom Cintiq 13HD - A Space-Saving Pen Display For Designers (Part 1)
  •  
    Top 10
    Review : Sigma 24mm f/1.4 DG HSM Art
    Review : Canon EF11-24mm f/4L USM
    Review : Creative Sound Blaster Roar 2
    Review : Philips Fidelio M2L
    Review : Alienware 17 - Dell's Alienware laptops
    Review Smartwatch : Wellograph
    Review : Xiaomi Redmi 2
    Extending LINQ to Objects : Writing a Single Element Operator (part 2) - Building the RandomElement Operator
    Extending LINQ to Objects : Writing a Single Element Operator (part 1) - Building Our Own Last Operator
    3 Tips for Maintaining Your Cell Phone Battery (part 2) - Discharge Smart, Use Smart
    REVIEW
    - First look: Apple Watch

    - 3 Tips for Maintaining Your Cell Phone Battery (part 1)

    - 3 Tips for Maintaining Your Cell Phone Battery (part 2)
    VIDEO TUTORIAL
    - How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 1)

    - How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 2)

    - How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 3)
    Popular Tags
    Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Biztalk Exchange Server Microsoft LynC Server Microsoft Dynamic Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 Adobe Indesign Adobe Flash Professional Dreamweaver Adobe Illustrator Adobe After Effects Adobe Photoshop Adobe Fireworks Adobe Flash Catalyst Corel Painter X CorelDRAW X5 CorelDraw 10 QuarkXPress 8 windows Phone 7 windows Phone 8