Display Camera output in Windows Phone 7
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 the开发者_开发技巧re a way to have the camera output displayed in a panel?
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
According to Microsoft's UI Design and Interaction Guide [PDF], They do not allow developers to access the camera with any UI Elements.
This comes from page 127:
There are no direct UI elements associated with the Camera, but developers have access to the Camera in the Microsoft.Phone.Tasks namespace.
As of today (1/19/2010) you only officially have access to photos taken using the CameraCaptureTask. If you do not plan to submit your application to the marketplace then you can use the PhotoCamera class from the Microsoft.Phone.Media.Extended
namespace as outlined by Dan Ardelean and Kevin Marshall. See this post for a video demo and his blog for more details. Please note that your application will not pass marketplace certification if you use these assemblies as they are not part of the official SDK.
The short answer to your question is: no.
You can capture photos using the CameraCaptureTask
provided by the Windows Phone 7 API (here), but, to the best of my knowledge, you cannot capture a live stream of data from the camera.
Microsoft has not announced whether this functionality will be augmented in a future release of the platform.
Example of using CameraCaptureTask
:
public partial class MainPage : PhoneApplicationPage
{
// Declare the CameraCaptureTask object with page scope.
CameraCaptureTask cameraCaptureTask;
// Constructor
public MainPage()
{
InitializeComponent();
// Initialize the CameraCaptureTask and assign the Completed handler in the page constructor.
cameraCaptureTask = new CameraCaptureTask();
cameraCaptureTask.Completed += new EventHandler<PhotoResult>(cameraCaptureTask_Completed);
}
// In this example, the CameraCaptureTask is shown in response to a button click.
private void button1_Click(object sender, RoutedEventArgs e)
{
cameraCaptureTask.Show();
}
// The Completed event handler. In this example, a new BitmapImage is created and
// the source is set to the result stream from the CameraCaptureTask
void cameraCaptureTask_Completed(object sender, PhotoResult e)
{
if (e.TaskResult == TaskResult.OK)
{
BitmapImage bmp = new BitmapImage();
bmp.SetSource(e.ChosenPhoto);
myImage.Source = bmp;
}
}
}
As noted here, we're limited to CameraCaptureTask Functionality for the moment.
Information has been released recently to indicate functionality to support AR is on the roadmap.
精彩评论