首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UWP MediaCapture拒绝进入照相机

UWP MediaCapture拒绝进入照相机
EN

Stack Overflow用户
提问于 2016-01-22 17:30:44
回答 2查看 5.3K关注 0票数 6

我试图做一个项目,让我可以拉相机,但我被告知,我被拒绝了相机每次运行程序。我从下面的链接https://msdn.microsoft.com/en-us/library/windows/apps/mt243896.aspx中阅读了本教程,并对代码做了一些微小的更改,但这些更改不应影响结果

代码语言:javascript
复制
    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();

            }
        }
    }

此外,我确保我的相机允许访问应用程序。有人知道为什么它不起作用吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-04-04 10:26:01

Manifest文件中添加麦克风和照相机属性。清单文件只存在于项目中。搜索功能选项卡并检查相关选项

票数 21
EN

Stack Overflow用户

发布于 2020-05-02 09:00:49

使用StreamingCaptureMode = StreamingCaptureMode.Video只请求访问照相机,如下所示:

代码语言:javascript
复制
await _mediaCapture.InitializeAsync(new MediaCaptureInitializationSettings
{
    StreamingCaptureMode = StreamingCaptureMode.Video 
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34952684

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档