How to create a base camera app for Windows Phone 8

How to create a base camera app for Windows Phone 8

[ This article is for Windows Phone eight developers. If you’re developing for Windows Ten, see the latest documentation. ]

Beginning with Windows Phone OS 7.1, you can programmatically access the phone’s camera using the Microsoft.Devices . PhotoCamera class. This topic is the very first in a series that describes how to create a camera app that demonstrates using this API for capturing photos. This topic demonstrates how to display the camera viewfinder, programmatically trigger the shutter, and save a captured pic to the media library and the local folder.

Commencing with Windows Phone 8, apps can use the PhotoCaptureDevice class for advanced capture screenplays. PhotoCaptureDevice permits apps to control photo properties such as ISO, exposure compensation, and manual concentrate position (when available on the phone). This topic doesn’t demonstrate PhotoCaptureDevice ; for more info, see Advanced photo capture for Windows Phone eight .

If photo capture is not a core feature of your app, consider using the Camera Capture Task to capture photos instead of using the camera APIs directly. Memory allocated for the Camera Capture Task does not count toward total app memory use. This helps minimize your app’s memory use, which is particularly significant when your app runs on a lower-memory device. For more information, see Developing apps for lower-memory phones for Windows Phone eight and How to use the camera capture task for Windows Phone eight .

This topic covers the following steps:

This topic corresponds to the Basic Camera Sample.

After you accomplish this base camera app, the following topics incorporate features such as implementing flash and concentrate, switching capture resolution, and using the phone hardware shutter button. You must accomplish the base camera app in this topic very first before proceeding. The following topics build upon this app and add functionality to the features implemented in this topic.

After completing all of these topics, the finished solution will show up as shown in the following photo.

In this topic, only the SH button is added to the app. The flash, autofocus, and resolution buttons are added in the other topics in the series.

When upgrading Windows Phone OS 7.0 apps to use the capabilities in Windows Phone OS 7.1, the camera capability ID_CAP_ISV_CAMERA is not automatically added to the app manifest file, WMAppManifest.xml . Without ID_CAP_ISV_CAMERA , apps using the camera API will not function. In fresh Windows Phone OS 7.1 projects, this capability is included in the app manifest file.

This topic can’t be finished with a Windows Phone OS 7.1 emulator ( Emulator 7.Five ). Develop this app with a phone or a Windows Phone eight emulator.

In this section, you create the camera UI that consists of a viewfinder region to display captured frames, and a shutter button to capture an photo.

To create the camera UI

Using the Windows Phone SDK, create a fresh project using the Windows Phone App template.

After the project has been created, on the Project menu, select Add Reference . On the .NET tab, choose Microsoft.XNA.Framework , and then click OK .

In the MainPage.xaml file, update the phone:PhoneApplicationPage element as shown in the following code.

This configures the page for landscape orientation and hides the system tray.

In MainPage.xaml , substitute the Grid named LayoutRoot with the following code.

This code creates a 640×480 viewfinder region with a StackPanel control that contains the shutter button, SH . You implement the ShutterButton_Click event in the following sections.

The software shutter button in this exercise is used only for demonstrating programmatic access to the PhotoCamera API. To optimize the practice for the user, we recommend that your app uses the hardware shutter button on the camera. For info about how to implement the hardware shutter button, see How to access the hardware camera shutter button in Windows Phone eight .

Open MainPage.xaml.cs , the code-behind file for the main page, and add the following directives at the top of the page.

In MainPage.xaml.cs , in the MainPage class, add the following variable declarations before the MainPage class constructor.

To create a camera app, the camera capability must be proclaimed in the app manifest file. Without it, the app won’t function. Open WMAppManifest.xml and confirm that the following capabilities element is present.

If your Windows Phone eight app requires a back-facing camera, use the ID_REQ_BACKCAMERA hardware requirement to prevent users from downloading your app if they don’t have a back-facing camera. For more info about capabilities and requirements, see App capabilities and hardware requirements for Windows Phone eight .

To access the media library, the media library capability must be proclaimed in the app manifest file. Without this, the app won’t be able to save files to the media library. Depending on the version of Windows Phone that your app targets, add the following capability:

ID_CAP_MEDIALIB : Windows Phone OS 7.1 apps can use this capability to access the media library.

ID_CAP_MEDIALIB_PHOTO : Windows Phone eight apps can use this capability to save photos to the media library.

