【问题标题】:Setting MediaStreamProperties on HoloLens does not work在 HoloLens 上设置 MediaStreamProperties 不起作用
【发布时间】:2019-11-18 15:28:08
【问题描述】:

我正在开发一个用于 HoloLens 的 UWP 应用程序,以从设备摄像头读取单帧。我想使用可用的最低分辨率的相机模式。

我查看了以下链接和示例,并尝试创建一个最小的工作应用程序:

这是 MainPage.xaml.cs 中的代码 sn-p:

  public async Task<int> Start()
        {
            // Find the sources 
            var allGroups = await MediaFrameSourceGroup.FindAllAsync();
            var sourceGroups = allGroups.Select(g => new
            {
                Group = g,
                SourceInfo = g.SourceInfos.FirstOrDefault(i => i.SourceKind == MediaFrameSourceKind.Color)
            }).Where(g => g.SourceInfo != null).ToList();

            if (sourceGroups.Count == 0)
            {
                // No camera sources found
                return 0;
            }
            var selectedSource = sourceGroups.FirstOrDefault();

            // Initialize MediaCapture
            _mediaCapture = new MediaCapture();
            var settings = new MediaCaptureInitializationSettings()
            {
                SourceGroup = selectedSource.Group,
                SharingMode = MediaCaptureSharingMode.ExclusiveControl,
                StreamingCaptureMode = StreamingCaptureMode.Video,
                MemoryPreference = MediaCaptureMemoryPreference.Cpu
            };
            await _mediaCapture.InitializeAsync(settings);


            // Query all properties of the device 
            IEnumerable<StreamResolution> allVideoProperties = _mediaCapture.VideoDeviceController.GetAvailableMediaStreamProperties(MediaStreamType.VideoRecord).Select(x => new StreamResolution(x));

            // Order them by resolution then frame rate
            allVideoProperties = allVideoProperties.OrderBy(x => x.Height * x.Width).ThenBy(x => x.FrameRate);

            await _mediaCapture.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.VideoRecord, allVideoProperties.ElementAt(0).EncodingProperties);

            // Create the frame reader
            MediaFrameSource frameSource = _mediaCapture.FrameSources[selectedSource.SourceInfo.Id];

            _reader = await _mediaCapture.CreateFrameReaderAsync(frameSource, MediaEncodingSubtypes.Bgra8);
            _reader.FrameArrived += ColorFrameReader_FrameArrivedAsync;
            await _reader.StartAsync();

            return 1;
        }


 private async void ColorFrameReader_FrameArrivedAsync(MediaFrameReader sender, MediaFrameArrivedEventArgs args)
        {

            var frame = sender.TryAcquireLatestFrame();
            if (frame != null)
            {
                var inputBitmap = frame.VideoMediaFrame?.SoftwareBitmap;         
            }
        }

在我的本地机器(带有 Bootcamp 分区的 MacBookPro)上,此代码使用网络摄像头工作。它检测三种支持的视频模式。我可以通过将索引从 0 更改为 1 或 2 来更改 FrameArrivedAsync 中位图图像的分辨率:

_mediaCapture.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.VideoRecord, allVideoProperties.ElementAt(0).EncodingProperties);

在 HoloLens 上,此代码不起作用。它会检测此处解释的不同模式 (https://docs.microsoft.com/en-us/windows/mixed-reality/locatable-camera)。但是设置 MediaStreamProperties 不会改变接收到的位图图像的任何内容。位图始终为 1280x720。

【问题讨论】:

    标签: c# uwp hololens


    【解决方案1】:

    为了以防万一,我们想分享我们如何设置捕获配置文件,您可以参考以下带有注释的代码来修改您的项目进行测试。如有疑问,请随时添加 cmets。

            private async void SetupAndStartMediaCapture()
            {
                string deviceId = string.Empty;
                _mediaCapture = new MediaCapture();
    
                DeviceInformationCollection devices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);
    
                foreach (var device in devices)
                {
                    if(MediaCapture.IsVideoProfileSupported(device.Id))
                    {
                        deviceId = device.Id; 
                        break; // The video device for which supported video profile support is queried.
                    }
                }
    
                MediaCaptureInitializationSettings mediaCapSettings = new MediaCaptureInitializationSettings
                {
                    VideoDeviceId = deviceId
                };
    
                IReadOnlyList<MediaCaptureVideoProfile> profiles = MediaCapture.FindAllVideoProfiles(deviceId); 
    
                var profileMatch = (
                    from profile in profiles
                    from desc in profile.SupportedRecordMediaDescription
                    where desc.Width == 896 && desc.Height == 504 && desc.FrameRate == 24     // HL1
                    select new { profile, desc }
    ).FirstOrDefault();// Select the Profile with the required resolution from all available profiles.
    
                if (profileMatch != null)
                {
                    mediaCapSettings.VideoProfile = profileMatch.profile;
                    mediaCapSettings.RecordMediaDescription = profileMatch.desc;
                }
    
                await _mediaCapture.InitializeAsync(mediaCapSettings); //Initializes the MediaCapture object.
            }
    
    

    【讨论】:

      猜你喜欢
      • 2017-02-16
      • 1970-01-01
      • 2019-06-09
      • 2015-09-08
      • 2018-08-09
      • 1970-01-01
      • 2017-12-16
      • 2013-08-20
      • 2015-12-14
      相关资源
      最近更新 更多