×
Namespaces

Variants
Actions

Using the camera light in Windows Phone 7, 8 and 8.1

From Nokia Developer Wiki
Jump to: navigation, search

Using the camera light as a torch is a common use case for smartphones. Windows Phone presents an evolution of APIs supporting this functionality.

WP Metro Icon Multimedia.png
Article Metadata
Article
Created: influencer (10 May 2014)
Last edited: influencer (16 May 2014)

Contents

Introduction

Using the camera light as a torch is not easy in Windows Phone. Each version has different APIs that only works on one OS version. Each version has different requirements regarding app capabilities. This article presents a wrap-up for the different versions of the OS.

Windows Phone 7

In Windows Phone 7 there is an API that's exposed in an undocumented assembly, Microsoft.Phone.Media.Extended.dll. It can be called by using reflection. Its drawback is that this assembly is not available on Windows Phone 8 or later.

Necessary capabilities: ID_CAP_ISV_CAMERA, ID_CAP_MICROPHONE, ID_CAP_MEDIALIB

While we are at the capabilities: there's a catch. You can define the capabilities in the manifest and your app will run fine - at least during debugging. When you prepare your release build and upload it into the store, you might get a surprise when the first users give fatal reviews - the app crashes! What's the reason? The store certification tool checks your app for the necessary capabilities and removes unnecessary capabilities. So your important caps get removed! And what can you do about it? Persuade the store you really need the caps by using another API that needs the same capabilities. Here's the code you can use to do just that:

// Capability detection workarounds
Microsoft.Xna.Framework.Audio.Microphone mic = Microsoft.Xna.Framework.Audio.Microphone.Default;
Microsoft.Xna.Framework.Media.MediaLibrary lib = new Microsoft.Xna.Framework.Media.MediaLibrary();

Place it into the XXXPage.OnNavigatedFrom handler (or somewhere else).

Here's the core code to switch on the camera light:

try
{
// Check to see if the camera is available on the device.
if (PhotoCamera.IsCameraTypeSupported(CameraType.Primary))
{
// Use standard camera on back of device.
_videoCamera = new VideoCamera();
 
// Event is fired when the video camera object has been initialized.
_videoCamera.Initialized += VideoCamera_Initialized;
 
// Add the photo camera to the video source
_videoCameraVisualizer = new VideoCameraVisualizer();
_videoCameraVisualizer.SetSource(_videoCamera);
}
else
ShowWhiteScreenInsteadOfCameraTorch();
}
catch (Exception)
{
// Flashlight isn't supported on this device, instead show a White Screen as the flash light
ShowWhiteScreenInsteadOfCameraTorch();
}

This code is borrowed from Windows Phone Flashlight: Getting started and uses the VideoCamera and VideoCameraVisualizer classes defined there.

Windows Phone 8

On Windows Phone 8 the above API can't be used as the assembly is not available any more. But we have a new API set in Windows.Phone.Media.Capture.

Necessary capabilities: ID_CAP_ISV_CAMERA, ID_CAP_MICROPHONE

The WP8 code is much shorter (remember the supporting classes) and is a big improvement over WP7.

var sensorLocation = CameraSensorLocation.Back;
 
try
{
// get the AudioViceoCaptureDevice
var avDevice = await AudioVideoCaptureDevice.OpenAsync(sensorLocation,
AudioVideoCaptureDevice.GetAvailableCaptureResolutions(sensorLocation).First());
 
// turn flashlight on
var supportedCameraModes = AudioVideoCaptureDevice
.GetSupportedPropertyValues(sensorLocation, KnownCameraAudioVideoProperties.VideoTorchMode);
if (supportedCameraModes.ToList().Contains((UInt32)VideoTorchMode.On))
{
avDevice.SetProperty(KnownCameraAudioVideoProperties.VideoTorchMode, VideoTorchMode.On);
 
// set flash power to maxinum
avDevice.SetProperty(KnownCameraAudioVideoProperties.VideoTorchPower,
AudioVideoCaptureDevice.GetSupportedPropertyRange(sensorLocation, KnownCameraAudioVideoProperties.VideoTorchPower).Max);
}
else
{
ShowWhiteScreenInsteadOfCameraTorch();
}
}
catch (Exception)
{
// Flashlight isn't supported on this device, instead show a White Screen as the flash light
ShowWhiteScreenInsteadOfCameraTorch();
}

Windows Phone 8.1

Windows Phone 8.1 is the first version with a dedicated API for controlling the camera light. This API stems from Windows 8.1 but is usable in Windows Phone 8.1 projects and in Windows Phone Silverlight 8.1 projects.

Necessary capabilities: Webcam

var mediaDev = new MediaCapture();
await mediaDev.InitializeAsync();
var videoDev = mediaDev.VideoDeviceController;
var tc = videoDev.TorchControl;
if (tc.Supported)
{
if (tc.PowerSupported)
tc.PowerPercent = 100;
tc.Enabled = true;
}

Note.pngNote: TorchControl.Supported returns false on most phones in WP8.1 developer preview. It is expected to be fixed by a firmware update by the time WP 8.1 is released. Tested Phones at the time of writing: Lumia 620, 822, 1020: not working, Lumia 1520: working.

Summary

The example of TorchControl shows how the APIs have improved in WP 8.1. While simplifying the classes the capability requirements are reduced.

Version Hint

Windows Phone: [[Category:Windows Phone]]
[[Category:Windows Phone 7.5]]
[[Category:Windows Phone 8]]

Nokia Asha: [[Category:Nokia Asha]]
[[Category:Nokia Asha Platform 1.0]]

Series 40: [[Category:Series 40]]
[[Category:Series 40 1st Edition]] [[Category:Series 40 2nd Edition]]
[[Category:Series 40 3rd Edition (initial release)]] [[Category:Series 40 3rd Edition FP1]] [[Category:Series 40 3rd Edition FP2]]
[[Category:Series 40 5th Edition (initial release)]] [[Category:Series 40 5th Edition FP1]]
[[Category:Series 40 6th Edition (initial release)]] [[Category:Series 40 6th Edition FP1]] [[Category:Series 40 Developer Platform 1.0]] [[Category:Series 40 Developer Platform 1.1]] [[Category:Series 40 Developer Platform 2.0]]

Symbian: [[Category:Symbian]]
[[Category:S60 1st Edition]] [[Category:S60 2nd Edition (initial release)]] [[Category:S60 2nd Edition FP1]] [[Category:S60 2nd Edition FP2]] [[Category:S60 2nd Edition FP3]]
[[Category:S60 3rd Edition (initial release)]] [[Category:S60 3rd Edition FP1]] [[Category:S60 3rd Edition FP2]]
[[Category:S60 5th Edition]]
[[Category:Symbian^3]] [[Category:Symbian Anna]] [[Category:Nokia Belle]]

This page was last modified on 16 May 2014, at 23:56.
285 page views in the last 30 days.
×