Display Camera output in Windows Phone 7

augmented-reality, camera, windows-phone-7

Solution

FYI: In Windows Phone SDK 7.1 (a.k.a. "Mango") you can now write apps that use the device camera as you describe. See App Hub for a link to the latest 7.1 development tools. The documentation describes how to do this at the following link:

How to: Create a Base Camera Application for Windows Phone

But basically, add a videobrush to display the camera feed (a.k.a. the "viewfinder"). For example, here a rectangle control is used display the camera viewfinder:

    <!--Camera viewfinder >-->
    <Rectangle Width="640" Height="480" 
               HorizontalAlignment="Left" 
               x:Name="viewfinderContainer">

        <Rectangle.Fill>
            <VideoBrush x:Name="viewfinderBrush" />
        </Rectangle.Fill>
    </Rectangle>

To use the camera in the code-behind for the page, add a reference to Microsoft.XNA.Framework and put the following Using statements at top of the page:

// Directives
using Microsoft.Devices;
using System.IO;
using System.IO.IsolatedStorage;
using System.Windows.Media.Imaging;
using Microsoft.Xna.Framework.Media;

Note: you may not need all of these, I just copied it from the docs. In Visual Studio (Pro, at least), you can clean them up after you're done by right-clicking your code file and clicking: Organize Usings | Remove Unused Usings.

Then, basically you apply the camera image to the rectangle in the OnNavigatedTo handler...

    //Code for initialization and setting the source for the viewfinder
    protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {

        // Initialize camera
        cam = new Microsoft.Devices.PhotoCamera();

        //Set the VideoBrush source to the camera.
        viewfinderBrush.SetSource(cam);
    }

...and dispose of the camera object in the OnNavigatingFrom.

    protected override void OnNavigatingFrom(System.Windows.Navigation.NavigatingCancelEventArgs e)
    {
        // Dispose camera to minimize power consumption and to expedite shutdown.
        cam.Dispose();

        // Good place to unhook camera event handlers too.
    }

The 7.1 docs also describe an augmented reality app in the following topic. Note that you'll need to scroll-down to the section titled Creating a Silverlight-based Augmented Reality Application, to find the instructions for building it with Mango.

How to: Use the Combined Motion API for Windows Phone

Hope that also helps others seeking information about PhotoCamera in Windows Phone OS 7.1.

Cheers

Problem

I'm writing an augmented reality app for Windows Phone 7 as a school project. I want to get the camera output and then add a layer of data over it. Is there a way to have the camera output displayed in a panel?

Original source