How to Build a Mobile App That Uses a Camera
Social apps have been gaining popularity, and learning how to build a Windows Phone mobile app that uses the camera can get you started in creating your first social app. C# is a computer programming language that can be used to build your Windows Phone app. The camera capture task is used to take a photo directly from your mobile application. Use the “ChosenPhoto” property to retrieve the image taken and you can display it using an image control.
Instructions
-
-
1
Launch Microsoft Visual Studio. Click the “New Project” link and expand “Other Languages” below Installed Templates. Expand the “Visual C#” node and click “Silverlight for Windows Phone.” Double-click “Windows Phone Application” to create a new project.
-
2
Double-click “Button” on the Toolbox pane to add a new button to your mobile app. Add an “Image” control using the same technique.
-
-
3
Double-click the button you added to create a click event. A button click event will execute the code inside of it when the button is clicked. Add the following code inside the click event to launch the camera task:
try
{
useCameraTask.Show();
}
catch (System.InvalidOperationException)
{
MessageBox.Show("Problem capturing picture");
} -
4
Copy and paste the following two lines of code in the namespace area located in the very top of your module:
using System.Windows.Media.Imaging;
using Microsoft.Phone.Tasks; -
5
Add the following code below “InitializeComponent” inside the “MainPage” procedure:
useCameraTask = new CameraCaptureTask();
useCameraTask.Completed += new EventHandler<PhotoResult>(cameraUsed_Completed); -
6
Create the procedure that will save the image taken by the camera by inserting the following code:
void cameraUsed_Completed(object sender, PhotoResult e)
{
if (e.TaskResult == TaskResult.OK)
{
System.Windows.Media.Imaging.BitmapImage imageTaken = new System.Windows.Media.Imaging.BitmapImage();
imageTaken.SetSource(e.ChosenPhoto);
this.image1.Source = imageTaken;
}
} -
7
Click the target combo box and choose “Windows Phone Emulator” to deploy your program to the Window Phone Emulator. Press the “F5” key to run your mobile app. Click “Button” to launch the camera and click the icon on the right-hand corner of the emulator to take the picture. Click “Accept” to display the image taken through the image control.
-
1
References
Resources
- Photo Credit Jupiterimages/Photos.com/Getty Images