【问题标题】:How to switch to Rear Camera in Windows 10 Universal App如何在 Windows 10 通用应用程序中切换到后置摄像头
【发布时间】:2017-08-17 17:22:48
【问题描述】:

我似乎找不到 MediaCapture 类的属性,该属性允许我检测 Surface 中的后置和前置摄像头,并在 Combobox 项可用时切换到它。这是我当前的设备设置。

        mc = new MediaCapture();
        DeviceInformationCollection devices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);

        // Use the front camera if found one
        if (devices == null) return;
        var info = devices[0];

        //var rearCamera;
        //DeviceInformation info2 = devices[1];

        foreach (var devInfo in devices)
        {
            if (devInfo.Name.ToLowerInvariant().Contains("front"))
            {

                info = devInfo;
                //info2 = devInfo;
                //frontCam = true;
                //backCam = true;
                camera = true;
                //continue;
            }
            if (devInfo.Name.ToLowerInvariant().Contains("back"))
            {
                var rearCamera = devices[1];
                rearCamera = devInfo;
            }
        }

        await mc.InitializeAsync(new MediaCaptureInitializationSettings
            {
            MediaCategory = MediaCategory.Communications,
            StreamingCaptureMode = StreamingCaptureMode.AudioAndVideo,
            VideoDeviceId = info.Id,
                //VideoDeviceId = info2.Id
            });


        DisplayInformation displayInfo = DisplayInformation.GetForCurrentView();
        displayInfo.OrientationChanged += DisplayInfo_OrientationChanged;
        DisplayInfo_OrientationChanged(displayInfo, null);

        stream = new InMemoryRandomAccessStream();
        llmr = await mc.PrepareLowLagRecordToStreamAsync(MediaEncodingProfile.CreateMp4(VideoEncodingQuality.Auto), stream);
        await llmr.StartAsync();
        await llmr.StopAsync();


        CaptureElement.Source = mc;
        CaptureElement.FlowDirection = camera ? FlowDirection.RightToLeft : FlowDirection.LeftToRight;
        CaptureStack.Visibility = Visibility.Visible;
        CameraErrorTextBlock.Visibility = Visibility.Collapsed;
        RecordProgress.Visibility = Visibility.Visible;
        CaptureGrid.Visibility = Visibility.Visible;
        CancelButton.HorizontalAlignment = HorizontalAlignment.Right;
        //CaptureElement.FlowDirection = FlowDirection.LeftToRight;

        // prepare low lag recording
        stream = new InMemoryRandomAccessStream();
        llmr = await mc.PrepareLowLagRecordToStreamAsync(MediaEncodingProfile.CreateMp4(VideoEncodingQuality.Auto), stream);



        await mc.StartPreviewAsync();

【问题讨论】:

    标签: c# uwp windows-phone


    【解决方案1】:

    您需要使用所需相机设备(从 DeviceInformationCollection 设备获取)的 VideoDeviceId 再次运行 MediaCapture.InitializeAsync。然后将 CaptureElement.Source 设置为新的 MediaCapture。

    var devices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);
    
    if (devices.Count > 0)
                {
                    if (devices.Count >= 2)
                    {
                        var device = devices.FirstOrDefault(d => d.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Front);
                    }
                }
    
    
    var frontVideoId = device.Id;
    await mc.InitializeAsync(new MediaCaptureInitializationSettings
                {
                MediaCategory = MediaCategory.Communications,
                StreamingCaptureMode = StreamingCaptureMode.AudioAndVideo,
                VideoDeviceId = frontVideoId
                });
    CaptureElement.Source = mc;
    await mc.StartPreviewAsync();
    

    【讨论】:

    • 你能用上面的代码发布一个简单的例子吗?
    • 示例添加,EnclosureLocation 会告诉你摄像头的位置
    • 是的,但我想要两个相机的代码,以便使用 Combobx Selected Index 切换它们。
    • 此函数的核心在示例代码中。只需获取相机设备信息列表,提供给 ComboBox itemssource,在 ComboBox 的事件处理程序上,使用我提供的代码初始化相机。
    • 是的,我已经尝试过您的代码,但是 var device = devices.FirstOrDefault(d => d.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Front);在 if(Statment) 内部被 decleread 并且当我像这样在外部输入时 ->var frontVideoId = device.Id;它不识别 device.ID(error) 请你也给我一个例子,我是如何用 combobx 完成的,我是 UWP 的新手..
    【解决方案2】:

    您应该使用 DeviceInformation 的 "EnclosureLocation" 属性。

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

    Windows.Devices.Enumeration.Panel.Back 将是后置摄像头的 EnclosureLocation。

    【讨论】:

    • 我已经尝试过了,但它总是前置摄像头,我还想使用 Combobox SelectedItem 访问后置摄像头
    • 您可以尝试访问 allVideoDevices 来查看每个 DeviceInformation 的 EnclosureLocation。关于组合框,您可以将面板绑定到您的组合框。
    猜你喜欢
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    • 2020-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-15
    • 2015-10-31
    相关资源
    最近更新 更多