我正在为 Windows Phone 7 编写增强现实应用程序作为学校项目。我想获得相机输出,然后在其上添加一层数据。有没有办法让相机输出显示在面板中?
5 回答
仅供参考:在 Windows Phone SDK 7.1(又名“Mango”)中,您现在可以编写使用您所描述的设备摄像头的应用程序。有关最新 7.1 开发工具的链接,请参阅App Hub 。该文档在以下链接中描述了如何执行此操作:
但基本上,添加一个视频画笔来显示相机馈送(又名“取景器”)。例如,这里使用了一个矩形控件来显示相机取景器:
<!--Camera viewfinder >-->
<Rectangle Width="640" Height="480"
HorizontalAlignment="Left"
x:Name="viewfinderContainer">
<Rectangle.Fill>
<VideoBrush x:Name="viewfinderBrush" />
</Rectangle.Fill>
</Rectangle>
要在页面的代码隐藏中使用相机,请添加对 Microsoft.XNA.Framework 的引用并将以下 Using 语句放在页面顶部:
// Directives
using Microsoft.Devices;
using System.IO;
using System.IO.IsolatedStorage;
using System.Windows.Media.Imaging;
using Microsoft.Xna.Framework.Media;
注意:您可能不需要所有这些,我只是从文档中复制的。在 Visual Studio(至少是 Pro)中,您可以在完成后通过右键单击代码文件并单击:Organize Usings |来清理它们。删除未使用的 Using。
然后,基本上你将相机图像应用到OnNavigatedTo处理程序中的矩形......
//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);
}
...并在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.
}
7.1 文档还在以下主题中描述了增强现实应用程序。请注意,您需要向下滚动到标题为“创建基于 Silverlight 的增强现实应用程序”的部分,以找到使用 Mango 构建它的说明。
如何:使用适用于 Windows Phone 的组合运动 API
希望这也有助于其他人在 Windows Phone OS 7.1 中寻找有关 PhotoCamera 的信息。
干杯
根据 Microsoft 的UI 设计和交互指南[PDF],它们不允许开发人员使用任何 UI 元素访问相机。
这来自第 127 页:
没有与相机关联的直接 UI 元素,但开发人员可以访问 Microsoft.Phone.Tasks 命名空间中的相机。
如此处所述,我们目前仅限于 CameraCaptureTask 功能。
最近发布的信息表明支持 AR 的功能已在路线图上。
对您的问题的简短回答是:不。
CameraCaptureTask
您可以使用Windows Phone 7 API(此处)提供的照片来捕捉照片,但据我所知,您无法从相机捕捉实时数据流。
Microsoft 尚未宣布是否会在该平台的未来版本中增强此功能。
使用示例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;
}
}
}