繁体   English   中英

在Windows Phone 8.1中保存之前旋转图像

[英]Rotate image before saving in Windows Phone 8.1

我的应用程序应保存相机中的图像。 我写了下面的代码:

  1. 初始化相机的方法。

     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(); } 
  2. 保存照片的方法:

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

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

我有两个问题:1。预览不会填满整个空间 在此输入图像描述

  1. 保存后我得到这样的东西:(没有旋转,两侧都有透明层)。
    在此输入图像描述

谢谢

我认为问题可能出在您正在使用的面板中。 因为我在XAML中的CaptureElement中看不到Grid.Row属性, 所以我怀疑你正在使用StackPanel - 它是一个不会伸展到你的屏幕只是堆叠元素的面板。

你应该尝试使用Grid ,因为我已经检查过以下代码应该工作:

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

我可以帮助解决第一个问题 - 更改预览方向。 看起来我们必须在方向更改事件上手动更改它。 代码是用Javascript编写的。 C#应该类似:

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

我没有找到任何简单的方法来更改已保存文件的方向。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM