【问题标题】:C# MediaCapture UnuthorizedAccessException or no supported formats after Windows UpdateC# MediaCapture UnuthorizedAccessException 或 Windows 更新后不支持的格式
【发布时间】: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


    【解决方案1】:

    这仅发生在我的机器上,具有相同机器(相同硬件和软件)以及不同机器的同事没有遇到此问题。

    我检查了你的代码,它与CameraFrames官方代码示例相似。我用 LifeCam HD-3000 进行了测试,效果很好(不修改注册表)。我认为您的硬件设备很可能存在问题。我认为您可以使用其他相机故障排除。对于使用相机,您也可以查看此sample

    【讨论】:

    • 是的,我尝试了 CameraFrames 示例,但我遇到了同样的错误。我的同事刚刚更新了他的 Windows 机器,现在遇到了同样的问题。
    • 最后,它竟然是我的杀毒软件……有点奇怪,我们可以用注册表值绕过最初的“访问被拒绝”异常,也许要记住一些事情。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-12
    • 1970-01-01
    相关资源
    最近更新 更多