前台加入MediaPlayerElement控件显示摄像头数据
<Grid>
<MediaPlayerElement Name="UIMediaPlayerElement" Stretch="Uniform" AreTransportControlsEnabled="False" Canvas.ZIndex="-1" MaxWidth="720" MaxHeight="720" Grid.Column="0" />
</Grid>
后台代码,将摄像头数据输出到MediaPlayerElement控件
private async Task StartPreviewAsync()
{
Debug.WriteLine("StartPreview");
// Find the sources
var allGroups = await MediaFrameSourceGroup.FindAllAsync();
List<MediaFrameSourceGroup> _mediaFrameSourceGroupList = allGroups.Where(group => group.SourceInfos.Any(sourceInfo => sourceInfo.SourceKind == MediaFrameSourceKind.Color
&& (sourceInfo.MediaStreamType == MediaStreamType.VideoPreview
|| sourceInfo.MediaStreamType == MediaStreamType.VideoRecord))).ToList();
var _selectedMediaFrameSourceGroup = _mediaFrameSourceGroupList[0];
// Create MediaCapture and its settings
MediaCapture _mediaCapture= new MediaCapture();
var settings = new MediaCaptureInitializationSettings
{
SourceGroup = _selectedMediaFrameSourceGroup,
PhotoCaptureSource = PhotoCaptureSource.Auto,
MemoryPreference = MediaCaptureMemoryPreference.Cpu,
StreamingCaptureMode = StreamingCaptureMode.Video
};
// Initialize MediaCapture
await _mediaCapture.InitializeAsync(settings);
MediaFrameSource _selectedMediaFrameSource = _mediaCapture.FrameSources.FirstOrDefault(source => source.Value.Info.MediaStreamType == MediaStreamType.VideoPreview
&& source.Value.Info.SourceKind == MediaFrameSourceKind.Color).Value;
if (_selectedMediaFrameSource == null)
{
_selectedMediaFrameSource = _mediaCapture.FrameSources.FirstOrDefault(source => source.Value.Info.MediaStreamType == MediaStreamType.VideoRecord
&& source.Value.Info.SourceKind == MediaFrameSourceKind.Color).Value;
}
// if no preview stream are available, bail
if (_selectedMediaFrameSource == null)
{
return;
}
var _mediaPlayer = new MediaPlayer();
_mediaPlayer.RealTimePlayback = true;
_mediaPlayer.AutoPlay = true;
_mediaPlayer.Source = MediaSource.CreateFromMediaFrameSource(_selectedMediaFrameSource);
UIMediaPlayerElement.SetMediaPlayer(_mediaPlayer);
UIMediaPlayerElement.Visibility = Visibility.Visible;
}
2