Lessons Learnt Using the Windows Phone Camera API

I’ve been working with the Windows Phone camera API (PhotoCaptureDevice) a lot recently for Foundbite, some of the things I’ve learnt:

Not every Windows Phone has flash

I didn’t realise this until recently when testing with the cheap as chips Nokia Lumia 520. It’s a great device for the price but you need to add a check to your camera code to make sure other flash modes are available before manipulating it.

Here’s a method to get the available flash states and another to check if flash is available for the current device (ie: if there is one or less flash states).

public bool IsFlashAvailable()
{
List<FlashState> flashStates = GetAvailableFlashStates().ToList();
return (flashStates.Count > 1);
}

public IReadOnlyList<FlashState> GetAvailableFlashStates()
{
IReadOnlyList<object> rawValueList = PhotoCaptureDevice.GetSupportedPropertyValues(Device.SensorLocation, KnownCameraPhotoProperties.FlashMode);
List<FlashState> flashStates = new List<FlashState>(rawValueList.Count);foreach (object rawValue in rawValueList) flashStates.Add((FlashState)(uint)rawValue);
return flashStates.AsReadOnly();
}

Update #1: Jay Benett ponts out that this can be done in a far simpler way than I have using LINQ’s .Any() https://twitter.com/JayTBennett/status/370221811423670272

Update #2: Just realised this update 1 method won’t work as even if there isn’t a flash on the device there will still be FlashState.Off in the list.

Update #3:After more discussion, it turns out there is a way to do it using Jay’s method: https://twitter.com/scottisafool/status/371913405080547328. I’m going to stop updating this post now.

In some regions the camera shutter sound can’t be turned off

Foundbite allows you to take a picture at the same time as recording audio so it wouldn’t be ideal if you could hear the shutter sound in every sound clip. It is possible to turn the sound off but in some regions of the world it’s a legal necessity to have this sound on.

It’s therefore a necessity to check that you can turn the sound off before doing so:

private void disableShutterSound()
{
if (!(bool)this.Device.GetProperty(KnownCameraGeneralProperties.IsShutterSoundRequiredForRegion))
this.Device.SetProperty(KnownCameraGeneralProperties.PlayShutterSoundOnCapture, false);
}

It’s possible to turn off the flash that happens when you focus the camera

This setting is called the FocusIlluminationMode and can easily be changed, much like FlashMode, between Auto, On and Off.

Here’s the code to get the current value and change it:

public FocusIlluminationMode GetCurrentFocusIlluminatonMode()
{
return (FocusIlluminationMode)(uint)Device.GetProperty(KnownCameraPhotoProperties.FocusIlluminationMode);
}

public void SetIlluminationMode(FocusIlluminationMode mode)
{
Device.SetProperty(KnownCameraPhotoProperties.FocusIlluminationMode, mode);
}

It’s worth nothing that you should check the flash is available before changing this mode too!
Hopefully these will be helpful and save some time for others playing around with the camera.

About these ads

3 thoughts on “Lessons Learnt Using the Windows Phone Camera API

  1. Pingback: Lessons Learnt Using the Windows Phone Camera API

  2. Pingback: Windows Store Developer Links – 2013-08-27 | Dan Rigby

  3. Pingback: Lessons Learnt Using the Windows Phone Camera API : Part 2 | Foundbite, Programming and the Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s