Learn How to Work With the Windows Phone SDK Camera
Working with the camera
Every Windows phone device has sensors to be able to capture the world around you. There are one or more image sensors which enables implementation of a camera capability. A GPS radio which implements location awareness capability. And an accelerometer, which enables you to determine when the device is actually physically moving in 3D space. So let's look at how you use each one of these three capabilities in order. First we'll take a look at our sample application, then we'll dig into the code. So first of all, let's look at the camera. The camera is connected in your code to something called the Camera Task.
And we launch it, the camera shows up. Now, in the Windows Phone Emulator, all you see in the sample camera user experience is this little square moving around. And buttons here at the bottom to simulate whether or not you've got the flash, whether you're connected to the front or rear camera. And whether you're capturing video or still pictures. because there's no camera actually connected to the emulator. When you click on the camera it takes a simulated picture, and then pops up these buttons. To allow you to decide whether you want to accept or retake the picture. When you click Accept, you get a white square with a colored square inside it, depending upon the little image was moving around the square.
When the camera actually, took the picture. Next up, let's take a look at the GPS. And we can start tracking the location. The way we set the location is by using this additional Tools fly-out and clicking on Location. Let's pick Seattle, click right there. Now we click Start Tracking Location. We can see the longitude, latitude and the accuracy within meters. We can also change the location over here, so for example, if we want to say, let's go look at London, we click here, and track location again. Now you can see we're at latitude nearly zero, longitude 51, and again an accuracy of five meters.
We've also hooked up the Maps task here, because In Windows Phone 8, it's incredibly easy to take a GPS coordinate and map it. So let's click the Map It button. Now you're going to get the full-on system mapping capability of Windows Phone without having to write more than about four lines of code in your app. Because it's a privacy consideration, you get this popup that says, will you allow Maps to access your location. We'll say yes, and you can see now we have a Windows Phone map which is actually quite a bit more detailed than this big map of exactly where we clicked over here in London. Finally in our Sensors app, there's an accelerometer where we can track the X Y and Z position of the device.
Over here in the additional tools, there's a simulated accelerometer that allows you to move the device around in 3D space. We can click Start Tracking and as we do. As we move the device around you can see the numbers over here on the left change. For example, this is y almost straight up, I can get it completely y to zero. But if I have the device face down or completely face up. You can see how that moves. There are some prerecorded data for the accelerometer, such as Shake.
So if I play that, you'll see the numbers over these change rapidly because it's simulating the user actually shaking the device. So that's the sample app. Now let's dig into the code behind the Camera task launcher. So we've fired up Visual Studio, and we're going to take a look at the sample implementation of the camera, GPS, and accelerometer that we just watched in the demo. To start with what we have, just the main page with three buttons. Each of those simply launches the camera, GPS, or accelerometer page.
Take a look at the code there. You can see that each one of those simply has a call to NavigationService.Navigate to go off to the proper page. Let's take a look at the camera page. The camera page consists of two controls, a button to launch the camera task, and an image control to display the result you get back from taking the picture. So we take a look at the code behind this. The key thing is to make sure that you bring in Microsoft.Phone.Tasks. This is how you get to the Camera Task. In the constructor, we instantiate a new instance of the Camera task and we also use this PhotoResult EventHandler.
For when the camera takes the picture so that we can capture the image. In our button handler, we simply call CameraCaptureTask.Show. And just for good measure, we cache invalid operation exception in that rare case where you might someday find a Windows Phone device with no camera. In the cameraCaptureTask_Completed handler, we look to see whether the photo result says the task result is okay. Meaning the user actually took the picture, and if it did, we create a BitmapImage.
And then we set the source of that bitmap to the chosen photo property of the photo result from the camera. And then set the image source of our image bitmap, image1, to the bitmapImage object we just created. Now, if you want to see this in action, you can use the emulator, or you can actually connect up a device. And unlike Windows Phone 7, Windows Phone 8 now allows you to debug your camera applications with a live device. So that if you take a picture on the camera, you actually will be able to see the result, and debug the output in Visual Studio.
Now, Microsoft also has sample code that allows you to access the raw camera data. So that if you wanted to create an augmented reality-type app, or things which implement custom zoom features. Or things which require raw access to the camera sensor or the flashlight, you can dig into that at a much deeper level. Windows Phone 8 has additional camera API's. You can create custom lenses. There is new API's for getting a preview image for getting a path to where your image is stored for sharing your media with social networks. And you can also adjust the exposure, focus, and white balance as well as get access to the uncompressed photo data.
Which is something you couldn't do on Windows Phone 7. Windows Phone 8 allows you to integrate into the photo viewer and photo edit picker as well. Now let's go on and take a look at the GPS
Building a Note-Taking App for Windows Phone 8 and Windows Store52,579 Views
Up and Running with Windows Phone 8 Development40,637 Views
Building Windows Store Apps Essential Training98,631 Views
Using Windows Azure with Windows Phone 817,283 Views
Visual Studio 2013 for Windows Store Developers36,401 Views
Illustrator CC 2013 One-on-One: Fundamentals1,678,359 Views
Illustrator CC 2013 One-on-One: Mastery302,647 Views
iOS 8 App Development with Swift 1 Essential Training1,024,214 Views
Illustrator CC 2013 One-on-One: Advanced392,295 Views
Edge Animate Essential Training632,647 Views