MediaCapture Windows 8 Desktop Application- Can't get it work

余生颓废 提交于 2019-12-02 17:23:29

问题


I'm trying to implement an image capture via this API, and I need to implement in via desktop application.

The problem is that when I save the image into a file (with CapturePhotoToStorageFileAsync) I get a dark picture (almost black), or I get zero size file.

My code is very simple though:

MediaCapture = new MediaCapture();
await mediaCapture.InitializeAsync();

Windows.Storage.StorageFile photoStorageFile;
String PHOTO_FILE_NAME = "photo.jpg";
photoStorageFile = await Windows.Storage.KnownFolders.PicturesLibrary.CreateFileAsync(PHOTO_FILE_NAME, Windows.Storage.CreationCollisionOption.GenerateUniqueName);
ImageEncodingProperties imageProperties = ImageEncodingProperties.CreateJpeg();
await mediaCapture.CapturePhotoToStorageFileAsync(imageProperties, photoStorageFile);

I read another thread about it: Captured photos are black when using MediaCapture on Windows Phone 8.1

The problem is that MediaCapture.StartPreviewAsync raised an exception "An unexpected error has occured in the operation requested", and I guess the reason for that is the MediaElement which I don't have (again, desktop).

I'll also glad to find a working example, when I searched, I didn't find any real sample to implement MediaCapture on desktop application, I saw an article but without attached code: http://blogs.msdn.com/b/eternalcoding/archive/2013/10/29/how-to-use-specific-winrt-api-from-desktop-apps-capturing-a-photo-using-your-webcam-into-a-wpf-app.aspx

I also found that others also asked about a sample, but with no real answer...

How can I get a working sample for that?


回答1:


Getting MediaCapture to preview the camera video in Desktop apps is not trivial. I wasn't sure all the pieces were there so I put together some code on GitHub to try it out and it seems to work ok.

https://github.com/mmaitre314/MediaCaptureWPF

One caveat: building the MediaCapture/WPF interop required some native code (C++/CLI), so the app needs to be built as either x86 or x64, but not AnyCPU.




回答2:


In order for your store app to interact with the Camera there are actually two ways, a simple one, and an advanced one:

  • Either by using the provided user interface, which is a common UI used to capture Media and it save you the pain of handling the basic capture operations,
  • Or by interacting with the MediaCapture API yourself to gain complete control over the entire flow of operations.

if you just want to take a picture or a video from the webcam, i suggest you use the first approach which mainly use the CameraCaptureUI API, but if you need complete control over the capturing operation, you need some lines of codes that interact with the MediaCapture APIs

here a simple example using the CameraCaptureUI API :

let say we have the following UI:

 <StackPanel Orientation="Vertical">
        <Button Click="TakePicture_Click" Content="Capture Photo" HorizontalAlignment="Stretch"/>
        <Border BorderBrush="White" BorderThickness="3">
            <Image x:Name="Picture" Height="700" VerticalAlignment="Center" HorizontalAlignment="Center" />
        </Border>            
    </StackPanel>

and here the capture button click handler:

private async void TakePicture_Click(object sender, RoutedEventArgs e)
    {
        var camera = new CameraCaptureUI();
        var image = await camera.CaptureFileAsync(CameraCaptureUIMode.Photo);
        if (image != null)
        {
            var stream = await image.OpenAsync(FileAccessMode.Read);
            var bitmap = new BitmapImage();
            bitmap.SetSource(stream);
            Picture.Source = bitmap;
        }
        else
        {
            (new MessageDialog("Something went wrong")).ShowAsync();                
        }
    }

you need also to offer your app the permission to use the webcam by checking the Webcam capability in the App Manifest



来源:https://stackoverflow.com/questions/31369312/mediacapture-windows-8-desktop-application-cant-get-it-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!