【发布时间】:2019-06-11 11:52:42
【问题描述】:
自从我在我的 Windows 机器上进行更新后,我再也无法在使用 MediaCapture 类的 UWP 应用程序中使用相机。
最初,我在尝试调用 InitializeAsync() 函数时收到了 UnuthorizedAccessException,但是,我设法通过将 EnableFrameServerMode 添加到我的寄存器中来解决此问题(通过此链接:https://www.macecraft.com/fix-webcam-issues-windows-10-anniversary-update/ )
但是,由于这样做,MediaFrameSource.SupportedFormats 属性为空(MediaFrameSource.SupportedFormats.Count 为 0)。
这仅发生在我的机器上,具有相同机器(相同硬件和软件)以及不同机器的同事没有遇到此问题。
我目前正在跑步:
- Windows 10 家庭版
- 版本 10.0.17134 内部版本 17134
我正在使用 Visual Studio 版本 15.9.4 构建具有 Xamarin.Forms 的应用程序。 该应用的目标版本是 Windows 10.0 Build 16299(最低版本相同)。
这是我的 ViewRenderer 中的代码:
var frameSourceGroups = await MediaFrameSourceGroup.FindAllAsync();
MediaFrameSourceGroup selectedGroup = null;
MediaFrameSourceInfo colorSourceInfo = null;
foreach (var sourceGroup in frameSourceGroups)
{
foreach (var sourceInfo in sourceGroup.SourceInfos)
{
if (sourceInfo.MediaStreamType == MediaStreamType.VideoRecord
&& sourceInfo.SourceKind == MediaFrameSourceKind.Color
&& sourceInfo.DeviceInformation.EnclosureLocation != null
&& sourceInfo.DeviceInformation.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Front)
{
colorSourceInfo = sourceInfo;
break;
}
colorSourceInfo = sourceInfo;
}
if (colorSourceInfo != null)
{
selectedGroup = sourceGroup;
break;
}
selectedGroup = sourceGroup;
}
if (selectedGroup == null || colorSourceInfo == null) return;
_mediaCapture = new MediaCapture();
var settings = new MediaCaptureInitializationSettings()
{
SourceGroup = selectedGroup,
SharingMode = MediaCaptureSharingMode.ExclusiveControl,
MemoryPreference = MediaCaptureMemoryPreference.Cpu,
StreamingCaptureMode = StreamingCaptureMode.Video
};
try
{
await _mediaCapture.InitializeAsync(settings);
}
catch (UnauthorizedAccessException)
{
Debug.WriteLine("The app was denied access to the camera");
return;
}
catch (Exception ex)
{
Debug.WriteLine("MediaCapture initialization failed: " + ex.Message);
return;
}
如果我的注册表中的 EnableFrameServerMode 未设置为 0,我会得到一个 UnauthorizedAccessException。如果是:
MediaFrameSource colorFrameSource = null;
foreach (var keyValuePairFrameSource in _mediaCapture.FrameSources)
{
if (keyValuePairFrameSource.Key.ToLowerInvariant().Contains(colorSourceInfo.SourceGroup.Id.ToLowerInvariant()))
{
colorFrameSource = keyValuePairFrameSource.Value;
}
}
_config = ConfigurationHelper.GetConfigData();
var preferredFormat = colorFrameSource?.SupportedFormats.FirstOrDefault(format =>
format.VideoFormat.Width == _config.CameraPreviewWidth &&
format.VideoFormat.Height == _config.CameraPreviewHeight &&
format.Subtype == MediaEncodingSubtypes.Nv12.ToUpper());
if (preferredFormat == null)
{
Debug.WriteLine("Our desired format is not supported");
return;
}
SupportedFormats 列表为空,导致我们的preferredFormat 对象为空。
如果我在preferredFormat 为空时不返回,请执行以下操作:
var _mediaFrameReader = await _mediaCapture.CreateFrameReaderAsync(colorFrameSource, MediaEncodingSubtypes.Nv12);
_mediaFrameReader.FrameArrived += ColorFrameReader_FrameArrived;
await _mediaFrameReader.StartAsync();
我只是得到一个黑色背景的屏幕,相机框架应该在。
【问题讨论】:
标签: c# windows xamarin uwp mediacapture