2015-11-03 22 views
1

我在C#和VS2013中爲Lumia 640 XL WP 8.1編寫了一個簡單的代碼,就像一個示例照片應用程序。不錯,但它有一個小問題:當圖片保存到媒體中時,則此圖片向左旋轉90度。如何在WP8.1中旋轉CaptureManager保存的照片

這裏是我的設置預覽區碼:

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); 

但它沒有奏效。

在這種情況下我該做什麼?

感謝您的幫助,併爲我的純英語感到抱歉。

回答

0

你應該看看CameraStarterKit SDK sample。它會告訴你怎麼做的旋轉:

前瞻:

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

但是你應該看看全樣本查看方向/旋轉信息來自何處,並獲得幫助器功能。