如何在WP8.1中旋转captureManager保存的照片

本文关键字:captureManager 保存 照片 旋转 WP8 | 更新日期: 2023-09-27 18:20:19

我用C#和VS2013为Lumia 640 XL WP 8.1编写了一个简单的代码,就像一个示例照片应用程序。还不错,但它有一个小问题:当图片保存到媒体上时,这张图片会向左旋转90度。

这是我设置预览区域的代码:

captureManager = new MediaCapture();
await captureManager.InitializeAsync();
captureManager.SetPreviewRotation(VideoRotation.Clockwise90Degrees);
captureManager.SetRecordRotation(VideoRotation.Clockwise90Degrees);
cptElement.Source = captureManager;
await captureManager.StartPreviewAsync();

这是捕获的代码:

ImageEncodingProperties imgFormat = ImageEncodingProperties.CreateJpeg();
StorageFile file = await KnownFolders.PicturesLibrary.CreateFileAsync("Photo.jpg", CreationCollisionOption.GenerateUniqueName);
await captureManager.CapturePhotoToStorageFileAsync(imgFormat, file);

所以,你可以看到,我试图在这个命令行中旋转最后一张图片:

captureManager.SetRecordRotation(VideoRotation.Clockwise90Degrees);

但没有奏效。

这种情况下我该怎么办?

谢谢你的帮助,对我纯正的英语感到抱歉。

如何在WP8.1中旋转captureManager保存的照片

您应该查看CameraStarterKit SDK示例。它将向您展示如何旋转:

预览:

/// <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>
/// Takes a photo to a StorageFile and adds rotation metadata to it
/// </summary>
/// <returns></returns>
private async Task TakePhotoAsync()
{
    // While taking a photo, keep the video button enabled only if the camera supports simultaneously taking pictures and recording video
    VideoButton.IsEnabled = _mediaCapture.MediaCaptureSettings.ConcurrentRecordAndPhotoSupported;
    // Make the button invisible if it's disabled, so it's obvious it cannot be interacted with
    VideoButton.Opacity = VideoButton.IsEnabled ? 1 : 0;
    var stream = new InMemoryRandomAccessStream();
    try
    {
        Debug.WriteLine("Taking photo...");
        await _mediaCapture.CapturePhotoToStreamAsync(ImageEncodingProperties.CreateJpeg(), stream);
        Debug.WriteLine("Photo taken!");
        var photoOrientation = ConvertOrientationToPhotoOrientation(GetCameraOrientation());
        await ReencodeAndSavePhotoAsync(stream, photoOrientation);
    }
    catch (Exception ex)
    {
        // File I/O errors are reported as exceptions
        Debug.WriteLine("Exception when taking a photo: {0}", ex.ToString());
    }
}

和视频:

/// <summary>
/// Records an MP4 video to a StorageFile and adds rotation metadata to it
/// </summary>
/// <returns></returns>
private async Task StartRecordingAsync()
{
    try
    {
        // Create storage file in Pictures Library
        var videoFile = await KnownFolders.PicturesLibrary.CreateFileAsync("SimpleVideo.mp4", CreationCollisionOption.GenerateUniqueName);
        var encodingProfile = MediaEncodingProfile.CreateMp4(VideoEncodingQuality.Auto);
        // Calculate rotation angle, taking mirroring into account if necessary
        var rotationAngle = 360 - ConvertDeviceOrientationToDegrees(GetCameraOrientation());
        encodingProfile.Video.Properties.Add(RotationKey, PropertyValue.CreateInt32(rotationAngle));
        Debug.WriteLine("Starting recording...");
        await _mediaCapture.StartRecordToStorageFileAsync(encodingProfile, videoFile);
        _isRecording = true;
        Debug.WriteLine("Started recording!");
    }
    catch (Exception ex)
    {
        // File I/O errors are reported as exceptions
        Debug.WriteLine("Exception when starting video recording: {0}", ex.ToString());
    }
}

但是,您应该查看完整的示例,以了解方向/旋转信息的来源,并获取辅助函数。