UWP MediaCapture Denied Access to Camera

5.5k Views Asked by At

I am trying to make a project that allows me to pull up a camera, but I am told that I was denied access to the camera every time the program ran. I read through the tutorial from the following link https://msdn.microsoft.com/en-us/library/windows/apps/mt243896.aspx and made some minor changes to the code, but the changes shouldn't affect the outcome

    private MediaCapture _mediaCapture;
    private bool _isInitialized;

  private async Task InitializeCameraAsync()
    {
        if (_mediaCapture == null)
        {
            // Get available devices for capturing pictures
            var allVideoDevices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);

            // Get the desired camera by panel
            DeviceInformation cameraDevice =
                allVideoDevices.FirstOrDefault(x => x.EnclosureLocation != null &&
                x.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Back);

            // If there is no camera on the specified panel, get any camera
            cameraDevice = cameraDevice ?? allVideoDevices.FirstOrDefault();

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

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

            MediaCaptureInitializationSettings mediaInitSettings = new MediaCaptureInitializationSettings {
                VideoDeviceId = cameraDevice.Id
              };

            // Initialize MediaCapture
            try
            {
                await _mediaCapture.InitializeAsync(mediaInitSettings);
                _isInitialized = true;
            }
            catch (UnauthorizedAccessException)
            {
                Debug.WriteLine("The app was denied access to the camera");
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Exception when initializing MediaCapture with {0}: {1}", cameraDevice.Id, ex.ToString());
            }

            // If initialization succeeded, start the preview
            if (_isInitialized)
            {
                // Figure out where the camera is located
                if (cameraDevice.EnclosureLocation == null || cameraDevice.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Unknown)
                {
                    // No information on the location of the camera, assume it's an external camera, not integrated on the device
                    _externalCamera = true;
                }
                else
                {
                    // Camera is fixed on the device
                    _externalCamera = false;

                    // Only mirror the preview if the camera is on the front panel
                    _mirroringPreview = (cameraDevice.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Front);
                }

                await StartPreviewAsync();

            }
        }
    }

Also, I made sure that my camera allows access to be used for apps. Does anyone have an idea of why it's not working?

3

There are 3 best solutions below

5
Apoorv On BEST ANSWER

Add Microphone and camera properties from Manifest file. The Manifest file shall be present in the project only. Search for Capabilities tab and check the relevant options

0
jameshfisher On

Use StreamingCaptureMode = StreamingCaptureMode.Video to only request access to the camera, like this:

await _mediaCapture.InitializeAsync(new MediaCaptureInitializationSettings
{
    StreamingCaptureMode = StreamingCaptureMode.Video 
});
0
Martin.Martinsson On

You can enable microphone or video access globally for all apps in registry:

1. reg add HKLM\SOFTWARE\Policies\Microsoft\Windows\AppPrivacy / v LetAppsAccessMicrophone / t REG_DWORD / d 0x1 / f
2. reg add HKLM\SOFTWARE\Policies\Microsoft\Windows\AppPrivacy / v LetAppsAccessCamera / t REG_DWORD / d 0x1 / f

You need admin rights to do it, so add a manifest file to your app to request that right!

    enum AppControlType
    {
        /// <summary>
        /// User must set access
        /// </summary>
        UserHasControl,
        /// <summary>
        /// Approval for all apps
        /// </summary>
        ForceApproval,
        /// <summary>
        /// Denial for all apps
        /// </summary>
        ForceDenial
    }

    static void SetMicrophoneAccessForApps(AppControlType type)
    {
        using var reg = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Policies\\Microsoft\\Windows\\AppPrivacy", true);
        reg?.SetValue("LetAppsAccessMicrophone", type, RegistryValueKind.DWord);
    }

    static void SetCameraAccessForApps(AppControlType type)
    {
        using var reg = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Policies\\Microsoft\\Windows\\AppPrivacy", true);
        reg?.SetValue("LetAppsAccessCamera", type, RegistryValueKind.DWord);
    }

Be aware: you enable microphone or video access for all apps!

Regards