Advanced handling of Photo Capture using PhotoCamera in Windows Phone

By | November 16, 2013

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.

One thought on “Advanced handling of Photo Capture using PhotoCamera in Windows Phone

  1. Pingback: Windows Store App Developer Links – 2013-11-20 | Dan Rigby

Comments are closed.