gpt4 book ai didi

c# - UWP MediaCapture 拒绝访问相机

转载 作者:太空狗 更新时间:2023-10-30 00:01:19 26 4
gpt4 key购买 nike

我正在尝试制作一个允许我拉起相机的项目,但我被告知每次程序运行时我都被拒绝访问相机。我从以下链接阅读了教程 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();

}
}
}

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

最佳答案

Manifest 文件添加麦克风和摄像头属性。 list 文件应仅存在于项目中。搜索Capabilities标签并检查相关选项

关于c# - UWP MediaCapture 拒绝访问相机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34952684/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com