我需要扫描条形码,如果用户可以看到他的目标,这显然会更好一些。虽然我明确将后置摄像头设置为捕捉设备,但我的表面始终可以启用前置摄像头。
我调试了它,它实际上选择了后置摄像头,将它的id提供给MediaCapture
对象进行初始化但最终无效。
var cameraDevices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);
var backfacing =
cameraDevices.FirstOrDefault(c => c.EnclosureLocation.Panel == Panel.Back);
var preferred = backfacing ?? cameraDevices.FirstOrDefault();
// TODO: Change into UI feedback instead
if (preferred == null) throw new Exception("No camera available.");
_capture = new MediaCapture();
_capture.Failed += CaptureOnFailed;
_capture.RecordLimitationExceeded += CaptureOnRecordLimitationExceeded;
try
{
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
_request = new DisplayRequest();
_request.RequestActive();
DisplayInformation.AutoRotationPreferences = DisplayOrientations.Landscape;
});
await _capture.InitializeAsync(
new MediaCaptureInitializationSettings()
{
VideoDeviceId = preferred.Id,
SourceGroup = selectedGroup,
MemoryPreference = MediaCaptureMemoryPreference.Cpu,
StreamingCaptureMode = StreamingCaptureMode.Video
});
var colorFrameSource = _capture.FrameSources[colorInfo.Id];
var preferredFormat = colorFrameSource.SupportedFormats.FirstOrDefault(format => format.VideoFormat.Width >= 720);
if (preferredFormat == null)
{
throw new Exception("Format not supported.");
}
await colorFrameSource.SetFormatAsync(preferredFormat);
_frameReader = await _capture.CreateFrameReaderAsync(colorFrameSource);
_frameReader.FrameArrived += FrameReaderOnFrameArrived;
await _frameReader.StartAsync();
}
catch (UnauthorizedAccessException)
{
// No permission for camera usage
}
catch (Exception ex)
{
Debugger.Break();
}
为什么这样?我该怎么做才能改变它?我没有找到有关此事的任何信息。
答案 0 :(得分:0)
您只需通过以下代码行选择设备前面板上的摄像头:
var backfacing = cameraDevices.FirstOrDefault(c => c.EnclosureLocation.Panel == Panel.Front);
因此它使用前置摄像头。如果您想使用后面板上的相机,则应将EnclosureLocation.Panel
属性设置为Windows.Devices.Enumeration.Panel.Back
var frameSourceGroups = await MediaFrameSourceGroup.FindAllAsync();
var selectedGroupObjects = frameSourceGroups.Select(group =>
new
{
sourceGroup = group,
colorSourceInfo = group.SourceInfos.FirstOrDefault((sourceInfo) =>
{
return sourceInfo.MediaStreamType == MediaStreamType.VideoPreview
&& sourceInfo.SourceKind == MediaFrameSourceKind.Color
&& sourceInfo.DeviceInformation?.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Back;
})
}).Where(t => t.colorSourceInfo != null)
.FirstOrDefault();
详细信息请参考official sample,它提供了可以很好地使用前后相机的样本。