Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从MediaCapture访问预览帧_C#_Xaml_Windows Store Apps_Windows Phone 8.1 - Fatal编程技术网

C# 从MediaCapture访问预览帧

C# 从MediaCapture访问预览帧,c#,xaml,windows-store-apps,windows-phone-8.1,C#,Xaml,Windows Store Apps,Windows Phone 8.1,我想获取显示在我的capturelementxaml元素中的预览帧。myCaptureElement的source设置为MediaCapture对象,我使用StartView()方法开始显示相机。我希望访问正在显示的帧,而不将其保存到img或视频文件中。目标是从预览中捕获10帧,并将每个帧发送到另一个接受字节[]的类 我尝试使用CapturePhotoToStorageFileAsync方法,但是这不是一个可行的选择,因为我不想每秒拍摄10张实际图像。我也不想使用screenscapture,因

我想获取显示在我的
capturelement
xaml元素中的预览帧。my
CaptureElement
source
设置为
MediaCapture
对象,我使用
StartView()
方法开始显示相机。我希望访问正在显示的帧,而不将其保存到img或视频文件中。目标是从预览中捕获10帧,并将每个帧发送到另一个接受字节[]的类

我尝试使用CapturePhotoToStorageFileAsync方法,但是这不是一个可行的选择,因为我不想每秒拍摄10张实际图像。我也不想使用
screenscapture
,因为它将捕获的内容存储到视频文件中。理想情况下,我不想在手机上临时存储任何媒体文件。在查看了for
MediaCapture
之后,我注意到有一个名为
GetPreviewFrameAsync()
的方法,但是这个方法在Windows Phone 8.1中不存在。我也偶然发现了这一点,但我不完全理解它是如何工作的


非常感谢您对如何实现这一点提出的任何建议。

Microsoft github页面上有一个相关的示例,尽管它们针对的是Windows 10。您可能对迁移项目以获得此功能感兴趣

:此示例将捕获预览帧,而不是完整的照片。一旦有了预览帧,它就可以编辑其上的像素

以下是相关部分:

private async Task GetPreviewFrameAsSoftwareBitmapAsync()
{
    // Get information about the preview
    var previewProperties = _mediaCapture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview) as VideoEncodingProperties;

    // Create the video frame to request a SoftwareBitmap preview frame
    var videoFrame = new VideoFrame(BitmapPixelFormat.Bgra8, (int)previewProperties.Width, (int)previewProperties.Height);

    // Capture the preview frame
    using (var currentFrame = await _mediaCapture.GetPreviewFrameAsync(videoFrame))
    {
        // Collect the resulting frame
        SoftwareBitmap previewFrame = currentFrame.SoftwareBitmap;

        // Add a simple green filter effect to the SoftwareBitmap
        EditPixels(previewFrame);
    }
}

private unsafe void EditPixels(SoftwareBitmap bitmap)
{
    // Effect is hard-coded to operate on BGRA8 format only
    if (bitmap.BitmapPixelFormat == BitmapPixelFormat.Bgra8)
    {
        // In BGRA8 format, each pixel is defined by 4 bytes
        const int BYTES_PER_PIXEL = 4;

        using (var buffer = bitmap.LockBuffer(BitmapBufferAccessMode.ReadWrite))
        using (var reference = buffer.CreateReference())
        {
            // Get a pointer to the pixel buffer
            byte* data;
            uint capacity;
            ((IMemoryBufferByteAccess)reference).GetBuffer(out data, out capacity);

            // Get information about the BitmapBuffer
            var desc = buffer.GetPlaneDescription(0);

            // Iterate over all pixels
            for (uint row = 0; row < desc.Height; row++)
            {
                for (uint col = 0; col < desc.Width; col++)
                {
                    // Index of the current pixel in the buffer (defined by the next 4 bytes, BGRA8)
                    var currPixel = desc.StartIndex + desc.Stride * row + BYTES_PER_PIXEL * col;

                    // Read the current pixel information into b,g,r channels (leave out alpha channel)
                    var b = data[currPixel + 0]; // Blue
                    var g = data[currPixel + 1]; // Green
                    var r = data[currPixel + 2]; // Red

                    // Boost the green channel, leave the other two untouched
                    data[currPixel + 0] = b;
                    data[currPixel + 1] = (byte)Math.Min(g + 80, 255);
                    data[currPixel + 2] = r;
                }
            }
        }
    }
}
当然,您的项目必须允许使用不安全的代码才能使所有这些工作正常进行


仔细查看示例,了解如何获取所有细节。或者,要进行演练,您可以从最近的//build/会议中观看,其中包括一些相机示例的演练。

非常有趣!我最终使用了Lumia Imaging SDK,因为我需要支持当前的Wp 8.1设备。每当有预览框架可用时,sdk就会启动一个事件。来看看Wp 10,我一定会更深入地研究这一点。谢谢
[ComImport]
[Guid("5b0d3235-4dba-4d44-865e-8f1d0e4fd04d")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
unsafe interface IMemoryBufferByteAccess
{
    void GetBuffer(out byte* buffer, out uint capacity);
}