在windows 8.1商店应用程序中找不到前后摄像头
本文关键字:找不到 摄像头 应用程序 windows | 更新日期: 2023-09-27 18:26:08
我已经开发了windows 8.1商店应用程序,它需要使用后置摄像头和post来拍摄照片。
MediaCaptureInitializationSettings _captureSettings = new var devices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture); foreach (var device in devices) { if (device.EnclosureLocation != null && device.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Back) { deviceId = device.Id; break; } } if (!string.IsNullOrEmpty(deviceId)) { _captureSettings.AudioDeviceId = ""; _captureSettings.VideoDeviceId = deviceId; _captureSettings.PhotoCaptureSource = Windows.Media.Capture.PhotoCaptureSource.Photo; _captureSettings.StreamingCaptureMode = Windows.Media.Capture.StreamingCaptureMode.Video; } captureManager = new MediaCapture(); await captureManager.InitializeAsync(_captureSettings); await captureManager.ClearEffectsAsync(MediaStreamType.Photo); capturePreview1.Source = captureManager; await captureManager.StartPreviewAsync(); </code> Here i am getting two devices but that devices EnclosureLocation is null, so i can't find which one is front and back camera. so have decided to get second device from list <code> deviceId = devices[1].Id; </code> but it throws an error like "The current capture source does not have an independent photo stream." in the line of initializing MediaCapture <code> await captureManager.InitializeAsync(_captureSettings); </code>我在windowssurfacepro2和acer设备上试过。请告知。提前谢谢。
试着更好地组织代码,因为同一行有两个等号,而且代码格式不好,所以很难阅读。
要在Windows 8.1停止应用程序中使用相机,我使用此代码:
// 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 captureManager = new MediaCapture();
await captureManager.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
capturePreview1.Source = captureManager;
// Start the preview
await captureManager.StartPreviewAsync();
这样更容易阅读。代码差别不大,MediaCaptureInitializationSettings也不一样。
这个代码在Surface 2 RT和诺基亚635上对我有效,所以它应该对你有效。
编辑:
它似乎适用于带有Windows RT的设备,但在完整的Windows 8.1设备上,它总是为空。Msdn说:
如果没有可用的机柜位置信息,则该属性将为空
所以你能做的是首先试着看看你是否找到了一个后台摄像头,如果它是空的,就拿最后一个;
DeviceInformation backWebcam = (from webcam in webcamList
where webcam.EnclosureLocation != null
&& webcam.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Back
select webcam).FirstOrDefault();
if (backWebcam == null)
{
backWebcam = webcamList.Last();
}
但你不确定集合中的最后一个是后面的,所以你应该添加一个按钮,让用户切换相机
如果你更换相机,
await captureManager.StopPreviewAsync();
await captureManager.InitializeAsync(new MediaCaptureInitializationSettings
{
// Choose an other webcam
VideoDeviceId = //id of the new webcam,
AudioDeviceId = "",
StreamingCaptureMode = StreamingCaptureMode.Video,
PhotoCaptureSource = PhotoCaptureSource.VideoPreview
});
await captureManager.StartPreviewAsync();
这样你就可以确保用户可以选择正确的相机,即使你在编程上无法判断哪一个是