Rotate an image before saving it in Windows Phone 8.1

My application should save the camera image. I wrote the code below:

  • The method that initializes the camera.

    Windows.Media.Capture.MediaCapture takePhotoManager; public async void InitializeCamera() { takePhotoManager = new Windows.Media.Capture.MediaCapture(); await takePhotoManager.InitializeAsync(); takePhotoManager.SetPreviewRotation(VideoRotation.Clockwise90Degrees); PhotoPreview.Source = takePhotoManager; await takePhotoManager.StartPreviewAsync(); // to stop it //await takePhotoManager.StopPreviewAsync(); } 
  • Method that saves the photo:

     public async void SavePhoto() { ImageEncodingProperties imgFormat = ImageEncodingProperties.CreateJpeg(); var file = await KnownFolders.PicturesLibrary.CreateFileAsync("Photo.jpg", CreationCollisionOption.ReplaceExisting); await takePhotoManager.CapturePhotoToStorageFileAsync(imgFormat, file); } 
  • Preview

      <CaptureElement x:Name="PhotoPreview" FlowDirection="LeftToRight" HorizontalAlignment="Center" VerticalAlignment="Center" Stretch="UniformToFill"> </CaptureElement> 

I have two problems: 1. Preview does not fill the whole space enter image description here

  1. After saving, I get something like this: (without rotation and with transparent layers on both sides).
    enter image description here

thanks

+6
source share
2 answers

I think the problem may be in the panel you are using. Since I do not see the Grid.Row property in CaptureElement in XAML, I suspect that you are using a StackPanel - this is a panel that will not stretch on your screen, it just stacks the elements.

What you are trying to do should be possible with a Grid, as I have verified that the following code should work:

 <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <StackPanel Orientation="Vertical" Grid.Row="0"> <Button Click="InitCameraBtn_Click" Content="Initialize Camera" /> <Button Click="StartPreviewBtn_Click" Content="Start Capture Preview" /> <Button Click="StopPreviewBtn_Click" Content="Stop Capture Preview" /> </StackPanel> <CaptureElement x:Name="PhotoPreview" Stretch="UniformToFill" Grid.Row="1" VerticalAlignment="Center" HorizontalAlignment="Center"/> </Grid> 
0
source

I can help with the first problem - change the orientation of the preview. It looks like we have to manually change it to an orientation change event. The code is in Javascript. C # should look like:

 capture = new CaptureNS.MediaCapture(); //additional capture settings and init function onOrientationChanged(e) { var o = e.orientation; //portrait if (o == 0 ) { capture && capture.setPreviewRotation(Windows.Media.Capture.VideoRotation.clockwise90Degrees); //landscape } else if (o == 1) { capture && capture.setPreviewRotation(Windows.Media.Capture.VideoRotation.none); } else if (o == 2) { capture && capture.setPreviewRotation(Windows.Media.Capture.VideoRotation.clockwise270Degrees); } else if (o == 3) { capture && capture.setPreviewRotation(Windows.Media.Capture.VideoRotation.clockwise180Degrees); } } orientationSensor = Windows.Devices.Sensors.SimpleOrientationSensor.getDefault(); if (orientationSensor) { orientationSensor.onorientationchanged = onOrientationChanged; } 

I did not find an easy way to change the orientation of the saved file.

-1
source

Source: https://habr.com/ru/post/976714/


All Articles