Augmented Reality and Windows Phone 7 Part 2

June 30, 2011 — Leave a comment

A few months ago I wrote about augmented reality on Windows Phone 7 and how the possibilities are there as the platform supports it but it’s all hidden away to third party developers.

Now that blog post was back on August 24 2010 less than 12 months ago.  Now with Windows Phone Mango, Augmented Reality scenario on Windows Phone is now a “reality”.  Kudos goes out to the Windows Phone team for getting this out in less than 12month! It was already there but my guess is it wasn’t tested enough for general use!

How Do I Use It!

Pre-Mango, we only had access to the CameraCaptureTask which would go ‘outside’ of our application and return us a back picture stream that the user took.  Not the most ideal scenario from a developers point of view but got the job done.

With Mango APIs we now have direct access to the camera and able to stream directly onto a page. You also have the ability take pictures and have various events to tell you what the user is doing, for example CameraButtons.ShutterKeyPressed which tells you when the camera hardware button was pressed.

Now I’m not going to go through how to implement augmented reality on Windows Phone as there is a good sample on MSDN on creating an Augmented Reality app on Windows Phone in combination using the Motion Sensor APIs and camera stream.  I will go through the APIs that allow you to use the camera stream.

First thing, create a new standard Windows Phone Application project

image

To add a video camera stream to your page is quite easy.  First thing add a VideoBrush as the Background to the Grid named ContentPanel in MainPage.xaml.  It should look something like this.

Be aware that the VideoBrush is new to Mango so you won’t be able to use this pre-Mango.  Here is the object model for video brush.

image

Note, that there is a CameraVideoBrushExtensions class.  Again this is Mango only, and this has a single method called SetSource() which allows you to add a video stream to the VideoBrush.

Now, to add the stream you create a PhotoCamera  object and call the VideoBrush.SetSource method with the PhotoCamera object as the parameter.  Here is the object model for the PhotoCamera

image

To set the source we create a new global PhotoCamera object  in our MainPage.xaml.cs as follows

Then in the constructor of MainPage we add the following

Your MainPage.xaml.cs should like like the following

Now, when you run the project you will get a stream in the ContentPanel.  You can try this on a real device or on the emulator.  In the emulator you will get a black box moving around a white box.

 

image

Also, the Windows Phone project when first created will automatically add ID_CAP_ISV_CAMERA to your WMAppManifest.xml.  This capability is required if you are using the camera.

And that’s it.  Three lines of XAML and four lines of C#, pretty straight forward to get a camera stream to your app.  Don’t think iPhone or Android devs have it this easy Smile

Feedback, let me know here or via twitter.

No Comments

Be the first to start the conversation.

Leave a Reply