Windows Phone eight apps need to use extra capabilities to access movie and audio in the media library. For more info, see App capabilities and hardware requirements for Windows Phone eight .

(Optional) If features of your app require a front-facing camera, also add the front-facing camera capability or hardware requirement to the app manifest file, depending on which Windows Phone version your app targets:

ID_HW_FRONTCAMERA : Windows Phone OS 7.1 apps can use this capability. With this capability, a user without a front-facing camera will be notified that some features of your app won’t work on their phone, but they can still choose to download it.

ID_REQ_FRONTCAMERA : Windows Phone eight apps can use this hardware requirement. With this requirement, a user without a front-facing camera won’t be able to download your app.

To implement the viewfinder, you set the viewfinderBrush source to the Windows Phone camera. You also implement several camera-based events such as camera initialization, capture ended, and pic availability.

To implement viewfinder and camera-based events

In MainPage.xaml.cs , add the following code to the MainPage class.

Until you finish the following steps of this procedure, Visual Studio may list errors about methods that do not exist in the current context. These methods will be added in the following steps.

This code uses the OnNavigatedTo(NavigationEventArgs) method to create a PhotoCamera object named cam and configure several events. This code also sets the VideoBrush source to the phone camera object, cam .

If the phone has a front-facing camera in addition to the primary camera, the app will use the front-facing camera. Not all phones have a front-facing camera, so the IsCameraTypeSupported(CameraType) method is used to check if it is available before creating the PhotoCamera object. It is also possible that the phone does not have any type of camera. In that case, the UI is disabled and a message is displayed.

To account for the Windows Phone execution model, the PhotoCamera object is initialized in the OnNavigatedTo(NavigationEventArgs) method and explicitly disposed during the OnNavigatingFrom(NavigatingCancelEventArgs) method. The event handlers for PhotoCamera events are added in OnNavigatedTo(NavigationEventArgs) and eliminated in OnNavigatingFrom(NavigatingCancelEventArgs) to help release memory.

For info about how to analyze and process individual frames from the camera preview buffer, see How to work with grayscale in a camera app for Windows Phone eight .

How to create a base camera app for Windows Phone eight

How to create a base camera app for Windows Phone 8

[ This article is for Windows Phone eight developers. If you’re developing for Windows Ten, see the latest documentation. ]

Beginning with Windows Phone OS 7.1, you can programmatically access the phone’s camera using the Microsoft.Devices . PhotoCamera class. This topic is the very first in a series that describes how to create a camera app that demonstrates using this API for capturing photos. This topic demonstrates how to display the camera viewfinder, programmatically trigger the shutter, and save a captured picture to the media library and the local folder.

Embarking with Windows Phone 8, apps can use the PhotoCaptureDevice class for advanced capture screenplays. PhotoCaptureDevice permits apps to control photo properties such as ISO, exposure compensation, and manual concentrate position (when available on the phone). This topic doesn’t demonstrate PhotoCaptureDevice ; for more info, see Advanced photo capture for Windows Phone eight .

If photo capture is not a core feature of your app, consider using the Camera Capture Task to capture photos instead of using the camera APIs directly. Memory allocated for the Camera Capture Task does not count toward total app memory use. This helps minimize your app’s memory use, which is particularly significant when your app runs on a lower-memory device. For more information, see Developing apps for lower-memory phones for Windows Phone eight and How to use the camera capture task for Windows Phone eight .

This topic covers the following steps:

This topic corresponds to the Basic Camera Sample.

After you finish this base camera app, the following topics incorporate features such as implementing flash and concentrate, switching capture resolution, and using the phone hardware shutter button. You must finish the base camera app in this topic very first before proceeding. The following topics build upon this app and add functionality to the features implemented in this topic.

After completing all of these topics, the finished solution will emerge as shown in the following photo.

In this topic, only the SH button is added to the app. The flash, autofocus, and resolution buttons are added in the other topics in the series.

When upgrading Windows Phone OS 7.0 apps to use the capabilities in Windows Phone OS 7.1, the camera capability ID_CAP_ISV_CAMERA is not automatically added to the app manifest file, WMAppManifest.xml . Without ID_CAP_ISV_CAMERA , apps using the camera API will not function. In fresh Windows Phone OS 7.1 projects, this capability is included in the app manifest file.

