在 Windows Phone 8.1 上访问相机预览流 [英] Access camera preview-stream on Windows Phone 8.1
问题描述
我正在尝试创建一个基本的相机应用程序(对于我的第一个针对 WP 的应用程序).当然,我想在拍摄前将相机数据预览到屏幕上.
I'm trying to create a basic camera application (for my first application targeted towards WP). And I want to, of course, preview the camera data to the screen before taking a shot.
但我从 MSDN 等在线看到的唯一示例太旧(许多对象已被删除,即库经常更新,使文章过时)
But the only samples I see online from MSDN etc are too old (many objects have been removed, ie the libraries are being updated frequently making the articles obsolete)
我刚开始使用预览流时遇到问题.如果有知识的人能在这方面给我一些帮助,我将不胜感激.
I'm having a problem just getting started with the preview-stream. It would be greatly appreciated if someone with knowledge could give me some help in this matter.
谢谢.
推荐答案
我建议使用 CaptureElement 控件
I suggest using the CaptureElement control
在您的 XAML 上添加:
On your XAML add this:
<CaptureElement x:Name="Capture"
Stretch="UniformToFill"
FlowDirection="LeftToRight" />
我不知道您是要使用前置网络摄像头还是后置网络摄像头,所以我将展示两者的代码.
I don't know if you want to use front or back webcam so I'll show code for both.
在您的代码隐藏(或您的 ViewModel,如果您使用 MVVM)中添加以下代码:
In your codeBehind (or your ViewModel if your using MVVM) add this code:
// 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
This will show the stream of the chosen webcam in your CaptureElement control, and works on both Windows Phone 8.1 and Windows 8.1
这篇关于在 Windows Phone 8.1 上访问相机预览流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!