Camera

0
205

All Windows Phone 7 physical devices include a high resolution camera. This is managed by the operating system, but your applications can invoke the camera using the CameraCaptureTask chooser.

To use the camera, you must do the following:

  • Add a reference to the assembly Microsoft.Phone.Tasks to your project.
  • Create a new CameraCaptureTask chooser instance as a global scoped variable (the application will be deactivated when the chooser is launched).
  • Specify a callback in the form of an event handler, or a lambda expression that will receive the picture when the user closes the chooser.
  • Show the chooser.

The camera is designed to be used in landscape mode, and it generates a picture in landscape format. The Exchangeable Image File (EXIF) headers in the image file indicate the correct orientation of the picture, and the photos application in the camera will automatically display the image in the correct orientation. However, in your own applications, you may need to rotate the image to obtain the correct orientation.

A library that you can use to read EXIF information in an image is available from “Understanding and Reading Exif Data” on The Code Project website (http://www.codeproject.com/KB/silverlight/Exif_Data.aspx). Details of how to rotate an image can be found in post, “Handling picture orientation in CameraCaptureTask in Windows Phone 7,” on Tim Heuer’s blog (http://timheuer.com/blog/archive/2010/09/23/working-with-pictures-in-camera-tasks-in-windows-phone-7-orientation-rotation.aspx).

The following code example shows how you can use the Camera CaptureTask chooser to capture a photo.

C#
CameraCaptureTask ctask = new CameraCaptureTask();
ctask.Completed += new EventHandler<PhotoResult>
(ctask_Completed);
ctask.Show();

You then access the picture that the user captured in your event handler. The user may have taken and discarded several photos, and the chooser returns only the one the user decided to keep. The following code example shows how you can access the chosen photo and save it into isolated storage on the device as a JPEG image. To use this code, you must add references to the namespaces System. Windows.Media.Imaging, System.IO.ISolatedStorage, and Microsoft. Phone (for the PictureDecoder class).

C#
void ctask_Completed(object sender, PhotoResult e)
{
WriteableBitmap theImage = PictureDecoder.DecodeJpeg
(e.ChosenPhoto);
String pictureName = “MyPhoto.jpg”;
// Create a virtual store and file stream.
// Check for an existing duplicate name.
var myStore = IsolatedStorageFile.GetUserStoreForApplication();
if (myStore.FileExists(pictureName))
{
myStore.DeleteFile(pictureName);
}
IsolatedStorageFileStream myFileStream = myStore.
CreateFile(pictureName);
// Encode the WriteableBitmap into the JPEG stream and
// place it into isolated storage.
Extensions.SaveJpeg(theImage, myFileStream,
theImage.PixelWidth, theImage.PixelHeight, 0, 85);
myFileStream.Close();
}

You can also use interop with the XNA operating system classes in Windows Phone 7 to save the photo to the Media Library, or to interact with the Media Library.

For more information about using the camera in your applications, see “CameraCaptureTask Class” on MSDN (http://msdn.microsoft.com/en-us/library/microsoft.phone.tasks.cameracapturetask(VS.92). aspx). You can also download a code sample that demonstrates capturing camera images from “Code Samples for Windows Phone”
on MSDN (http://msdn.microsoft.com/en-us/library/ff431744(VS.92).aspx).