如何在Windows Phone 8.1(WinRT / Jupiter)中切换到前置摄像头

时间:2014-08-23 20:29:07

标签: winrt-xaml

我似乎无法找到MediaCapture类的属性,它允许我检测前置摄像头并切换到它(如果可用)。这是我当前设备的设置,它在Windows(前凸轮)和电话(后凸轮)上按预期工作。没有一个Microsoft示例显示前置摄像头在Universal或WP 8.1(WinRT / Jupiter)中使用。

mediaCaptureManager = new MediaCapture();
            await mediaCaptureManager.InitializeAsync();

            if (mediaCaptureManager.MediaCaptureSettings.VideoDeviceId != "" && mediaCaptureManager.MediaCaptureSettings.AudioDeviceId != "")
            {
                StartStopRecordingButton.IsEnabled = true;
                TakePhotoButton.IsEnabled = true;

                ShowStatusMessage("device initialized successfully!");
                mediaCaptureManager.VideoDeviceController.PrimaryUse = CaptureUse.Video;
                mediaCaptureManager.SetPreviewRotation(VideoRotation.Clockwise90Degrees);
                mediaCaptureManager.SetRecordRotation(VideoRotation.Clockwise90Degrees);
                mediaCaptureManager.RecordLimitationExceeded += RecordLimitationExceeded;
                mediaCaptureManager.Failed += Failed;
            }

2 个答案:

答案 0 :(得分:1)

以下是如何获取设备的可用摄像头并为流设置前置摄像头:

                mediaCaptureManager = new MediaCapture();
            var devices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);
            var deviceInfo = devices[0]; //grab first result

            foreach (var device in devices)
            {
                if (device.Name.ToLowerInvariant().Contains("front"))
                {
                    deviceInfo = frontCamera = device;
                    hasFrontCamera = true;
                }
                if (device.Name.ToLowerInvariant().Contains("back"))
                {
                    rearCamera = device;
                }
            }

            var mediaSettings = new MediaCaptureInitializationSettings
            {
                MediaCategory = MediaCategory.Communications,
                StreamingCaptureMode = StreamingCaptureMode.AudioAndVideo,
                VideoDeviceId = deviceInfo.Id
            };

            await mediaCaptureManager.InitializeAsync(mediaSettings);

您需要考虑旋转,因为不同设备上的前置和后置摄像头具有不同的旋转,但这会正确初始化您的MediaCapture

答案 1 :(得分:1)

Microsoft github页面上有一个相关的示例,尽管它们的目标是Windows 10.但是,API应该适用于8 / 8.1。

UniversalCameraSample:此照片会捕捉照片,并支持纵向和横向方向。以下是相关部分:

private static async Task<DeviceInformation> FindCameraDeviceByPanelAsync(Windows.Devices.Enumeration.Panel desiredPanel)
{
    // Get available devices for capturing pictures
    var allVideoDevices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);

    // Get the desired camera by panel
    DeviceInformation desiredDevice = allVideoDevices.FirstOrDefault(x => x.EnclosureLocation != null && x.EnclosureLocation.Panel == desiredPanel);

    // If there is no device mounted on the desired panel, return the first device found
    return desiredDevice ?? allVideoDevices.FirstOrDefault();
}

你可以像这样使用它:

// Attempt to get the front camera if one is available, but use any camera device if not
var cameraDevice = await FindCameraDeviceByPanelAsync(Windows.Devices.Enumeration.Panel.Front);

if (cameraDevice == null)
{
    Debug.WriteLine("No camera device found!");
    return;
}

// Create MediaCapture and its settings
_mediaCapture = new MediaCapture();

var settings = new MediaCaptureInitializationSettings { VideoDeviceId = cameraDevice.Id };

// Initialize MediaCapture
try
{
    await _mediaCapture.InitializeAsync(settings);
    _isInitialized = true;
}
catch (UnauthorizedAccessException)
{
    Debug.WriteLine("The app was denied access to the camera");
}
catch (Exception ex)
{
    Debug.WriteLine("Exception when initializing MediaCapture with {0}: {1}", cameraDevice.Id, ex.ToString());
}

仔细查看示例,了解如何获取所有详细信息。或者,要进行演练,您可以观看最近//版本/会议中的camera session,其中包括一些相机示例的演练。