Derik Whittaker

Syndication

News


How to Capture Images w/ the WebCam in WinRT (C#) applications.

When building WinRT applications you will want to implement the ability to capture images/video’s from the onboard webcamera in order to use in your application.  Fortunately for you this is very easy to accomplish.  We will walk through how to do this in this post.

Step 1: Enable access to the web camera

The first thing you will need to do is to enable the ‘Webcam’ capability in your application.  If you do not do this you will see the following screen when trying to use the webcam API

image

To enable this capability do this

  1. Open the Package.appxmanifest
  2. Click on the capabilities tab
  3. Check the Webcam item in the list

image

Step 2: Start to implement the Camera API, basic approach

private async void CapturePicture()
{
    var dialog = new CameraCaptureUI();


    var file = await dialog.CaptureFileAsync(CameraCaptureUIMode.Photo);
    if (file != null)
    {
	// Do something useful w/ the file
    }
}

The code above is the least amount you can use to access the webcamera, but we can add a few options to help the user out

Step 3: Allow the user to Crop the image.

When using the API you can toggle a setting which will allow the user to crop the image to fit their own need.

private async void CapturePicture()
{
    var dialog = new CameraCaptureUI();
    dialog.PhotoSettings.AllowCropping = true;

    var file = await dialog.CaptureFileAsync(CameraCaptureUIMode.Photo);
    if (file != null)
    {
	// Do something useful w/ the file
    }
}

If you set the bool flag you will get the following app bar button

image

You will also get the crop ‘points’ once you hit the crop button as below

image

Step 4: Change the max resolution

If you want, you can even change the resolution allowed by your application.

private async void CapturePicture()
{
    var dialog = new CameraCaptureUI();
    dialog.PhotoSettings.MaxResolution = CameraCaptureUIMaxPhotoResolution.HighestAvailable;

    var file = await dialog.CaptureFileAsync(CameraCaptureUIMode.Photo);
    if (file != null)
    {
	// Do something useful w/ the file
    }
}

When setting the resolution you can pick from many options which are part of the enumeration as shown below:

image

Step 5: Choosing the format for the image to be saved as

private async void CapturePicture()
{
    var dialog = new CameraCaptureUI();
    dialog.PhotoSettings.Format = CameraCaptureUIPhotoFormat.Png;

    var file = await dialog.CaptureFileAsync(CameraCaptureUIMode.Photo);
    if (file != null)
    {
	// Do something useful w/ the file
    }
}

When setting the format you can pick from multiple options that are part of the enumeration as shown below:

image

As you can see using the Camera API can be very basic or you can customize to meet the direct needs of your application, it is all up to you.

Till next time,


Posted 07-06-2012 1:21 PM by Derik Whittaker
Filed under: , ,

[Advertisement]

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)