问题描述
所以我找不到关于这个的任何信息,想知道是否有人遇到过同样的问题。
我在 dot net core 3.1 版本中使用 "c#" 和 "Windows.Media.Capture"。 当我将 build 用作 exe 时,我可以与网络摄像头通信并获取图像和录制视频。
但是,当我将 exe 捆绑到服务中时(我使用的是 NSSM),我在初始化时遇到了未经授权的访问异常。
我已更改相机隐私设置以允许所有桌面应用程序等使用相机。
我找不到访问摄像头的服务选项。我还更改了注册表设置并创建了组策略,但没有成功。
我相信这是某种形式的隐私设置,因为该应用程序在作为桌面应用程序运行时可以正常工作。
任何帮助将不胜感激。 谢谢 罗布
这里还有我正在使用的初始化代码(直接来自微软网站)。只是为了透明。
/// <summary>
/// Initializes the MediaCapture,registers events,gets camera device @R_815_4045@ion for mirroring and rotating,starts preview and unlocks the UI
/// </summary>
/// <returns></returns>
private async Task InitializeCameraAsync()
{
Debug.WriteLine("InitializeCameraAsync");
var picturesLibrary = await
StorageLibrary.GetLibraryAsync(KNownLibraryId.Pictures);
// Fall back to the local app storage if the Pictures Library is not available
_captureFolder = picturesLibrary.SaveFolder ?? ApplicationData.Current.LocalFolder;
if (_mediaCapture == null)
{
// Attempt to get the back camera if one is available,but use any camera device if not
var cameraDevice = await FindCameraDeviceByPanelAsync(Windows.Devices.Enumeration.Panel.Back);
if (cameraDevice == null)
{
Debug.WriteLine("No camera device found!");
return;
}
// Create MediaCapture and its settings
_mediaCapture = new MediaCapture();
// Register for a notification when video recording has reached the maximum time and when something goes wrong
// _mediaCapture.RecordLimitationExceeded += MediaCapture_RecordLimitationExceeded;
// _mediaCapture.Failed += MediaCapture_Failed;
var settings = new MediaCaptureInitializationSettings { Videodeviceid = cameraDevice.Id };
settings.StreamingCaptureMode = Windows.Media.Capture.StreamingCaptureMode.Video;
// Initialize MediaCapture
try
{
await _mediaCapture.InitializeAsync(settings);
_isInitialized = true;
}
catch (UnauthorizedAccessException)
{
Debug.WriteLine("The app was denied access to the camera");
throw new Exception("The app was denied access to the camera");
}
}
}
解决方法
我相信这是某种形式的隐私设置,因为该应用程序在作为桌面应用程序运行时可以正常工作。
可能是用户界面权限隔离或会话 0 隔离。有一些 Vista 时代的黑客可以尝试绕过它们,这些黑客在 Win10 上可能会也可能不会起作用,但这些黑客都没有被推荐过,而且这些天肯定会皱眉。
最好的解决方案可能是用自动启动的应用程序替换 Win32 服务。如果您绝对需要 Win32 服务,那么您可能需要使用较低级别的 API。