我正在使用PhotoCamera类来填充VideoBrush并在应用程序中拍照。默认情况下,我将相机设置为正面。但是,我希望能够在两者都存在时在主摄像头和前置摄像头之间切换。我为此设置了一个按钮,但我不确定如何正确实现它。我到目前为止的内容如下
MainPage.xaml.cs中
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
//Set up the Camera
if (null == camera)
{
InitializeCamera();
}
}
private void InitializeCamera()
{
// Check to see if the camera is available on the device.
if ((PhotoCamera.IsCameraTypeSupported(CameraType.Primary) == true) || (PhotoCamera.IsCameraTypeSupported(CameraType.FrontFacing) == true))
{
// Initialize the camera, when available.
if (PhotoCamera.IsCameraTypeSupported(CameraType.FrontFacing))
{
// Use front-facing camera if available.
camera = new Microsoft.Devices.PhotoCamera(CameraType.FrontFacing);
}
else
{
// Otherwise, use standard camera on back of device.
camera = new Microsoft.Devices.PhotoCamera(CameraType.Primary);
}
camera.Initialized += camera_Initialized;
//Event is fired when the button is half pressed
CameraButtons.ShutterKeyHalfPressed += camera_ButtonHalfPress;
//Event is fired when the button is fully pressed
CameraButtons.ShutterKeyPressed += camera_ButtonFullPress;
//Event is fired when the capture sequence is complete and an image is available.
camera.CaptureImageAvailable += camera_CaptureImageAvailable;
camera.CaptureCompleted += camera_CaptureCompleted;
//Set the VideoBrush source to the camera
videoBrush.SetSource(camera);
}
}
private void UninitializeCamera()
{
camera.Dispose();
CameraButtons.ShutterKeyPressed -= camera_ButtonFullPress;
CameraButtons.ShutterKeyHalfPressed -= camera_ButtonHalfPress;
camera.CaptureImageAvailable -= camera_CaptureImageAvailable;
camera = null;
}
void sensor_Click(object sender, EventArgs e)
{
if (null != camera)
{
//Determine what cameras exist, and switch if able to
}
}
我的主要问题是考虑到我现有的设置,完成此任务的最佳方法是什么?有更好的选择吗?