Windows Phone 8.1:如何改变前置摄像头旋转

本文关键字:改变 旋转 摄像头 Phone 何改变 Windows | 更新日期: 2023-09-27 18:14:56

var cameraID = await GetCameraID(Windows.Devices.Enumeration.Panel.Front);
mediaCapture = new MediaCapture();
await mediaCapture.InitializeAsync(new MediaCaptureInitializationSettings
{
    StreamingCaptureMode = StreamingCaptureMode.Video,
    PhotoCaptureSource = PhotoCaptureSource.VideoPreview,
    AudioDeviceId = string.Empty,
    VideoDeviceId = cameraID.Id
});
当我设置 时
capturePreview.Source = mediaCapture;

预览是颠倒的。

如果我设置方向

mediaCapture.SetPreviewRotation(VideoRotation.Clockwise180Degrees);

我看到一个镜像。例如,我可以看到自己左手放在右边。

如果我尝试

mediaCapture.SetPreviewMirroring(true);

程序崩溃。我需要改变这些错误行为

Windows Phone 8.1:如何改变前置摄像头旋转

如果您查看来自Microsoft GitHub存储库的CameraStarterKit示例,您将对如何处理相机旋转有更好的了解。

主要归结为:

    // Receive notifications about rotation of the device and UI and apply any necessary rotation to the preview stream and UI controls       
    private readonly DisplayInformation _displayInformation = DisplayInformation.GetForCurrentView();
    private readonly SimpleOrientationSensor _orientationSensor = SimpleOrientationSensor.GetDefault();
    private SimpleOrientation _deviceOrientation = SimpleOrientation.NotRotated;
    private DisplayOrientations _displayOrientation = DisplayOrientations.Portrait;
    // Rotation metadata to apply to the preview stream and recorded videos (MF_MT_VIDEO_ROTATION)
    // Reference: http://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh868174.aspx
    private static readonly Guid RotationKey = new Guid("C380465D-2271-428C-9B83-ECEA3B4A85C1");
    /// <summary>
    /// Gets the current orientation of the UI in relation to the device (when AutoRotationPreferences cannot be honored) and applies a corrective rotation to the preview
    /// </summary>
    private async Task SetPreviewRotationAsync()
    {
        // Only need to update the orientation if the camera is mounted on the device
        if (_externalCamera) return;
        // Calculate which way and how far to rotate the preview
        int rotationDegrees = ConvertDisplayOrientationToDegrees(_displayOrientation);
        // The rotation direction needs to be inverted if the preview is being mirrored
        if (_mirroringPreview)
        {
            rotationDegrees = (360 - rotationDegrees) % 360;
        }
        // Add rotation metadata to the preview stream to make sure the aspect ratio / dimensions match when rendering and getting preview frames
        var props = _mediaCapture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview);
        props.Properties.Add(RotationKey, rotationDegrees);
        await _mediaCapture.SetEncodingPropertiesAsync(MediaStreamType.VideoPreview, props, null);
    }
    /// <summary>
    /// Registers event handlers for hardware buttons and orientation sensors, and performs an initial update of the UI rotation
    /// </summary>
    private void RegisterEventHandlers()
    {
        // If there is an orientation sensor present on the device, register for notifications
        if (_orientationSensor != null)
        {
            _orientationSensor.OrientationChanged += OrientationSensor_OrientationChanged;
            // Update orientation of buttons with the current orientation
            UpdateButtonOrientation();
        }
        _displayInformation.OrientationChanged += DisplayInformation_OrientationChanged;
    }

但这只是代码的一部分。您应该查看完整的文件(如果不是完整的示例),以便更好地理解它是如何工作的。