c# - 我应该如何使用 MediaCapture 访问 UWP 应用程序中的摄像头?

标签 c# uwp camera media

我尝试在 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. 打开Package.appxmanifest
  2. 转到功能选项卡
  3. 检查麦克风网络摄像头

关于c# - 我应该如何使用 MediaCapture 访问 UWP 应用程序中的摄像头?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44825091/

相关文章:

c# - HTML 到格式化文本 C#

c# - WPF:获取堆栈面板中触摸的项目索引

javascript - Windows 8 如何选择要初始化的相机

android - Webrtc Android 摄像头捕获

c# - 在 C# 中的属性的设置访问器中重命名 "value"变量

c# - 使用 SafeHandle 的约束与抽象类

c# - 在 UWP 中播放流中的音频

c# - 在 Microsoft Store 中共享 UWP App 的链接

c++ - 使用 XAML 和 C++ 再现本地视频文件

.net - OpenCV多线程调用