MOBILE

Windows Phone 8 : Working with the Camera (part 2) - Raw Hardware Access

9/21/2013 4:50:34 AM

2. Raw Hardware Access

The Windows Phone SDK includes a couple of key classes that support low-level access to the camera and microphone hardware. Although it is much easier to use the PhotoCamera class, if you need to capture video and/or audio and manipulate it in real time, these APIs are the best tool for the job.

The starting point to the raw camera API is the CaptureSource class. The CaptureSource class enables you to have access to video input on the phone. Like the PhotoCamera class, you can use the CaptureSource class as the source for a VideoBrush. This way, you can show the raw input from the camera in your application:

CaptureSource _src = new CaptureSource();

protected override void OnNavigatedTo(NavigationEventArgs e)
{
  // Show the preview
  previewBrush.SetSource(_src);
}

To enable the video, you need to enable the CaptureSource by using the Start method:

private void camButton_Click(object sender, RoutedEventArgs e)
{
  _src.Start();
}


Silverlight Developers

Unlike desktop Silverlight, you can start the CaptureSource without asking for permission with the CaptureDeviceConfiguration class.


At this point, you have only the camera showing up in your own application. To deal with live input from the camera and microphone, you have to create special classes called sinks. To retrieve video you need a VideoSink class; for audio you need an AudioSink class. These are abstract classes from which you must derive in order to retrieve real-time data from the hardware. They have a small number of abstract methods (that you have to override). A skeleton VideoSink derived class would look like this:

public class MyVideoSink : VideoSink
{
  protected override void OnSample(long
sampleTimeInHundredNanoseconds,
    long frameDurationInHundredNanoseconds,
    byte[] sampleData)
  {
    // Encode or Save the stream
  }

  protected override void OnCaptureStarted()
  {
    // Handle Startup of Capture
  }

  protected override void OnCaptureStopped()
  {
    // Cleanup After Capture
  }

  VideoFormat _format = null;

  protected override void OnFormatChange(VideoFormat videoFormat)
  {
     // Store the Video Format
    _format = videoFormat;
  }
}

Here are the abstract methods you must override.

OnCaptureStarted: This is where you can do any initialization necessary to prepare for data capture.

OnCaptureStopped: This is where you can clean up or save after a capture is complete.

OnFormatChange: This is where you would store the current format to determine how to consume the capture samples.

OnSample: This is where most of the real work is accomplished in the sink. This is called periodically with a set of samples from the hardware.

After you have a sink, you can specify the CaptureSource of the sink before you start capturing:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
  // Show the preview
  previewBrush.SetSource(_src);

  // Capture the video
  _sink.CaptureSource = _src;
}

When you specify the CaptureSource, your sink will be notified because the CaptureSource is manipulated by the user. In this way, you can have more than one audio and video sink per CaptureSource.

Because the raw camera API originated from Silverlight, it enables you to specify the camera on the device you want. The CaptureDeviceConfiguration class allows you to enumerate the different audio and video capture devices. In this way, you can specify the capture device for the CaptureSource to use, like so:

ICollection<VideoCaptureDevice> devices =
  CaptureDeviceConfiguration.GetAvailableVideoCaptureDevices();

// Pick the first device
_src.VideoCaptureDevice = devices.First();

The API supports multiple devices so that, in the future, if there are multiple video devices (for example, a front-facing camera), your code will be able to pick which device to use. By default, the CaptureSource uses the “primary device,” which is usually the standard camera, so working with this API isn’t necessary with the current crop of phones.

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