Access camera preview-stream on Windows Phone 8.1

老子叫甜甜 提交于 2019-12-11 10:45:25

问题


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.

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.

Thank you.


回答1:


I suggest using the CaptureElement control

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.

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();

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



来源:https://stackoverflow.com/questions/26225009/access-camera-preview-stream-on-windows-phone-8-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!