Namespaces
Variants
Actions
Revision as of 08:42, 5 November 2012 by chintandave_er (Talk | contribs)

How to detect camera availability in Windows Phone

Jump to: navigation, search

This article explains how to detect camera availability in Windows Phone 7 (and also briefly covers Android).

Article Metadata

Code Example
Tested with
SDK: Windows Phone 7.1

Compatibility
Platform(s): Windows Phone 7.5

Article
Created: Vaishali Rawat (19 Oct 2012)
Last edited: chintandave_er (05 Nov 2012)

Introduction

Detecting whether the camera is available on the device or not can be helpful in applications which require to launch camera. If we try to open camera without checking its existence, the app may crash. So, it is always better to determine in advance what kind of cameras (primary, secondary or both) are available on the device.

Detecting camera existence

To detect camera existence in device, we use PhotoCamera API. The PhotoCamera API provides a function IsCameraTypeSupported which accepts a string type variable stating the camera type.

The steps are:

  • First we create an instance of PhotoCamera
private PhotoCamera _Camera;
  • Next, we invoke the DETECT_CAMERA() method
 public void DETECT_CAMERA()
{
if (PhotoCamera.IsCameraTypeSupported(CameraType.FrontFacing) && PhotoCamera.IsCameraTypeSupported(CameraType.Primary))
{
MessageBox.Show("Both Primary and Secondary Camera's are available.");
}
else if (PhotoCamera.IsCameraTypeSupported(CameraType.FrontFacing))
{
MessageBox.Show("Only Secondary Camera is available.");
}
else if (PhotoCamera.IsCameraTypeSupported(CameraType.Primary))
{
MessageBox.Show("Only Primary Camera is available.");
}
else
{
MessageBox.Show("No Camera available on this Device.");
}
}
Note.png
Note: If you're migrating or porting from Android, then the equivalent class to determine whether the phone supports camera or not is PackageManager. PackageManager provides a function hasSystemFeature to determine whether a particular feature is available in the device or not. This function accepts a string type parameter, in this case FEATURE_CAMERA, specifying the feature whose information we need.

The code looks like this:

Boolean isAvailable =  packagemanager.hasSystemFeature(PackageManager.FEATURE_CAMERA);

Downloads

You can download sample project code from this file Media:DetectingCamera.zip.

155 page views in the last 30 days.
Nokia Developer aims to help you create apps and publish them so you can connect with users around the world.

京ICP备05048969号  © Copyright Nokia 2013 All rights reserved