从本机C#代码(Windows 10)打开摄像头用户界面
我需要从cordova javascript调用WinRT中的本机函数。此函数应以本机方式(使用C#)打开CameraUI 限制是无法使用摄像头插件打开摄像头。它必须从本机代码打开 下面的代码是WinRT组件。当从Cordova Javascript调用它(Test.TestController.openCamera();)时,它不会打开摄像头。但是,如果我从本机windows应用程序调用它,它将能够打开摄像头。是否有解决方案来解决此问题 总之,调用本机WinRT组件以打开自定义相机UI所需的Windows混合应用程序(JavaScript) 名称空间测试 { 公共密封类测试控制器 { 公共静态异步void openCamera() { System.Diagnostics.Debug.WriteLine(“调用openCamera”); 预览预览=新建预览(); preview.openCam(); }从本机C#代码(Windows 10)打开摄像头用户界面,windows,cordova,windows-runtime,hybrid-mobile-app,Windows,Cordova,Windows Runtime,Hybrid Mobile App,我需要从cordova javascript调用WinRT中的本机函数。此函数应以本机方式(使用C#)打开CameraUI 限制是无法使用摄像头插件打开摄像头。它必须从本机代码打开 下面的代码是WinRT组件。当从Cordova Javascript调用它(Test.TestController.openCamera();)时,它不会打开摄像头。但是,如果我从本机windows应用程序调用它,它将能够打开摄像头。是否有解决方案来解决此问题 总之,调用本机WinRT组件以打开自定义相机UI所需的W
}
public sealed class Preview
{
MediaCapture mediaCapture;
public async void openCam()
{
await StartPreviewAsync();
}
private async Task StartPreviewAsync()
{
try
{
CaptureElement preview = new CaptureElement();
StackPanel stackPanel = new StackPanel { Orientation = Orientation.Vertical };
preview.Stretch = Windows.UI.Xaml.Media.Stretch.UniformToFill;
preview.Width = 400;
preview.Height = 650;
stackPanel.Width = 400;
stackPanel.Height = 650;
stackPanel.Children.Add(preview);
//TODO : Instead of using Windows.Current , need to find a way to get reference of the main application view.
//Window.Current.Content = stackPanel;
CoreApplicationView newView = CoreApplication.CreateNewView();
int newViewId = 0;
await newView.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
Window.Current.Content = stackPanel;
// You have to activate the window in order to show it later.
Window.Current.Activate();
newViewId = ApplicationView.GetForCurrentView().Id;
});
bool viewShown = await ApplicationViewSwitcher.TryShowAsStandaloneAsync(newViewId);
var _deviceInformation = await GetCameraDeviceInfoAsync(Windows.Devices.Enumeration.Panel.Back);
var settings = new MediaCaptureInitializationSettings();
settings.StreamingCaptureMode = StreamingCaptureMode.Video;
settings.PhotoCaptureSource = PhotoCaptureSource.VideoPreview;
settings.AudioDeviceId = "";
if (_deviceInformation != null)
settings.VideoDeviceId = _deviceInformation.Id;
mediaCapture = new MediaCapture();
await mediaCapture.InitializeAsync(settings);
mediaCapture.SetPreviewRotation(VideoRotation.Clockwise90Degrees);
preview.Source = mediaCapture;
await mediaCapture.StartPreviewAsync();
}
catch (UnauthorizedAccessException)
{
// This will be thrown if the user denied access to the camera in privacy settings
System.Diagnostics.Debug.WriteLine("The app was denied access to the camera");
}
catch (Exception)
{
System.Diagnostics.Debug.WriteLine("MediaCapture initialization failed.");
}
}
private static async Task<DeviceInformation> GetCameraDeviceInfoAsync(Windows.Devices.Enumeration.Panel desiredPanel)
{
DeviceInformation device = (await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture))
.FirstOrDefault(d => d.EnclosureLocation != null && d.EnclosureLocation.Panel == desiredPanel);
if (device == null)
{
throw new InvalidOperationException(string.Format(System.Globalization.CultureInfo.InvariantCulture, "No suitable devices found for the camera of type {0}.", desiredPanel));
}
return device;
}
}
}
公开密封类预览
{
媒体捕获媒体捕获;
公共异步void openCam()
{
等待StartPreviewSync();
}
专用异步任务StartPreviewSync()
{
尝试
{
CaptureElement预览=新建CaptureElement();
StackPanel StackPanel=新StackPanel{Orientation=Orientation.Vertical};
preview.Stretch=Windows.UI.Xaml.Media.Stretch.UniformToFill;
预览。宽度=400;
预览。高度=650;
堆叠面板。宽度=400;
stackPanel.高度=650;
stackPanel.Children.Add(预览);
//TODO:不使用Windows.Current,需要找到获取主应用程序视图引用的方法。
//Window.Current.Content=stackPanel;
CoreApplicationView newView=CoreApplication.CreateNewView();
int newViewId=0;
等待newView.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,()=>
{
Window.Current.Content=stackPanel;
//您必须激活该窗口,以便稍后显示。
Window.Current.Activate();
newViewId=ApplicationView.GetForCurrentView().Id;
});
bool VIEWSHOWED=等待应用程序ViewSwitcher.TryShowAsStandaloneAsync(newViewId);
var_deviceInformation=wait getCameradeviceInfo异步(Windows.Devices.Enumeration.Panel.Back);
var settings=新的MediaCaptureInitializationSettings();
settings.StreamingCaptureMode=StreamingCaptureMode.Video;
settings.PhotoCaptureSource=PhotoCaptureSource.VideoPreview;
settings.AudioDeviceId=“”;
如果(_deviceInformation!=null)
settings.VideoDeviceId=\u deviceInformation.Id;
mediaCapture=新的mediaCapture();
等待mediaCapture.InitializeAsync(设置);
mediaCapture.SetPreviewRotation(视频旋转,顺时针90度);
preview.Source=mediaCapture;
等待mediaCapture.StartPreviewSync();
}
捕获(未经授权的访问例外)
{
//如果用户在“隐私设置”中拒绝访问相机,将引发此问题
System.Diagnostics.Debug.WriteLine(“应用程序被拒绝访问摄像头”);
}
捕获(例外)
{
System.Diagnostics.Debug.WriteLine(“MediaCapture初始化失败”);
}
}
专用静态异步任务GetCameradeviceInfo异步(Windows.Devices.Enumeration.Panel desiredPanel)
{
DeviceInformation设备=(等待DeviceInformation.FindAllAsync(DeviceClass.VideoCapture))
.FirstOrDefault(d=>d.EnclosureLocation!=null&&d.EnclosureLocation.Panel==desiredPanel);
如果(设备==null)
{
抛出新的InvalidOperationException(string.Format(System.Globalization.CultureInfo.InvariantCulture),“未找到适合{0}类型摄像机的设备”,desiredPanel));
}
返回装置;
}
}
}
}
public sealed class Preview
{
MediaCapture mediaCapture;
public async void openCam()
{
await StartPreviewAsync();
}
private async Task StartPreviewAsync()
{
try
{
CaptureElement preview = new CaptureElement();
StackPanel stackPanel = new StackPanel { Orientation = Orientation.Vertical };
preview.Stretch = Windows.UI.Xaml.Media.Stretch.UniformToFill;
preview.Width = 400;
preview.Height = 650;
stackPanel.Width = 400;
stackPanel.Height = 650;
stackPanel.Children.Add(preview);
//TODO : Instead of using Windows.Current , need to find a way to get reference of the main application view.
//Window.Current.Content = stackPanel;
CoreApplicationView newView = CoreApplication.CreateNewView();
int newViewId = 0;
await newView.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
Window.Current.Content = stackPanel;
// You have to activate the window in order to show it later.
Window.Current.Activate();
newViewId = ApplicationView.GetForCurrentView().Id;
});
bool viewShown = await ApplicationViewSwitcher.TryShowAsStandaloneAsync(newViewId);
var _deviceInformation = await GetCameraDeviceInfoAsync(Windows.Devices.Enumeration.Panel.Back);
var settings = new MediaCaptureInitializationSettings();
settings.StreamingCaptureMode = StreamingCaptureMode.Video;
settings.PhotoCaptureSource = PhotoCaptureSource.VideoPreview;
settings.AudioDeviceId = "";
if (_deviceInformation != null)
settings.VideoDeviceId = _deviceInformation.Id;
mediaCapture = new MediaCapture();
await mediaCapture.InitializeAsync(settings);
mediaCapture.SetPreviewRotation(VideoRotation.Clockwise90Degrees);
preview.Source = mediaCapture;
await mediaCapture.StartPreviewAsync();
}
catch (UnauthorizedAccessException)
{
// This will be thrown if the user denied access to the camera in privacy settings
System.Diagnostics.Debug.WriteLine("The app was denied access to the camera");
}
catch (Exception)
{
System.Diagnostics.Debug.WriteLine("MediaCapture initialization failed.");
}
}
private static async Task<DeviceInformation> GetCameraDeviceInfoAsync(Windows.Devices.Enumeration.Panel desiredPanel)
{
DeviceInformation device = (await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture))
.FirstOrDefault(d => d.EnclosureLocation != null && d.EnclosureLocation.Panel == desiredPanel);
if (device == null)
{
throw new InvalidOperationException(string.Format(System.Globalization.CultureInfo.InvariantCulture, "No suitable devices found for the camera of type {0}.", desiredPanel));
}
return device;
}
}