![](/img/trans.png)
[英]Trying to use back camera followed by front camera c# mediacapture windows phone 8.1
[英]Cannot find Front, Back Camera in windows 8.1 store app
我已經開發了Windows 8.1商店應用程序,它需要通過使用后置攝像頭和帖子來捕獲照片。
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>
我試過windows surface pro 2和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和Nokia 635,因此它適用於您。
編輯:
似乎它適用於使用Windows RT的設備,但在完整的Windows 8.1設備上,它始終為空。 Msdn說:
如果沒有可用的機箱位置信息,則該屬性將為null
所以你能做的就是首先嘗試看看你是否找到了backwebcam,如果它是null,那就拿最后一個;
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();
通過這種方式你可以確定用戶可以選擇合適的相機,即使你是programmaticaly無法分辨哪一個是哪個
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.