How to detect camera availability in Windows Phone
This article explains how to port the camera detection code from Android to Windows Phone 7.
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 , chances are there that the app may crash. So, its always better to determine in advance what kind of camera (primary, secondary or both) are available on the device.
Detecting camera existence in Android
In Android, we use PackageManager class to determine whether the phone supports camera or not. The PackageManager class provides a function hasSystemFeature to determine whether a particular feature is available in the device or not. This function accepts a string type parameter stating about the feature whose information we need. To determine camera existence, the parameter string we'll use is FEATURE_CAMERA.
The code to detect camera existence in Android:
Boolean isAvailable = packagemanager.hasSystemFeature(PackageManager.FEATURE_CAMERA);
Detecting camera existence in Windows Phone 7
To detect camera existence in device, we use PhotoCamera API. As in Android, the PhotoCamera API provides a function IsCameraTypeSupported which accepts a string type variable stating the camera type.
The code to detect camera existence in Windows Phone 7:
- 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.");
MessageBox.Show("No Camera available on this Device.");
Now we have successfully ported the code of camera detection from Android to Windows Phone 7.