5

カメラを取得できるプロジェクトを作成しようとしていますが、プログラムを実行するたびにカメラへのアクセスが拒否されたと言われます。次のリンクからチュートリアルを読みましたhttps://msdn.microsoft.com/en-us/library/windows/apps/mt243896.aspxコードにいくつかの小さな変更を加えましたが、変更は結果に影響しません

    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();

            }
        }
    }

また、カメラでアプリへのアクセスが許可されていることを確認しました。なぜそれが機能しないのか、誰かが考えを持っていますか?

4

2 に答える 2