gpt4 book ai didi

c# - 使用 CaptureElement/MediaCapture 时如何在网络摄像头之间切换?

转载 作者:行者123 更新时间:2023-11-30 22:31:36 25 4
gpt4 key购买 nike

<分区>

我正在尝试提供一个选项来切换用于使用 CaptureElement/MediaCapture 显示预览的网络摄像头。不幸的是,我尝试了调用序列的多种组合,但预览只显示在我使用的第一台设备上。

这是我一直在努力做的事情:

XAML:

<CaptureElement
x:Name="captureElement"
Stretch="UniformToFill" />

C#:

MediaCapture mediaCapture;
DeviceInformationCollection devices;
int currentDevice = 0;

private async void LayoutRoot_Tapped(object sender, Windows.UI.Xaml.Input.TappedEventArgs e)
{
if (devices != null)
{
currentDevice = (currentDevice + 1) % devices.Count;
InitializeWebCam();
}
}

private async void InitializeWebCam()
{
if (devices == null)
{
devices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);
ListDeviceDetails();
}

if (mediaCapture != null)
{
await mediaCapture.StopPreviewAsync();

this.captureElement.Source = null;
}

mediaCapture = new MediaCapture();
await mediaCapture.InitializeAsync(
new MediaCaptureInitializationSettings
{
VideoDeviceId = devices[currentDevice].Id
});

this.captureElement.Source = mediaCapture;
await mediaCapture.StartPreviewAsync();
}

private void ListDeviceDetails()
{
int i = 0;

foreach (var device in devices)
{
Debug.WriteLine("* Device [{0}]", i++);
Debug.WriteLine("EnclosureLocation.InDock: " + device.EnclosureLocation.InDock);
Debug.WriteLine("EnclosureLocation.InLid: " + device.EnclosureLocation.InLid);
Debug.WriteLine("EnclosureLocation.Panel: " + device.EnclosureLocation.Panel);
Debug.WriteLine("Id: " + device.Id);
Debug.WriteLine("IsDefault: " + device.IsDefault);
Debug.WriteLine("IsEnabled: " + device.IsEnabled);
Debug.WriteLine("Name: " + device.Name);
Debug.WriteLine("IsDefault: " + device.IsDefault);

foreach (var property in device.Properties)
{
Debug.WriteLine(property.Key + ": " + property.Value);
}
}
}

偶尔(低于 10% 的次数)切换到第二个摄像头似乎确实有效,然后当我回到第一个时保持全黑。

有时,在我尝试切换相机一次或两次后,应用程序会挂起(它停止响应输入,卡在 App.Run() 中,尽管相机预览会不断刷新)。

其他时候 - 它的工作方式是显示第一台设备的预览,但不会显示另一台设备,当我回到第一台设备时 - 它再次正常工作。

错误?

似乎在任何地方都没有 Dispose 或 Uninitialize 方法。这些是我看到的属性(它是三星的 Build 2011 平板电脑):

* Device [0]
EnclosureLocation.InDock: False
EnclosureLocation.InLid: False
EnclosureLocation.Panel: Front
Id: \\?\USB#VID_2232&PID_1021&MI_00#7&2469C269&0&0000#{e5323777-f976-4f5b-9b55-b94699c46e44}\GLOBAL
IsDefault: False
IsEnabled: True
Name: WebCam SC-20FHM11347N
IsDefault: False
System.ItemNameDisplay: WebCam SC-20FHM11347N
System.Devices.DeviceInstanceId: USB\VID_2232&PID_1021&MI_00\7&2469C269&0&0000
System.Devices.Icon: C:\Windows\System32\DDORes.dll,-2068
System.Devices.InterfaceEnabled: True
System.Devices.IsDefault: False
* Device [1]
EnclosureLocation.InDock: False
EnclosureLocation.InLid: False
EnclosureLocation.Panel: Back
Id: \\?\USB#VID_2232&PID_1022&MI_00#7&27072759&0&0000#{e5323777-f976-4f5b-9b55-b94699c46e44}\GLOBAL
IsDefault: False
IsEnabled: True
Name: WebCam SC-30H2L11449N
IsDefault: False
System.ItemNameDisplay: WebCam SC-30H2L11449N
System.Devices.DeviceInstanceId: USB\VID_2232&PID_1022&MI_00\7&27072759&0&0000
System.Devices.Icon: C:\Windows\System32\DDORes.dll,-2068
System.Devices.InterfaceEnabled: True
System.Devices.IsDefault: False

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