【问题标题】:rotation of videobrush for photocapturedevicephotocapturedevice的视频画笔旋转
【发布时间】:2013-06-22 01:27:19
【问题描述】:

我的视频画笔在手机上显示 photocapturedevice 时的方向出现了一些问题。它实际上应该和内置相机应用程序一样灵活,这意味着它应该适用于

  • 所有纵横比
  • 两个摄像头(后置和前置)
  • 和所有页面方向

其中至少有一个总是错误的。我试过https://projects.developer.nokia.com/cameraexplorer 让它工作,但即使它有最好的方法,它在不同的页面方向上对我不起作用,前置摄像头旋转错误的方式(当我顺时针旋转手机时逆时针,所以我是颠倒的)。

是否有任何带有完整工作相机视频刷的代码-sn-p?

【问题讨论】:

    标签: c# xaml camera windows-phone-8


    【解决方案1】:

    要正确显示取景器,您需要两个信息:

    • orientation:预览图片相对于页面方向的方向
    • 比例:预览图片大小和 xaml 控件之间的因素。

    首先,您需要一个以视频画笔为背景的画布

    <Canvas x:Name="viewfinderCanvas" Width="480" Height="800" >
        <Canvas.Background>
            <VideoBrush x:Name="viewfinderBrush"  Stretch="None" />
        </Canvas.Background>
    </Canvas>
    

    您必须使用 Stretch="None" 否则 XAML 将在视图画笔上应用缩放。现在您需要 viewfinderBrush 转换才能正确显示它。 默认情况下,画布中心对应于预览图片中心,所以我们需要计算一个角度,一个比例因子,并将画布中心作为变换中心。

    计算你需要的角度:

    代码:

    double ComputeAngle(PageOrientation orientation)
    {
        if ((orientation & PageOrientation.Portrait) == PageOrientation.Portrait)
        {
            return  m_captureDevice.SensorRotationInDegrees;
        }
        else if ((orientation & PageOrientation.LandscapeLeft) == PageOrientation.LandscapeLeft)
        {
            return m_captureDevice.SensorRotationInDegrees - 90;
        }
        else //PageOrientation.LandscapeRight
        {
            return m_captureDevice.SensorRotationInDegrees + 90;
        }
    }
    

    比例只是画布尺寸和预览图片尺寸之间的因素:

    //orient preview picture size from the computed anle.
    var tmp = new CompositeTransform(){Rotation = ComputeAngle(currentPageOrientation)};
    var previewSize = tmp.TransformBounds (new Rect(new Point(), new Size(m_captureDevice.PreviewResolution.Width, m_captureDevice.PreviewResolution.Height))).Size;
    double s1 = viewfinderCanvas.Width/ (double)previewSize.Width;
    double s2 = viewfinderCanvas.Height/ (double)previewSize.Height;
    
    • 如果使用最大因子,则进行拟合 => 比例 = 数学.Max(s1, s2)
    • 如果您使用最小因子,您将进行适合 => 比例 = 数学.Min(s1, s2)

    前后摄像头的视线方向相反。因此,要正确显示前置摄像头,您需要在一维中应用一面镜子。在 WP8 上,传感器方向通常为 90°,因此 Y 尺寸相反。

    if (sensorLocation == CameraSensorLocation.Back)
    {
        viewfinderBrush.Transform = new CompositeTransform() { 
                            Rotation = ComputeAngle(currentPageOrientation), 
                            CenterX = viewfinderCanvas.Width / 2, 
                            CenterY = viewfinderCanvas.Height / 2, 
                            ScaleX = scale, 
                            ScaleY = scale };
    }
    else
    {
        viewfinderBrush.Transform = new CompositeTransform() { 
                            Rotation = ComputeAngle(currentPageOrientation), 
                            CenterX = viewfinderCanvas.Width / 2, 
                            CenterY = viewfinderCanvas.Height / 2, 
                            ScaleX =  scale, 
                            ScaleY = -1 * scale };//Y mirror 
    }
    

    您可以在 github 上找到该示例的最新版本:https://github.com/yan-verdavaine/wp8-sample/tree/master/Imaging/ViewFinder

    【讨论】:

    • 在回答原始问题的代码上投票 -1 很奇怪:/。如果我做错了什么,你能解释一下吗?
    • 在 SO 上不首选仅链接答案,因为它们指向的内容可能会更改或被删除,请尝试将示例代码添加到您的答案中。
    • @yan skydrive 链接不再工作。也许你可以重新上传?谢谢!
    【解决方案2】:

    这个问题很老,但无论如何,使用前置摄像头时,网络上没有关于错误方向的答案。要解决这个问题,需要改变 VideBrush X 轴的方向。这是关于如何做到这一点的代码sn-p:

    if (cameraType == CameraType.Primary)
                {
                    viewfinderBrush.RelativeTransform =
                        new CompositeTransform() { CenterX = 0.5, CenterY = 0.5, Rotation = 90 };
                }
                else
                {
                    viewfinderBrush.RelativeTransform =
                                           new CompositeTransform() { CenterX = 0.5, CenterY = 0.5, Rotation = 90, ScaleX = -1 };                    
                } 
    

    【讨论】:

      【解决方案3】:

      你能解决这个问题吗?当使用前置摄像头时,我发现了同样的行为。

      我在一个矩形(它承载了 videobrush 元素)上应用了旋转变换(角度 = 180°),并且它起作用了。我还更改了我的 AudioVideoCaptureDevice 对象上的 EncodeWithOrientation 属性的值,并且录制的电影也可以。此解决方案仅适用于纵向模式,但是当您更改回横向时,一切都会出错,因此这种技术并不能真正解决问题。也许这是 SDK 中的错误?

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-06
      • 1970-01-01
      • 1970-01-01
      • 2018-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-20
      相关资源
      最近更新 更多