如何使用MediaCapture在UWP应用程序中访问摄像头?

时间:2017-06-29 12:24:41

标签: c# camera uwp media access

我尝试在UWP应用程序中访问摄像头,但我有一个错误:

Exception thrown: 'System.UnauthorizedAccessException' in mscorlib.ni.dll
WinRT information: Access is denied.

The app was denied access to the camera
The thread 0x2c9c has exited with code 0 (0x0).
The program '[3352] CameraGetPreviewFrame.exe' has exited with code 1 (0x1).

这是我的代码:

if (cameraDevice == null)
{
    Debug.WriteLine("No camera device found!");
    return;
}

// Create MediaCapture and its settings
_mediaCapture = new MediaCapture();

// Register for a notification when something goes wrong
_mediaCapture.Failed += MediaCapture_Failed;

var settings = new MediaCaptureInitializationSettings { VideoDeviceId = cameraDevice.Id };

// Initialize MediaCapture
try
{
    await _mediaCapture.InitializeAsync(settings);
    _isInitialized = true;
}
catch (UnauthorizedAccessException)
{
    Debug.WriteLine("The app was denied access to the camera");
}

您能否向我解释为什么会发生异常以及如何解决问题?

1 个答案:

答案 0 :(得分:3)

您应为项目设置麦克风和网络摄像头功能。为此,您可以按照以下步骤进行操作

  1. 打开 Package.appxmanifest
  2. 转到功能标签
  3. 检查麦克风网络摄像头