【问题标题】:Rotate image before saving in Windows Phone 8.1在 Windows Phone 8.1 中保存之前旋转图像
【发布时间】:2014-12-09 04:48:45
【问题描述】:

我的应用应该保存来自相机的图像。我写了下面的代码:

  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. 保存后我得到这样的结果:(没有旋转,两边都有透明层)。

谢谢

【问题讨论】:

  • 也许回答this question 会有所帮助。您还可以找到一些信息here at this post。
  • 谢谢,它解决了我与透明空间相关的问题。不幸的是,保存过程后旋转图像仍然存在问题,并在预览中填充所有空间。
  • 您是否尝试过:HorizontalAlignment="Strech" VerticalAlignment="Strech" of CaptureElement in XAML?
  • 不幸的是它没有任何改变。
  • 你解决了轮播问题吗?

标签: c# rotation windows-runtime windows-phone-8.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>

【讨论】:

  • 谢谢罗马兹。是的,我使用了 StackPanel。您的解决方案是正确的,但是如果您可以在链接link 中看到,相机图像附近仍然存在黑色空间。
  • @user3149172 这很可能是因为在初始化时,您选择了宽度最大的分辨率(在我的手机上为 3552x2000)。事实上,还有其他分辨率,例如 3264x2448 可能更合适。在调试模式下运行:var resols = captureManager.VideoDeviceController.GetAvailableMediaStreamProperties(MediaStreamType.Photo).ToList();,看看你有什么分辨率。您还可以考虑根据自己的目的更改代码 var maxResolution = captureManager.VideoDeviceController.GetAvailableMedi...。
  • 我认为这是我的观点造成的。我的 maxResolution 没问题,但是如果我在 CaptureElement 上方和下方添加一些控件,它会被缩放,并且图像的左侧和右侧都有黑色空间。但我不知道如何解决问题...
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多