【发布时间】:2019-11-18 15:28:08
【问题描述】:
我正在开发一个用于 HoloLens 的 UWP 应用程序,以从设备摄像头读取单帧。我想使用可用的最低分辨率的相机模式。
我查看了以下链接和示例,并尝试创建一个最小的工作应用程序:
- https://docs.microsoft.com/en-us/windows/uwp/audio-video-camera/set-media-encoding-properties
- https://docs.microsoft.com/en-us/windows/uwp/audio-video-camera/use-opencv-with-mediaframereader
- https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/CameraResolution
- https://github.com/microsoft/Windows-universal-samples/tree/master/Samples/CameraOpenCV
这是 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。
【问题讨论】: