Extending the Windows Phone 8 Camera App with Lenses
This tutorial explains how to support the new lens feature in the Windows Phone 8 Camera App.
Article Metadata
Code Example
Tested with
Compatibility
Article
Contents |
Introduction
The Camera app in Windows Phone 8 allows you to extend the app with lenses. Creating a “lens” in Windows Phone 8 will make it easier for your users to modify or share an image with your app directly from the Camera app. For instance, the Microsoft Research Face Touch app (which allows users to modify the facial expressions in photos) could create a lens so that users can quickly and more efficiently modify their photo.
First Things First
Required Icons
Lens apps require extra icons for the lens picker. You must add three different icons in your Assets folder: Lens.Screen-WVGA.png should be a 173x173 pixel image, Lens.Screen-720p.png should be a 259x259 pixel image, and Lens.Screen-WXGA.png should be a 277x277 pixel image.
Registering as a Lens
You’ll also need to register your app as lens-enabled. You can do this by editing the WMAppManifest.xml file located in the Properties folder of your app. Add the following lines to the XML file after the closing </token> tag (via MSDN):
<Extensions>
<Extension ExtensionName="Camera_Capture_App"
ConsumerID="{5B04B775-356B-4AA0-AAF8-6491FFEA5631}"
TaskID="_default" />
</Extensions>
Handling a Lens Request
When your app is launched as lens, it will launch the default page unless you specify otherwise. If you’d like to have a separate page for your lens viewfinder, create a new page which does contain a viewfinder. After you have a viewfinder, you will need to forward the app to that page if the app is opened from the Camera app. Open your App.xaml.cs file, then add the following class to it (via MSDN):
class LensExampleUriMapper : UriMapperBase
{
private string tempUri;
public override Uri MapUri(Uri uri)
{
tempUri = uri.ToString();
// Look for a URI from the lens picker.
if (tempUri.Contains("ViewfinderLaunch"))
{
// Launch as a lens, launch viewfinder screen.
return new Uri("/viewfinderExperience.xaml", UriKind.Relative);
}
// Otherwise perform normal launch.
return uri;
}
}
Finally, add the following to your InitializePhoneApplication function (you might need to show the "Phone application initialization" region) in App.xaml.cs:
//Assign the lens example URI-mapper class to the application frame.
RootFrame.UriMapper = new LensExampleUriMapper();
The above example will send your app to the viewfinderExperience.xaml page if it’s being launched as a lens, and to your app’s default start page otherwise.
Developing the Viewfinder Page
Getting the MediaViewer
The easiest way to go about getting the viewfinder page set up would be to download Microsoft's MediaView control and use it in your app. Download the Basic Lens Sample on MSDN, and then import the "MediaView" project into your own application. Build the solution (Build -> Build solution), and add a reference in your own project (right-click references in the Solution Explorer and click Add) to MediaView, then go back to (or create) your viewfinderExperience.xaml page and add the following line to the <phone:PhoneApplicationPage> element:
xmlns:controls="clr-namespace:Microsoft.Phone.Controls;assembly=MediaViewer"
Frontend/XAML
At the very least, you must add a<controls:MediaViewer>Items="{Binding CameraRoll}"Backend/C#
You should download the code from the sdkBasicLensWP8CS project (again, part of the Basic Lens Sample download on MSDN), and then copy the ViewModels folder into your project (changing sdkBasicLensWP8CS to the name of your project). You can then modify the code in the MainPage.xaml.cs file found in the sdkBasicLensWP8CS project of the Basic Lens Sample download on MSDN to fit your needs.
How to Test Your Lens
The Windows Phone 8 Emulator has a functioning camera which will display a multi-coloured block moving around the screen. While this is satisfactory to ensure that you've set up the lens correctly, it's highly recommended that you test your app on an actual Windows Phone 8 device before completion.
Credit
Most of the code samples on this page were taken from the Basic Lens Sample on MSDN and Lens Extensibility on MSDN.


Contents
Hamishwillee - Subedited + a few notes
Hi matthewthepc
You write well, and I particularly like that you've put references to your source materials, which is something that people sometimes miss. This would have benefited from more images.
I've just made some minor edits to for "wiki style". FYI, as a rule:
My main concern with this article (irrespective of the competition) is that the content on the wiki we already have covers similar ground - What's_new_in_Windows_Phone_8#Camera:_Lenses_apps (its OK for that not to be exhaustive because its part of a bigger "overview" article". ) As a rule we try not to duplicate content here or on MSDN. We will never compete with the excellent Microsoft content, so we aim very much to complement it with great examples and real world use cases. Sometime their documentation misses "tips and tricks", covers only a few use cases, or sometimes the good documentation on a topic is spread over a lot of articles. Any of these cases we can "value add".
As a competition entry we're looking for (copied from the competition page): Innovative uses of new features, Real world use cases, Comprehensive tutorials. I don't think this article quite fits any of these yet.
As I say, you write well and I'd certainly love to see what else you can come up with. Thank you!
Regards
Hamishhamishwillee 06:05, 8 November 2012 (EET)
Matthewthepc - Agreed
Hamishwille,
I'd have to agree, I was somewhat confused as to what type of article to submit. I'm currently writing a more comprehensive tutorial which will combine multiple new APIs (including the Nokia Music API) that I think will fit in more with your expectations.
Thanks,
Matthew
Hamishwillee - Thanks Matthew
There is no doubt that picking a topic is not easy! The Windows_Phone_8_Wiki_Competition_2012Q4#Stuck_for_ideas? section is supposed to help a bit at least in explaining what we mean by innovative, real world and comprehensive. I guess in terms of camera lens topics a comprehensive one would be one that covers the use of the API, some examples, tips and tricks and gotchas - but since the camera lens docs are pretty good I'm not sure needed. I think there must be lots of innovative combinations like you have suggested - suggesting music based on what is displayed on the current lens? :-) ?
Great to hear you're continuing!hamishwillee 06:31, 14 November 2012 (EET)
Matthewthepc - :D
I think there's been a bit of a misunderstanding - by "writing a more comprehensive tutorial which will combine multiple new APIs (including the Nokia Music API)" I was referring to my latest submission - "Creating a voice-controlled WP8 Music Information app with the Nokia Music API" http://www.developer.nokia.com/Community/Wiki/Creating_a_Voice-Controlled_Windows_Phone_8_Music_Information_app_with_the_Nokia_Music_API
If you'd like to delete this page, I don't think it adds that much to the "What's new in WP8" page you linked to before.
By the way, the link to Ideas Project in the WP8 Wiki Competition site is broken, you have an extra "/" at the end of the URLmatthewthepc 07:07, 14 November 2012 (EET)
Hamishwillee - No misunderstanding
Hi Matthew
I was talking "generally". I know about your other article but I haven't got to it yet.
>If you'd like to delete this page, I don't think it adds that much to the "What's new in WP8" page you linked to before.
Thanks. I'll think about it. Maybe someone else will see the comments and decide on some way to extend this.
>By the way, the link to Ideas Project in the WP8 Wiki Competition site is broken, you have an extra "/" at the end of the URL
Thanks, I will fix it now.
Regards
Hamishhamishwillee 05:20, 15 November 2012 (EET)