Windows phone 8.1 旋转时相机预览黑边

Windows phone 8.1 camera preview black edge on rotation

我在 windows phone 8.1 上使用视频预览来拍照。没有旋转,预览没有黑边。旋转 90 度后,捕获元素上出现条纹。

这是我的屏幕截图和 XAML,c# 代码。

XAML

CaptureElement x:Name="capturePreview" Stretch="UniformToFill" Margin="27,158,10,10" Grid.ColumnSpan="2" />

C#

    public async void preview()
    {
        DeviceInformationCollection webcamList = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);

        DeviceInformation backWebcam = (from webcam in webcamList
                                        where webcam.EnclosureLocation != null
                                        && webcam.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Back
                                        select webcam).FirstOrDefault();
         newCapture = new MediaCapture();          
        await newCapture.InitializeAsync(new MediaCaptureInitializationSettings
        {
            VideoDeviceId = backWebcam.Id,
            AudioDeviceId = "",
            StreamingCaptureMode = StreamingCaptureMode.Video,
            PhotoCaptureSource = PhotoCaptureSource.VideoPreview
        });                 
        await newCapture.StartPreviewAsync();
        newCapture.SetPreviewRotation(VideoRotation.Clockwise90Degrees);
    }

旋转图片

不旋转

不要使用 mediaCapture.SetPreviewRotation,因为它会给您的流添加信箱。相反,使用 Camera Starter Kit UWP sample from the Microsoft GitHub repository 中的 SetPreviewRotationAsync 方法。如果您仔细观察它,您将更好地了解如何处理相机的旋转。

主要归结为:

    // 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(int rotationDegrees)
    {
        // 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);
    }

但这只是代码的一部分。您应该查看 the full file(如果不是完整示例)以更好地了解其工作原理。