65.9K
CodeProject is changing. Read more.
Home

Hacking the WP7 Camera (The basics)

starIconstarIconstarIconstarIconstarIcon

5.00/5 (2 votes)

Jan 28, 2011

CPOL
viewsIcon

19141

Before you start, you need to get the Microsoft.Phone.Media.Extended.dll and GAC it.

Before you start, you need to get the Microsoft.Phone.Media.Extended.dll and GAC it. For more information on this, read this and this!

Let’s Start

Create a new Windows Phone application, add the Microsoft.Phone.Media.Extended.dll reference and add the ID_CAP_CAMERA capability to the WMAppManifest.xml.

<?xml version="1.0" encoding="utf-8"?>

<Deployment xmlns="http://schemas.microsoft.com/windowsphone/2009/deployment" 
    AppPlatformVersion="7.0">
  <App xmlns="" ProductID="{56a0189a-5739-410b-a400-d70214739c88}" 
      Title="WindowsPhoneApplication1" RuntimeType="Silverlight" 
      Version="1.0.0.0" Genre="apps.normal"  
      Author="WindowsPhoneApplication1 author" Description="Sample description" 
      Publisher="WindowsPhoneApplication1">
    <IconPath IsRelative="true" 
        IsResource="false">ApplicationIcon.png</IconPath>
    <Capabilities>
      <Capability Name="ID_CAP_CAMERA" />
      <Capability Name="ID_CAP_GAMERSERVICES"/>
      <Capability Name="ID_CAP_IDENTITY_DEVICE"/>
      <Capability Name="ID_CAP_IDENTITY_USER"/>
      <Capability Name="ID_CAP_LOCATION"/>
      <Capability Name="ID_CAP_MEDIALIB"/>
      <Capability Name="ID_CAP_MICROPHONE"/>
      <Capability Name="ID_CAP_NETWORKING"/>
      <Capability Name="ID_CAP_PHONEDIALER"/>
      <Capability Name="ID_CAP_PUSH_NOTIFICATION"/>
      <Capability Name="ID_CAP_SENSORS"/>
      <Capability Name="ID_CAP_WEBBROWSERCOMPONENT"/>
    </Capabilities>
    <Tasks>
      <DefaultTask  Name ="_default" NavigationPage="MainPage.xaml"/>
    </Tasks>
    <Tokens>
      <PrimaryToken TokenID="WindowsPhoneApplication1Token" TaskName="_default">
        <TemplateType5>
          <BackgroundImageURI IsRelative="true" 
              IsResource="false">Background.png</BackgroundImageURI>
          <Count>0</Count>
          <Title>WindowsPhoneApplication1</Title>
        </TemplateType5>
      </PrimaryToken>
    </Tokens>
  </App>
</Deployment>

In our main window, we need to add a CameraVisualizer

<phone:PhoneApplicationPage 
    x:Class="WindowsPhoneApplication1.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:media="clr-namespace:Microsoft.Phone;assembly=Microsoft.Phone.Media.Extended"
    mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    shell:SystemTray.IsVisible="True">

    <Grid x:Name="LayoutRoot" Background="Transparent">
        <media:CameraVisualizer x:Name="cameraVisualizer" />
    </Grid>
    
</phone:PhoneApplicationPage>

This is used to show the actual images captured from the camera. Next, setup the camera

VideoCamera camera = new VideoCamera();
cameraVisualizer.SetSource(camera);

And that is it!

NOTE: Do not run this in the emulator... It will crash!!!

 Is their any practical uses for this? Sure, Kevin Marshall (from Clarity consulting) has a excellent demo of this being used for reading barcodes and for augmented reality!

This is a MUST READ article....

Hacking the WP7 Camera (The basics) - CodeProject