0

自从我在我的 Windows 机器上进行了更新后,我就不能再在使用MediaCapture该类的 UWP 应用程序中使用相机了。

最初,我UnuthorizedAccessException在尝试调用该InitializeAsync()函数时遇到了问题,但是,我设法通过将EnableFrameServerMode添加到我的寄存器来解决此问题(通过此链接:https ://www.macecraft.com/fix-webcam-issues-windows-10 -周年纪念更新/

但是,由于这样做,该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();

我只是得到一个黑色背景的屏幕,相机框架应该在哪里。

4

1 回答 1

0

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

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

于 2019-01-18T07:04:49.440 回答