This topic can’t be finished with a Windows Phone OS 7.1 emulator ( Emulator 7.Five ). Develop this app with a phone or a Windows Phone eight emulator.

In this section, you create the camera UI that consists of a viewfinder region to display captured frames, and a shutter button to capture an picture.

To create the camera UI

Using the Windows Phone SDK, create a fresh project using the Windows Phone App template.

After the project has been created, on the Project menu, select Add Reference . On the .NET tab, choose Microsoft.XNA.Framework , and then click OK .

In the MainPage.xaml file, update the phone:PhoneApplicationPage element as shown in the following code.

This configures the page for landscape orientation and hides the system tray.

In MainPage.xaml , substitute the Grid named LayoutRoot with the following code.

This code creates a 640×480 viewfinder region with a StackPanel control that contains the shutter button, SH . You implement the ShutterButton_Click event in the following sections.

The software shutter button in this exercise is used only for demonstrating programmatic access to the PhotoCamera API. To optimize the practice for the user, we recommend that your app uses the hardware shutter button on the camera. For info about how to implement the hardware shutter button, see How to access the hardware camera shutter button in Windows Phone eight .

Open MainPage.xaml.cs , the code-behind file for the main page, and add the following directives at the top of the page.

In MainPage.xaml.cs , in the MainPage class, add the following variable declarations before the MainPage class constructor.

To create a camera app, the camera capability must be announced in the app manifest file. Without it, the app won’t function. Open WMAppManifest.xml and confirm that the following capabilities element is present.

If your Windows Phone eight app requires a back-facing camera, use the ID_REQ_BACKCAMERA hardware requirement to prevent users from downloading your app if they don’t have a back-facing camera. For more info about capabilities and requirements, see App capabilities and hardware requirements for Windows Phone eight .

To access the media library, the media library capability must be proclaimed in the app manifest file. Without this, the app won’t be able to save files to the media library. Depending on the version of Windows Phone that your app targets, add the following capability:

ID_CAP_MEDIALIB : Windows Phone OS 7.1 apps can use this capability to access the media library.

ID_CAP_MEDIALIB_PHOTO : Windows Phone eight apps can use this capability to save photos to the media library.

Windows Phone eight apps need to use extra capabilities to access movie and audio in the media library. For more info, see App capabilities and hardware requirements for Windows Phone eight .

(Optional) If features of your app require a front-facing camera, also add the front-facing camera capability or hardware requirement to the app manifest file, depending on which Windows Phone version your app targets:

ID_HW_FRONTCAMERA : Windows Phone OS 7.1 apps can use this capability. With this capability, a user without a front-facing camera will be notified that some features of your app won’t work on their phone, but they can still choose to download it.

ID_REQ_FRONTCAMERA : Windows Phone eight apps can use this hardware requirement. With this requirement, a user without a front-facing camera won’t be able to download your app.

To implement the viewfinder, you set the viewfinderBrush source to the Windows Phone camera. You also implement several camera-based events such as camera initialization, capture finished, and picture availability.

To implement viewfinder and camera-based events

In MainPage.xaml.cs , add the following code to the MainPage class.

Until you accomplish the following steps of this procedure, Visual Studio may list errors about methods that do not exist in the current context. These methods will be added in the following steps.

This code uses the OnNavigatedTo(NavigationEventArgs) method to create a PhotoCamera object named cam and configure several events. This code also sets the VideoBrush source to the phone camera object, cam .

If the phone has a front-facing camera in addition to the primary camera, the app will use the front-facing camera. Not all phones have a front-facing camera, so the IsCameraTypeSupported(CameraType) method is used to check if it is available before creating the PhotoCamera object. It is also possible that the phone does not have any type of camera. In that case, the UI is disabled and a message is displayed.

To account for the Windows Phone execution model, the PhotoCamera object is initialized in the OnNavigatedTo(NavigationEventArgs) method and explicitly disposed during the OnNavigatingFrom(NavigatingCancelEventArgs) method. The event handlers for PhotoCamera events are added in OnNavigatedTo(NavigationEventArgs) and eliminated in OnNavigatingFrom(NavigatingCancelEventArgs) to help release memory.

For info about how to analyze and process individual frames from the camera preview buffer, see How to work with grayscale in a camera app for Windows Phone eight .

Related video:

Leave a Reply