我正在尝试创建一个基本的相机应用程序(我的第一个应用程序针对WP)。当然,我想在拍摄之前将相机数据预览到屏幕上。
但是我从MSDN等网上看到的唯一样本太旧了(许多对象已被删除,即库经常更新,文章已经过时)
我刚开始使用预览流时出现问题。如果有知识的人能在这件事上给我一些帮助,我将不胜感激。
谢谢。
答案 0 :(得分:1)
我建议使用CaptureElement控件
在你的XAML上添加:
<CaptureElement x:Name="Capture"
Stretch="UniformToFill"
FlowDirection="LeftToRight" />
我不知道您是否要使用前置或后置摄像头,因此我会显示两者的代码。
在你的codeBehind(或你的ViewModel,如果使用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 Phone 8.1和Windows 8.1上运行