如何使用CaptureElement / MediaCapture当摄像头之间切换? [英] How to switch between webcams when using CaptureElement/MediaCapture?

查看:543
本文介绍了如何使用CaptureElement / MediaCapture当摄像头之间切换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提供一个选项,用于显示使用CaptureElement / MediaCapture预览摄像头之间切换。不幸的是我试图调用序列的多种组合和预览只显示了我使用的第一个装置



这是我一直在努力做的:



XAML:

 < CaptureElement 
X:NAME =captureElement
弹力=UniformToFill/>



C#:

  mediaCapture mediaCapture; 
DeviceInformationCollection装置;
INT currentDevice = 0;

私人异步无效LayoutRoot_Tapped(对象发件人,Windows.UI.Xaml.Input.TappedEventArgs E)
{
如果(设备!= NULL)
{
currentDevice =(currentDevice + 1)%devices.Count;
InitializeWebCam();
}
}

私人异步无效InitializeWebCam()
{
如果(设备== NULL)
{
设备=等待DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);
ListDeviceDetails();
}

如果(mediaCapture!= NULL)
{
等待mediaCapture.StopPreviewAsync();

this.captureElement.Source = NULL;
}

mediaCapture =新MediaCapture();
等待mediaCapture.InitializeAsync(
新MediaCaptureInitializationSettings
{
VideoDeviceId =设备[currentDevice] .ID
});

this.captureElement.Source = mediaCapture;
等待mediaCapture.StartPreviewAsync();
}

私人无效ListDeviceDetails()
{
INT I = 0;

的foreach(在设备的无功设备)
{
的Debug.WriteLine(*设备[{0}]我++);
的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(姓名:+ device.Name);
的Debug.WriteLine(ISDEFAULT:+ device.IsDefault);

的foreach(在device.Properties变种属性)
{
的Debug.WriteLine(property.Key +:+ property.Value);
}
}
}



这似乎是它的工作切换到第二个摄像头过一段时间(下面的时间10%),然后停留,当我再回到第一个全黑的。



有时应用程序后挂起我尝试一次或两次(停止响应输入,它是停留在App.Run(),虽然摄像头预览不断刷新)将相机切换。



其他倍 - 它工作的方式,它示出了从第一装置预览,但不为其他之一,当我回到第一个 - 它再次正常工作与它



错误?



似乎没有成为一个废弃或者未初始化方法的任何地方。这是我看到(这是三星的Build 2011片)的属性:

  *设备[0] 
EnclosureLocation.InDock :假
EnclosureLocation.InLid:假
EnclosureLocation.Panel:前
编号:\\ \USB#VID_2232&放大器; PID_1021&安培; MI_00#7和; 2469C269&安培; 0&安培; 0000#{? e5323777-f976-4f5b-9b55-b94699c46e44} \GLOBAL
ISDEFAULT:假
IsEnabled:真
名称:摄像头SC-20FHM11347N
ISDEFAULT:假
系统。 ItemNameDisplay:摄像头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:真
System.Devices.IsDefault:假
*设备[1]
EnclosureLocation.InDock:假
EnclosureLocation.InLid:假
EnclosureLocation.Panel:返回
编号:\\ \USB#VID_2232&放大器; PID_1022&安培; MI_00#7和; 27072759&安培; 0&安培; 0000#{e5323777-f976 -4f5b-9b55-b94699c46e44} \GLOBAL
ISDEFAULT:假
IsEnabled:真
名称:摄像头SC-30H2L11449N
ISDEFAULT:假
System.ItemNameDisplay:摄像头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:真
System.Devices.IsDefault:假


解决方案

我认为这个问题一定是与开发者预览版本。我没有问题,在消费者预览一种3-网络摄像头之间切换。


I am trying to provide an option to switch between webcams used to display preview using CaptureElement/MediaCapture. Unfortunately I tried multiple combinations of call sequences and the preview only shows up for the first device I use.

This is what I have been trying to do:

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);
        }
    }
}

It seems like it does work to switch to second camera once in a while (below 10% of times) and then stays all black when I go back to the first one.

Sometimes the app hangs after I try to switch the camera once or twice (it stops responding to input, it is stuck in App.Run(), though the camera preview keeps refreshing).

Other times - it works in a way that it shows preview from the first device, but does not for the other one and when I go back to the first one - it works fine with it again.

Bugs?

There does not seem to be a Dispose or Uninitialize method anywhere. These are the properties I see (it's Samsung's Build 2011 tablet):

* 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

解决方案

I think the problem must have been with the Developer Preview build. I have no problem switching between 3 web cams in the Consumer Preview one.

这篇关于如何使用CaptureElement / MediaCapture当摄像头之间切换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