首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >App MediaFrameReader总是返回空位图

App MediaFrameReader总是返回空位图
EN

Stack Overflow用户
提问于 2020-12-10 13:20:54
回答 1查看 175关注 0票数 0

我使用MediaCapture在屏幕上预览相机,这是很好的工作。

但是,我需要访问我的应用程序中的当前帧。因此,我将FrameReader添加到MediaCapture中以获取事件Reader_FrameArrived。

因此,该事件正在工作,但是位图本身始终为空。

我实现了ecent回调,如示例中所示:

代码语言:javascript
运行
复制
var mediaFrameReference = sender.TryAcquireLatestFrame();
var videoMediaFrame = mediaFrameReference?.VideoMediaFrame;
var softwareBitmap = videoMediaFrame?.SoftwareBitmap;

if (softwareBitmap != null)
{
    Debug.WriteLine("here");
}
else
{
    return;
}

我就是这样初始化读取器的:

代码语言:javascript
运行
复制
var frameSourceGroups = await MediaFrameSourceGroup.FindAllAsync();

var allGroups = await MediaFrameSourceGroup.FindAllAsync();
var eligibleGroups = allGroups.Select(g => new
{
    Group = g,

    // For each source kind, find the source which offers that kind of media frame,
    // or null if there is no such source.
    SourceInfos = new MediaFrameSourceInfo[]
    {
        g.SourceInfos.FirstOrDefault(info => info.SourceKind == MediaFrameSourceKind.Color),
        g.SourceInfos.FirstOrDefault(info => info.SourceKind == MediaFrameSourceKind.Depth),
        g.SourceInfos.FirstOrDefault(info => info.SourceKind == MediaFrameSourceKind.Infrared),
    }
}).Where(g => g.SourceInfos.Any(info => info != null)).ToList();

if (eligibleGroups.Count == 0)
{
    System.Diagnostics.Debug.WriteLine("No source group with color, depth or infrared found.");
    return;
}

var selectedGroupIndex = 0; // Select the first eligible group
MediaFrameSourceGroup selectedGroup = eligibleGroups[selectedGroupIndex].Group;
MediaFrameSourceInfo colorSourceInfo = eligibleGroups[selectedGroupIndex].SourceInfos[0];
MediaFrameSourceInfo infraredSourceInfo = eligibleGroups[selectedGroupIndex].SourceInfos[1];
MediaFrameSourceInfo depthSourceInfo = eligibleGroups[selectedGroupIndex].SourceInfos[2];

//_mediaCapture.FrameSources.TryGetValue(cameraDevice.Id, out _source);
var colorFrameSource = _mediaCapture.FrameSources[colorSourceInfo.Id];

if (colorFrameSource != null)
{
    _reader = await _mediaCapture.CreateFrameReaderAsync(colorFrameSource, MediaEncodingSubtypes.Argb32);
    _reader.FrameArrived += Reader_FrameArrived;
    
    MediaFrameReaderStartStatus result = await _reader.StartAsync();

    Debug.WriteLine(result.ToString());
}

知道为什么位图可能是空的吗?

EN

Stack Overflow用户

回答已采纳

发布于 2020-12-10 14:35:01

我使用MediaCapture在屏幕上预览相机,这是很好的工作。

对于该场景,我们建议您使用MediaCapture类捕获位图,它包含GetPreviewFrameAsync方法从捕获设备获取预览帧,而不是将其转换为SoftwareBitmap

代码语言:javascript
运行
复制
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;

        // Show the frame information
        FrameInfoTextBlock.Text = String.Format("{0}x{1} {2}", previewFrame.PixelWidth, previewFrame.PixelHeight, previewFrame.BitmapPixelFormat);

        // Add a simple green filter effect to the SoftwareBitmap
        if (GreenEffectCheckBox.IsChecked == true)
        {
            ApplyGreenFilter(previewFrame);
        }

        // Show the frame (as is, no rotation is being applied)
        if (ShowFrameCheckBox.IsChecked == true)
        {
            // Create a SoftwareBitmapSource to display the SoftwareBitmap to the user
            var sbSource = new SoftwareBitmapSource();
            await sbSource.SetBitmapAsync(previewFrame);

            // Display it in the Image control
            PreviewFrameImage.Source = sbSource;
        }

        // Save the frame (as is, no rotation is being applied)
        if (SaveFrameCheckBox.IsChecked == true)
        {
            var file = await _captureFolder.CreateFileAsync("PreviewFrame.jpg", CreationCollisionOption.GenerateUniqueName);

            Debug.WriteLine("Saving preview frame to " + file.Path);

            await SaveSoftwareBitmapAsync(previewFrame, file);
        }
    }
}

这是官方的代码样本,你可以直接参考。

票数 1
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65235335

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档