In this blog we illustrate the how to open a camera in windows 8.1 phone app.
Code for Xaml file:
<CaptureElement Height="650" Name="captureElement" />
This CaptureElement control is used to render the capture device like camera or webcam.
Code for the Xml.cs file:
public sealed partial class MainPage : Page
{
// Creates a new instance of the MediaCapture object.
MediaCapture captureManager = new MediaCapture();
public MainPage()
{
this.InitializeComponent();
this.NavigationCacheMode = NavigationCacheMode.Required;
// Monitors and controls physical display information.
// The class provides events to allow clients to monitor for changes in the display.
var appView = Windows.UI.ViewManagement.ApplicationView.GetForCurrentView();
//Calling method to open front camera in device
OpenFrontCamera();
}
async private void OpenFrontCamera()
{
MessageDialog msgDialog;
DeviceInformation deviceInformation = await GetFrontCameraInformation(Windows.Devices.Enumeration.Panel.Front);
if (deviceInformation == null)
deviceInformation = (await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture)).FirstOrDefault();
if (deviceInformation == null)
{
//when not supported
msgDialog = new MessageDialog("Using device does not support either web camera or front camera", "Information");
await msgDialog.ShowAsync();
return;
}
await captureManager.InitializeAsync(new MediaCaptureInitializationSettings
{
VideoDeviceId = deviceInformation.Id //Gets the DeviceInformation.Id of the video camera.
});
// Set the camera mode in portrait mode with the help of video preview stream rotation.
// The amount by which to rotate the video.
captureManager.SetPreviewRotation(VideoRotation.Clockwise270Degrees);
//Sets the source MediaCapture to start previewing on CaptureElement represents.
captureElement.Source = captureManager;
// Starts preview.
await captureManager.StartPreviewAsync();
}
private static async Task<DeviceInformation> GetFrontCameraInformation(Windows.Devices.Enumeration.Panel camera)
{
DeviceInformation deviceInformation = (await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture))
.FirstOrDefault(x => x.EnclosureLocation != null && x.EnclosureLocation.Panel == camera); //Find the video capture devices and its id.
return deviceInformation;
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
DisplayInformation.AutoRotationPreferences = DisplayOrientations.Portrait; //Sets the Portrait orientation of the app.
}
}
To set the Device Properties
1. Double click on Package.appxmanifest file.
2. In application tab select the app's orientation.

3. In requirements tab select the hardware requirements of your app.

4. In Capabilties tab select the system features or device that your app can use.

5. Save the changes done in Package.appxmainfest.
Connect your device on which you want to run your application and select the device option and run your application.
0 Comment(s)