ビューファインダーを正しく表示するには、次の 2 つの情報が必要です。
- orientation : ページの向きに対するプレビュー画像の向き
- Scale : プレビュー画像のサイズと 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;
- 最大係数を使用する場合は、Fit out => scale = Math.Max(s1, s2) を作成します。
- 最小係数を使用する場合は、Fit In => scale = Math.Min(s1, s2) を作成します。
前面カメラと背面カメラの視線方向は反対です。そのため、フロント カメラを正しく表示するには、ミラーを 1 次元で適用する必要があります。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