VideoBrush就像相机应用程序一样

时间:2014-01-21 21:22:01

标签: c# xaml windows-phone-8

有没有办法,在整个屏幕上创建一个视频投影,就像Windows Phone 8上的默认相机应用程序一样?

1 个答案:

答案 0 :(得分:0)

是。网上有很多关于如何做到这一点的例子。我找到的最好的是this one.我会在这里复制相关部分。实际上,您只需要创建一个画布并挂钩到相机中。链接的文章将为您提供更多信息。您还需要使用您的应用程序权限,以便可以访问摄像头(以及前置摄像头,如果适用)。

创建画布:

<Canvas x:Name="viewfinderCanvas" Width="640" Height="480">
  <Canvas.Background>
    <VideoBrush x:Name="viewfinderBrush">
      <VideoBrush.RelativeTransform>
        <CompositeTransform
          x:Name="viewfinderTransform"
          CenterX="0.5"
          CenterY="0.5"/>
      </VideoBrush.RelativeTransform>
    </VideoBrush>
  </Canvas.Background>
</Canvas>

在你的代码中,设置钩子:

// Using statements
using Microsoft.Devices;
using Microsoft.Xna.Framework.Media;

// Class Variables
PhotoCamera cam;
MediaLibrary library = new MediaLibrary();

// Event hooks
protected override void OnNavigatedTo
  (System.Windows.Navigation.NavigationEventArgs e)
{
  if (PhotoCamera.IsCameraTypeSupported(CameraType.Primary) == true)
  {
    cam = new PhotoCamera(CameraType.Primary);
    cam.CaptureImageAvailable +=
      new EventHandler<Microsoft.Devices.ContentReadyEventArgs>
        (cam_CaptureImageAvailable);
    viewfinderBrush.SetSource(cam);
  }
  else
  {
    txtMessage.Text = "A Camera is not available on this device.";
  }
}
protected override void OnNavigatingFrom
  (System.Windows.Navigation.NavigatingCancelEventArgs e)
{
  if (cam != null)
  {
    cam.Dispose();
  }
}