The advantage of the PhotoCamera
class is that it gives control over the flash and autofocus when capturing images.
PhotoCamera
is usually defined as a field in a page or viewmodel and then instantiated when needed. The device camera needs time to ready itself for use and cannot be used until it is initialized, indicated by an Initialized
event. Initialization commences when the PhotoCamera
is assigned to the source of a VideoBrush
, using its SetSource
method.
Note
Failure to call VideoBrush.SetSource
with the PhotoCamera
instance prevents the Initialized
event from being raised.
The following example shows how to create and initialize a PhotoCamera
using a VideoBrush
within a PhoneApplicationPage
:
PhotoCamera photoCamera;
protected override void OnNavigatedTo(NavigationEventArgs e)
{
photoCamera = new PhotoCamera();
photoCamera.Initialized += HandlePhotoCameraInitialized;
videoBrush_Preview.SetSource(photoCamera);
base.OnNavigatedTo(e);
}
The Initialized
event allows the configuration of the camera. This may include setting the image resolution for captured images, setting the flash mode of the camera, and allowing subscription to the various PhotoCamera
events, as demonstrated in the following excerpt:
void HandlePhotoCameraInitialized(
object sender, CameraOperationCompletedEventArgs e)
{
PhotoCamera camera = (PhotoCamera)sender;
camera.Resolution = camera.AvailableResolutions.ElementAt(0);
camera.AutoFocusCompleted += HandleAutoFocusCompleted;
camera.CaptureCompleted += HandleCaptureCompleted;
camera.CaptureThumbnailAvailable += HandleCaptureThumbnailAvailable;
camera.CaptureImageAvailable += HandleCaptureImageAvailable;
if (camera.IsFlashModeSupported(FlashMode.Auto))
{
FlashMode = FlashMode.Auto;
}
}
Each of the events and the PhotoCamera
properties are discussed in greater detail in the following section.
3.147.69.50