2017-06-29 96 views
2

我嘗試在UWP應用訪問攝像頭,但我有一個錯誤:如何使用MediaCapture訪問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"); 
} 

你能向我解釋爲什麼異常發生,如何解決問題?

回答

3

您應該爲您的項目設置麥克風和攝像頭功能。要做到這一點,你可以按照下面的步驟

  1. 打開Package.appxmanifest
  2. 轉到功能選項卡
  3. 檢查麥克風網絡攝像頭
相關問題