Windows(手机)8.1相机使用

我正在创建一个Windows Universal应用程序。 我希望用户能够上传图片,用户应该可以选择当场拍摄并发送。 我使用MediaCapture api工作。 但是我似乎只能使用一台摄像机,例如,如果我的手机有前置摄像头和后置摄像头,则仅使用前置摄像头。 我怎样才能切换正在使用的相机?

我曾经读过一些关于使用这样的东西的东西:

private static async Task GetCameraID(Windows.Devices.Enumeration.Panel desired) { DeviceInformation deviceID = (await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture)) .FirstOrDefault(x => x.EnclosureLocation != null && x.EnclosureLocation.Panel == desired); return deviceID; } 

但是这总是为我返回null,因为deviceID始终为null。

或者可以选择控制应用程序拍摄照片并将拍摄的照片返回给我的应用程序吗? 我找到了以下内容,但它不适用于Windows Universal应用程序: http : //msdn.microsoft.com/en-us/library/windows/apps/hh394006(v = vs.105).aspx

我是这样做的:

首先是初始化部分

 // First need to find all webcams DeviceInformationCollection webcamList = await DeviceInformation.FindAllAsync(DeviceClass.All) // 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 newCapture = new MediaCapture(); await newCapture.InitializeAsync(new MediaCaptureInitializationSettings { // Choose the webcam you want VideoDeviceId = backWebcam.Id, AudioDeviceId = "", StreamingCaptureMode = StreamingCaptureMode.Video, PhotoCaptureSource = PhotoCaptureSource.VideoPreview }); // Set the source of the CaptureElement to your MediaCapture // (In my XAML I called the CaptureElement *Capture*) Capture.Source = newCapture; // Start the preview await newCapture.StartPreviewAsync(); 

其次拍照

 //Set the path of the picture you are going to take StorageFolder folder = ApplicationData.Current.LocalFolder; var picPath = "\\Pictures\\newPic.jpg"; StorageFile captureFile = await folder.CreateFileAsync(picPath, CreationCollisionOption.GenerateUniqueName); ImageEncodingProperties imageProperties = ImageEncodingProperties.CreateJpeg(); //Capture your picture into the given storage file await newCapture.CapturePhotoToStorageFileAsync(imageProperties, captureFile); 

这应该可以解决你的问题。