// </SnippetHelperOrientationChanged> // <SnippetCapturePhotoWithOrientation> private async Task CapturePhotoWithOrientationAsync() { var captureStream = new InMemoryRandomAccessStream(); try { await mediaCapture.CapturePhotoToStreamAsync(ImageEncodingProperties.CreateJpeg(), captureStream); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("Exception when taking a photo: {0}", ex.ToString()); return; } var decoder = await BitmapDecoder.CreateAsync(captureStream); var file = await KnownFolders.PicturesLibrary.CreateFileAsync("SimplePhoto.jpeg", CreationCollisionOption.GenerateUniqueName); using (var outputStream = await file.OpenAsync(FileAccessMode.ReadWrite)) { var encoder = await BitmapEncoder.CreateForTranscodingAsync(outputStream, decoder); var photoOrientation = CameraRotationHelper.ConvertSimpleOrientationToPhotoOrientation( _rotationHelper.GetCameraCaptureOrientation()); var properties = new BitmapPropertySet { { "System.Photo.Orientation", new BitmapTypedValue(photoOrientation, PropertyType.UInt16) } }; await encoder.BitmapProperties.SetPropertiesAsync(properties); await encoder.FlushAsync(); } }
// </SnippetDeclareRotationHelper> private void InitRotationHelper() { // <SnippetInitRotationHelper> _rotationHelper = new CameraRotationHelper(_cameraDevice.EnclosureLocation); _rotationHelper.OrientationChanged += RotationHelper_OrientationChanged; // </SnippetInitRotationHelper> }
//<SnippetSetPreviewRotationAsync> private async Task SetPreviewRotationAsync() { if (!_externalCamera) { // Add rotation metadata to the preview stream to make sure the aspect ratio / dimensions match when rendering and getting preview frames var rotation = _rotationHelper.GetCameraPreviewOrientation(); var props = mediaCapture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview); Guid RotationKey = new Guid("C380465D-2271-428C-9B83-ECEA3B4A85C1"); props.Properties.Add(RotationKey, CameraRotationHelper.ConvertSimpleOrientationToClockwiseDegrees(rotation)); await mediaCapture.SetEncodingPropertiesAsync(MediaStreamType.VideoPreview, props, null); } }
//</SnippetSetPreviewRotationAsync> //<SnippetHelperOrientationChanged> private async void RotationHelper_OrientationChanged(object sender, bool updatePreview) { if (updatePreview) { await SetPreviewRotationAsync(); } await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { // Rotate the buttons in the UI to match the rotation of the device var angle = CameraRotationHelper.ConvertSimpleOrientationToClockwiseDegrees(_rotationHelper.GetUIOrientation()); var transform = new RotateTransform { Angle = angle }; // The RenderTransform is safe to use (i.e. it won't cause layout issues) in this case, because these buttons have a 1:1 aspect ratio CapturePhotoButton.RenderTransform = transform; CapturePhotoButton.RenderTransform = transform; }); }
//</SnippetCapturePhotoWithOrientation> //<SnippetStartRecordingWithOrientationAsync> private async Task StartRecordingWithOrientationAsync() { try { var videoFile = await KnownFolders.VideosLibrary.CreateFileAsync("SimpleVideo.mp4", CreationCollisionOption.GenerateUniqueName); var encodingProfile = MediaEncodingProfile.CreateMp4(VideoEncodingQuality.Auto); var rotationAngle = CameraRotationHelper.ConvertSimpleOrientationToClockwiseDegrees( _rotationHelper.GetCameraCaptureOrientation()); Guid RotationKey = new Guid("C380465D-2271-428C-9B83-ECEA3B4A85C1"); encodingProfile.Video.Properties.Add(RotationKey, PropertyValue.CreateInt32(rotationAngle)); await mediaCapture.StartRecordToStorageFileAsync(encodingProfile, videoFile); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("Exception when starting video recording: {0}", ex.ToString()); } }