2014-10-06 132 views
1

我正在尝试创建一个基本的摄像头应用程序(针对WP的第一个应用程序)。当然,我想在拍摄前将相机数据预览到屏幕上。在Windows Phone 8.1上访问摄像头预览流

但唯一的样品我看到从MSDN等网络太旧(很多对象已被删除,即正在更新的库经常使得文章过时)

我在刚开始使用的一个问题预览流。如果有知识的人可以在这个问题上给我一些帮助,那将不胜感激。

谢谢。

回答

1

我建议使用CaptureElement控制

在您的XAML补充一点:

<CaptureElement x:Name="Capture" 
       Stretch="UniformToFill" 
       FlowDirection="LeftToRight" /> 

我不知道,如果你想用正面或背面的摄像头,所以我会为这两个显示的代码。

在您的代码隐藏(或您的视图模型,如果你使用MVVM)添加以下代码:

// First need to find all webcams 
DeviceInformationCollection webcamList = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture) 

// Then I do a query to find the front webcam 
DeviceInformation frontWebcam = (from webcam in webcamList 
where webcam.EnclosureLocation != null 
&& webcam.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Front 
select webcam).FirstOrDefault(); 

// Same for the back webcam 
DeviceInformation backWebcam = (from webcam in webcamList 
where webcam.EnclosureLocation != null 
&& webcam.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Back 
select webcam).FirstOrDefault(); 

// Then you need to initialize your MediaCapture 
var newCapture = new MediaCapture(); 
await newCapture.InitializeAsync(new MediaCaptureInitializationSettings 
{ 
    // Choose the webcam you want (backWebcam or frontWebcam) 
    VideoDeviceId = backWebcam.Id, 
    AudioDeviceId = "", 
    StreamingCaptureMode = StreamingCaptureMode.Video, 
    PhotoCaptureSource = PhotoCaptureSource.VideoPreview 
}); 

// Set the source of the CaptureElement to your MediaCapture 
Capture.Source = newCapture; 

// Start the preview 
await newCapture.StartPreviewAsync(); 

这将显示在您的CaptureElement控制所选择的网络摄像头的流,并且可以在Windows手机8.1和Windows 8.1

+0

非常感谢!这工作完美 – Tokfrans 2014-10-06 22:52:39

+0

很高兴它帮助! – 2014-10-06 22:53:07