如何获得可访问的相机分辨率?

时间:2015-01-18 11:38:48

标签: silverlight camera windows-runtime windows-phone-8.1 resolution

我想问一下,如何在Windows Phone 8.1应用程序(适用于Silverlight和WinRT)中获得所有可访问的相机分辨率。我想用:

Windows.Phone.Media.Capture.PhotoCaptureDevice.GetAvailableCaptureResolutions(
     Windows.Phone.Media.Capture.CameraSensorLocation.Back);

但是我收到的消息是命名空间Windows.Phone.Media.Capture已经过时,从Windows Phone Blue开始的下一版Windows Phone可能不支持,而我应该使用Windows.Media.Capture。但是Windows.Media.Capture不允许我获取可访问的摄像头分辨率,所以我想问一下,如何解决这个问题。

谢谢。

1 个答案:

答案 0 :(得分:3)

可以这样做:

首先让我们定义获取用于拍照的设备ID 的方法:

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

    if (deviceID != null) return deviceID;
    else throw new Exception(string.Format("Camera of type {0} doesn't exist.", desiredCamera));
}

然后我们初始化相机后 - 我们可以读取这样的分辨率:

private async void InitCameraBtn_Click(object sender, RoutedEventArgs e)
{
    var cameraID = await GetCameraID(Windows.Devices.Enumeration.Panel.Back);
    captureManager = new MediaCapture();

    await captureManager.InitializeAsync(new MediaCaptureInitializationSettings
        {
            StreamingCaptureMode = StreamingCaptureMode.Video,
            PhotoCaptureSource = PhotoCaptureSource.VideoPreview,
            AudioDeviceId = string.Empty,
            VideoDeviceId = cameraID.Id
        });
    // Get resolutions
    var resolutions = captureManager.VideoDeviceController.GetAvailableMediaStreamProperties(MediaStreamType.Photo).Select(x => x as VideoEncodingProperties).ToList();
    // get width and height:
    uint width = resolutions[0].Width;
    uint height = resolutions[0].Height;
}