我的视频画笔在手机上显示照片捕获设备时遇到了一些方向问题。它实际上应该像内置相机应用程序一样灵活,这意味着它应该适用于
- 所有纵横比
- 两个摄像头(后置和前置)
- 和所有页面方向
其中至少有一个总是错误的。我试过 https://projects.developer.nokia.com/cameraexplorer让它工作,但即使它有最好的方法,它在不同的页面方向上对我不起作用,而且前置摄像头旋转方向错误(当我顺时针旋转手机时逆时针旋转,所以我颠倒了)。
是否有任何带有完整工作相机画笔的代码片段?
最佳答案
要正确显示取景器,您需要两个信息:
- orientation : 预览图片方向相对于你的页面方向
- Scale:预览图片大小和 xaml 控制之间的因素。
首先你需要一个 Canvas 作为背景
<Canvas x:Name="viewfinderCanvas" Width="480" Height="800" >
<Canvas.Background>
<VideoBrush x:Name="viewfinderBrush" Stretch="None" />
</Canvas.Background>
</Canvas>
您必须使用 Stretch="None" 否则 XAML 将在 View 画笔上应用缩放。现在您需要 viewfinderBrush 转换才能正确显示它。 默认情况下, Canvas 中心对应于预览图片的中心,所以我们需要计算一个角度,一个比例因子,将 Canvas 中心作为变换中心。
要计算您需要的角度:
- 相对于设备纵向方向的传感器方向。 该值由 PhotoCaptureDevice.SensorRotationInDegrees属性(property)。
- 您的页面方向相对于设备纵向方向。
代码:
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;
}
}
比例只是 Canvas 尺寸和预览图片尺寸之间的因数:
//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;
- 如果你使用最大因子,你做一个 Fit out => scale = 数学.Max(s1, s2)
- 如果使用最小因子,则适合 => scale = 数学.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
关于c# - 用于照片捕获设备的视频刷的旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17246469/