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

前端 未结 2 1653
终归单人心
终归单人心 2021-01-26 06:21

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 <

相关标签:
2条回答
  • 2021-01-26 06:49

    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

    enter image description here

    0 讨论(0)
  • 2021-01-26 06:51

    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.

    0 讨论(0)
提交回复
热议问题