gpt4 book ai didi

c# - 如何在 Windows 10 通用应用程序中切换到后置摄像头

转载 作者:太空宇宙 更新时间:2023-11-03 19:54:18 25 4
gpt4 key购买 nike

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

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

最佳答案

您需要使用所需相机设备的 VideoDeviceId(从 DeviceInformationCollection 设备获取)再次运行 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();

关于c# - 如何在 Windows 10 通用应用程序中切换到后置摄像头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35462501/

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