MediaCapture与预览和照片捕获 [英] MediaCapture with Preview and Photo Capture

查看:439
本文介绍了MediaCapture与预览和照片捕获的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用户控件,上面有一个CaptureElement。

I have a user control with a single CaptureElement on it.

<UserControl
    x:Class="TestApp.Views.BarcodeReaderWithPreview"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300"
    d:DesignWidth="400">
    <Grid>
        <CaptureElement x:Name="_captured" />
    </Grid>
</UserControl>

我想在捕获元素中显示相机流,并定期将当前图像保存到文件系统。这是我的代码背后:

I want to show the camera stream in the capture element and periodically save the current image to the file system. This is my code behind:

public async void Run()
{
    try
    {
        var mediaCapture = new MediaCapture();
        var settings = new MediaCaptureInitializationSettings
        {
            PhotoCaptureSource = PhotoCaptureSource.VideoPreview,
            StreamingCaptureMode = StreamingCaptureMode.AudioAndVideo,
        };
        await mediaCapture.InitializeAsync(settings);
        _captured.Source = mediaCapture;
        await mediaCapture.StartPreviewAsync();

        var encodingProperties = new ImageEncodingProperties();
        encodingProperties.Subtype = "BMP";
        encodingProperties.Width = 400;
        encodingProperties.Height = 300;

        while (!_isStopped)
        {
            try
            {
                using (var imageStream = new InMemoryRandomAccessStream())
                {
                    await mediaCapture.CapturePhotoToStreamAsync(encodingProperties, imageStream);
                    await imageStream.FlushAsync();
                    imageStream.Seek(0);
                    var bitmap = new WriteableBitmap(400, 300);
                    bitmap.SetSource(imageStream);

                    try
                    {
                        // TODO: save
                    }
                    catch (Exception e)
                    {
                        Debug.WriteLine(e.Message);
                        Debug.WriteLine(e.StackTrace);
                    }
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
                Debug.WriteLine(e.StackTrace);
            }
        }
    }
    catch (Exception e)
    {
        Debug.WriteLine(e.Message);
        Debug.WriteLine(e.StackTrace);
    }
}

不幸的是,我总是得到以下异常:来自HRESULT的异常:0xC00D3704

Unfortunately, I always get the following Exception: Exception from HRESULT: 0xC00D3704

为什么我得到这个例外吗?

Why I get this exception?

感谢任何帮助!

推荐答案

我能够成功使用以下代码将映像保存到磁盘。它有一些额外的步骤,并将图像存储为jpeg。它应该很容易使它成为位图。

I was able to successfully save an image to disk with the following code. It has some extra steps and stores the image as jpeg. It should be easy to make it a bitmap.

// Setup MediaCapture and initialize
m_mediaCaptureMgr = new Windows.Media.Capture.MediaCapture();
await m_mediaCaptureMgr.InitializeAsync();

// Setup some events for errors
m_mediaCaptureMgr.RecordLimitationExceeded += 
   new Windows.Media.Capture.RecordLimitationExceededEventHandler(
                     RecordLimitationExceeded);
m_mediaCaptureMgr.Failed += 
   new Windows.Media.Capture.MediaCaptureFailedEventHandler(Failed); ;

// Show the live image on screen
previewElement1.Source = m_mediaCaptureMgr;
await m_mediaCaptureMgr.StartPreviewAsync();

// Create a file
m_photoStorageFile = 
     await Windows.Storage.KnownFolders.PicturesLibrary.CreateFileAsync(
         PHOTO_FILE_NAME, Windows.Storage.CreationCollisionOption
                          .GenerateUniqueName);

// Encode for JPEG
ImageEncodingProperties imageProperties = ImageEncodingProperties.CreateJpeg();

// Capture photo as JPG
await m_mediaCaptureMgr.CapturePhotoToStorageFileAsync(
                         imageProperties, m_photoStorageFile);

// Prepare to write photo stream to disk
var photoStream = await m_photoStorageFile.OpenAsync( 
                          Windows.Storage.FileAccessMode.Read);
var bmpimg = new BitmapImage();
bmpimg.SetSource(photoStream);

// Show Photo on screen
imageElement1.Source = bmpimg;














这篇关于MediaCapture与预览和照片捕获的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