Image Capture in windows Phone Development

Posted by Unknown

Today i want to explain image capturing in windows phone with a small article 

Let's write code:

Step 1: Add a 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 using directive of 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 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: Now connect your device to PC and wait for Zune to start and sync, now deploy the app and click on the button, you will notice it won't work.

Now disconnect your device from USB and run the application again and 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 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.

Labels:

Post a Comment

 
test