Windows Phone 7 Sensors and XNA
Camera Task: XNA Camera Demo
Back to main page

Release 1.0 (XNA V4.0)

1. Topics Covered

This program demonstrates XNA support for invoking and obtaining images from the Camera on the phone, and from the "Photo Chooser Task". You may wish to download the source code, open the project and refer to the source code while follow the discussion. Topics covered in this tutorial includes:

Pre-requisite: this tutorial assumes you have read through the Pinch Zoom Touch Panel Tutorial, in particular,

2. Demo Program Behavior


Note: at the time of writing (Sep 15th 2010), this program has only been tested on the WP7 emulator.

You will see screens similar to the following when you compile the attached source code, dispatch to WP7 device/emulator, and run the program.

Portrait Image
Tap the screen to bring up the Photo Choose program as follow:
Portrait Image
Select any image/photograph, and the control will return to the tutorial with the selected image as the new background. As follows:
Portrait Image

3. Solution Structure and External Environment

As discussed, the solution structure of this tutorial is similar to the solution structure of the Pinch Zoom Touch Panel tutorial, where SimpleLibrary is referenced for the image and font output. The following are the main differences:

4. Implementation Details:

4a. CameraDemo.cs: (User Interaction Support)

An Image Image
The above listing shows a structure similar to that from the structure of the Pinch Zoom Touch panel tutorial. The main difference is in the instance variables support (at Label E) for the Camera and Photo Chooser Tasks:
  1. PhotoChooserTask and CameraCaptureTask: conceptually, thses are references to threads that we can invoke at run time.
  2. mLoadCount: is a simple count of how many times we have invoked the camera or photo chooser tasks.
  3. mTaskStarted: this boolean indicates a task has already been started and ensures we do not attempt to start the same task more than once based on the same user action.
The Initialize() function:
An Image Image
The above listing shows the initialization of the Tasks are performed in the Initialize() function:
The Update() Function:
An Image Image
The above listing shows that, with the tasks properly initialized, during update, we must check for users action (Label U1), move the little airplane (Label U2), and detect the quit condition (Label U3). The following listings focus on the code under each of labels U1 and U2.

4b. CameraDemo_Compute.cs: Event service support

An Image Image
This file implements the event service routine when the invoked tasks have completed. The following explains each function in this file according to their order in the above listing.
An Image Image
Simple construct a message (for Draw() to display) displaying the status of the photo retrieval. If successful, use the image name (file name) to create a Texture2D object to be used as the background image.
An Image Image
This is the actual routine that services the "Camera Task Completed" event. Once again, this routine is invoked asynchronously from a different thread so all it does is create a thread-safe way of calling RetrivePhoto().
An Image Image
Same as above, this is the event service routine for the "Photo Chooser Task Completed" event .

5. Self Test and Exercises


Kelvin Sung
Computing and Software Systems
University of Washington, Bothell
Project home page: The Game-Themed Introductory Programming Project.

Microsoft Logo This work is supported in part by Microsoft Research under the Computer Gaming Curriculum in Computer Science RFP, Award Number 15871 and 16531, and Microsoft Higher Education.