Advanced handling of Photo Capture using PhotoCamera in Windows Phone

Well, as I have already dealt with CameraCaptureTask or PhotoChooserTask before for simple scenarios, it is time to go a little further with dealing with continuous capture or even how to switch between FrontFacing Camera or Primary camera etc. To deal with such scenarios, we use the PhotoCamera class and show the stream from camera directly to a canvas or take picture from it. Let us look into the steps to create an app that can make use of Camera Streams directly.

1. First of all, you need to connect to the Cameara. To connect to the Camera, you need to create an object of PhotoCamera and pass the PhotoCamera Source to a canvas element on the App.

private PhotoCamera Camera { get; set; }

var cameraType = CameraType.FrontFacing;
if (!PhotoCamera.IsCameraTypeSupported(cameraType))
    cameraType = CameraType.Primary;
if(!PhotoCamera.IsCameraTypeSupported(cameraType))
{
    MessageBox.Show("Cannot find a camera on this device");
    return;
}
this.Camera = new PhotoCamera(cameraType);

In the above code, the PhotoCamera is initialized. Once initialized, ou can set the Camera photo streams to an element in the screen. To do that, we put a border and directly write the stream using VideoBrush.

<Border x:Name="cameraView" BorderBrush="Black" BorderThickness="5" Grid.Column="0">
       <Border.Background>
           <VideoBrush x:Name="cameraBrush" />
       </Border.Background>
   </Border>

Here the background of the border is filled using a VideoBrush. Now to set the CameraStream to the background of the Border, we simply write as :

this.cameraBrush.SetSource(this.Camera);

Now to capture an image using the PhotoCamera, we call

this.Camera.CaptureImage();

The above code generates CaptureImageAvailable and CaptureThumbnailAvailable events. When the image is available you can write the data directly from the stream to a disk file.

private void Camera_CaptureImageAvailable(object sender, ContentReadyEventArgs e)
{
     Deployment.Current.Dispatcher.BeginInvoke(() =>
     {
         MediaLibrary ml = new MediaLibrary();
         ml.SavePictureToCameraRoll(string.Format("{0:yyyyMMdd-HHmmss}.jpg", DateTime.Now), e.ImageStream);
     });
 }

In the above code, you can see the Image when available after CaptureImage is called, is saved to CameraRoll folder of MediaLibrary. The e.ImageStream will hold the image that is been captured from PhotoCamera object. Using this API, you can capture images continuously and add it to the application and/or disk.

I hope this post will come handy. Will post more about PhotoCamera in subsequent entries. Stay tune.

Abhishek Sur

Abhishek Sur is a Microsoft MVP since year 2011. He is an architect in the .NET platform. He has profound theoretical insight and years of hands on experience in different .NET products and languages. He leads the Microsoft User Group in Kolkata named KolkataGeeks, and regularly organizes events and seminars in various places for spreading .NET awareness. He is associated with the Microsoft Insider list on WPF and C#, and is in constant touch with product group teams. He blogs at http://www.abhisheksur.com His Book : Visual Studio 2012 and .NET 4.5 Expert Development Cookbook. Follow Abhishek at Twitter : @abhi2434

One Comment to “Advanced handling of Photo Capture using PhotoCamera in Windows Phone”

Comments are closed.