我正在创建一个 Windows 通用应用程序。我希望用户能够上传图片,并且用户应该可以选择当场拍摄并发送。我使用 MediaCapture api 进行了这项工作。但是我似乎只能使用一个摄像头,例如,如果我的手机有一个前置摄像头和一个后置摄像头,则只使用前置摄像头。我如何才能切换正在使用的相机?
我在某处读到过一些关于使用这样的东西:
private static async Task<DeviceInformation> 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 通用应用程序: 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);
这应该可以解决您的问题。
关于c# - Windows(手机)8.1 相机使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26137683/