目前国内有关Windows Phone 8.1开发的书籍非常少,对于摄像头的调用基本是一片空白,网上有博客介绍过Silverlight8.1如何调用摄像头模块,单非常不幸的是Windows Phone的API在8.1发生了变化。之前可以预览摄像头画面的VideoBrush已经停止使用,通过国外的一些博客了解到Windows Phone 8.1中的CaptureElement组件可以预览摄像头画面。
一开始的调用摄像头的问题解决了,但是发现摄像头的方向偏转了90度,同样经过几经波折后在国外的论坛上找到了答案。下面是程序全代码(xaml中只有两个按钮和一个CaptureElement,所以不给出xaml代码了),关于如何将画面保存,这方面的博客非常多,我也就不介绍了。
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
this.NavigationCacheMode = NavigationCacheMode.Required;
}
Windows.Media.Capture.MediaCapture captureManager;
async private void Start_Capture_Preview_Click(object sender, RoutedEventArgs e)
{
captureManager = new MediaCapture();
await captureManager.InitializeAsync();
captureManager.SetPreviewRotation(VideoRotation.Clockwise90Degrees);
capturePreview.Source = captureManager;//定义的CaptureElement的Name为captureView
await captureManager.StartPreviewAsync();
SimpleOrientationSensor sensor = SimpleOrientationSensor.GetDefault();
sensor.OrientationChanged += (s, arg) =>
{
switch (arg.Orientation)
{
case SimpleOrientation.Rotated90DegreesCounterclockwise:
captureManager.SetPreviewRotation(VideoRotation.None);
break;
case SimpleOrientation.Rotated180DegreesCounterclockwise:
case SimpleOrientation.Rotated270DegreesCounterclockwise:
captureManager.SetPreviewRotation(VideoRotation.Clockwise180Degrees);
break;
default:
captureManager.SetPreviewRotation(VideoRotation.Clockwise90Degrees);
break;
}
};
}
async private void Stop_Capture_Preview_Click(object sender, RoutedEventArgs e)
{
await captureManager.StopPreviewAsync(); //stop camera capturing
}
}