Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Run windows 8 camera api from c#

I'm trying to use webcam to take picture via c#

previously I used some libraries to do that like;

  • webcamlibrarydotnet
  • directshownet
  • Versatile-WebCam-C-library
  • Webcam-in-Csharp-Easiest-Way-to-Capture-Images-fro
  • aforge

I faced a problem that when I used default win 8(by the way my operation system is win 8) camera application's maximum photo quality was 8px (3264 x 2468); but using above libraries I programaticaly searched for available snapshot qualities, the maximum size was below 2px. I have no idea how win 8 default camera app makes such a big difference. Therefore I decided to use the default windows 8 camera application.

Problem I googled for solution but not could find any idea about how to programaticaly run win 8 camera app from c#. (Just like mobile phones open camera application: take photo, close cam application, then coppy picture from its location into your application directory) Can anyone help please?

like image 565
Ismail Sahin Avatar asked Sep 19 '13 15:09

Ismail Sahin


2 Answers

Its very simple. Just follow below steps.

protected async override void OnNavigatedTo(NavigationEventArgs e)
    {
        var ui = new CameraCaptureUI();
        ui.PhotoSettings.CroppedAspectRatio = new Size(4, 3);
        var file = await ui.CaptureFileAsync(CameraCaptureUIMode.Photo);

        if (file != null)
        {
            var bitmap = new BitmapImage();
            bitmap.SetSource(await file.OpenAsync(FileAccessMode.Read));
            Photo.Source = bitmap;
        }
    }

Use below header files,

using Windows.Media.Capture;
using Windows.UI.Xaml.Media.Imaging; // for BitmapImage
using Windows.Storage; // for FileAccessMode

Add Photo inside Grid, like this

<Grid Background="{StaticResource ApplicationPageBackgroundBrush}">
 <Image x:Name="Photo" Width="700" Height="700" />
</Grid>

And finally, Allow WebCam in Capabilities Which you can find in package.manifest. You are done.

like image 116
Balasubramani M Avatar answered Oct 19 '22 01:10

Balasubramani M


Did you try to set some properties to default dialog?

public static async Task CapturePhoto()
        {
            CameraCaptureUI dialog = new CameraCaptureUI();
            Size aspectRatio = new Size(16, 9);  // 1
            dialog.PhotoSettings.CroppedAspectRatio = aspectRatio; // 2
            dialog.PhotoSettings.MaxResolution = CameraCaptureUIMaxPhotoResolution.HighestAvailable; // 3
            dialog.PhotoSettings.Format = CameraCaptureUIPhotoFormat.Jpeg; // 4
            dialog.PhotoSettings.AllowCropping = true; //5

            StorageFile file = await dialog.CaptureFileAsync(CameraCaptureUIMode.Photo); // Your new file
        }

In my Windows Store App it works great.

like image 35
jimpanzer Avatar answered Oct 19 '22 00:10

jimpanzer