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

时间:2014-10-14 11:44:50

标签: c# rotation windows-runtime 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>
    
  4. 我有两个问题: 1.预览不会填满整个空间 enter image description here

    1. 保存后我会得到这样的结果:(没有旋转,两侧都有透明层) enter image description here
    2. 由于

2 个答案:

答案 0 :(得分:0)

我认为问题可能出在您正在使用的面板中。由于我无法在XAML中的 CaptureElement 中看到 Grid.Row 属性,因此我怀疑您使用的是 StackPanel - 它&#39 ; sa面板不会伸展到你的屏幕,只是堆叠元素。

您应该尝试使用 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>

答案 1 :(得分:-1)

我可以帮助解决第一个问题 - 更改预览方向。看起来我们必须在方向更改事件上手动更改它。 代码是用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;    
}

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