Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Using Windows Phone Camera on Image Control

DZone's Guide to

Using Windows Phone Camera on Image Control

· Mobile Zone
Free Resource

Download this comprehensive Mobile Testing Reference Guide to help prioritize which mobile devices and OSs to test against, brought to you in partnership with Sauce Labs.

This will be a very small article on how to capture images using Windows Phone Camera and Display on Image Control, and one of the issue I encountered while using camera in Windows Phone.

Let's write the code:

Step 1: Add an image button inside ContentPanel of MainPage.xaml.

<Image Height="450" HorizontalAlignment="Left" Margin="50,0,0,0" Name="Image1" VerticalAlignment="Top" Width="400" />

Step 2: Add a button in the application bar or inside ContentPanel.

<phone:PhoneApplicationPage.ApplicationBar>
   <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
      <shell:ApplicationBarIconButton Text="camera" IconUri="appbar.camera.png" x:Name="btnCamera" Click="btnCamera_Click"/>
   </shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>

Step 3: Add a Using Directive for Microsoft.Phone.Tasks and System.Windows.Media.Imaging.

using Microsoft.Phone.Tasks;
using System.Windows.Media.Imaging;

Step 4: Add a class level variable of CameraCaptureTask in MainPage.xaml.cs

public partial class camera : PhoneApplicationPage
{
   CameraCaptureTask cameraCapture = new CameraCaptureTask();

Step 5: Add a camera capture completed event in the constructor of MainPage.xaml.

cameraCapture.Completed += new EventHandler<PhotoResult>(cameraCapture_Completed);

Step 6: Add cameraCapture_Completed method in MainPage.xaml.cs which will be executed once the photo is captured.

void cameraCapture_Completed(object sender, PhotoResult e)
{
   if (e.TaskResult == TaskResult.OK)
   {
      Image1.Source = new BitmapImage(new Uri(e.OriginalFileName));
   }
}

Step 7: Add btnCamera_Click method which will show the camera to take picture.

private void btnCamera_Click(object sender, EventArgs e)
{
   try
   {
      cameraCapture.Show();
   }
   catch (Exception ex)
   {
   }
}

Step 8: Connect your device to your PC and wait for Zune to start and sync. Then, deploy the app and click on the button. You will notice it won't work.

Now, disconnect your device from the USB and run the application again. Click on the button to turn on the camera and take photo. Once you click on the camera button, there will be two buttons: accept, and retake.

Windows Phone Camera

Once you click on the accept button, the taken photo will appear on the image control of MainPage.xaml.

Windows Phone CameraCaptureTask

Important: Device should not be connected to PC when you are capturing photo using CameraCaptureTask.

This ends the article about taking photos using camera and display on image control.

 

 

Analysts agree that a mix of emulators/simulators and real devices are necessary to optimize your mobile app testing - learn more in this white paper, brought to you in partnership with Sauce Labs.

Topics:

Published at DZone with permission of Sumit Dutta, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}