public SoftwareBitmap ProcessFrame(MediaFrameReference frame) { var softwareBitmap = FrameRenderer.ConvertToDisplayableImage(frame?.VideoMediaFrame); var softwareBitmap2 = softwareBitmap != null?SoftwareBitmap.Copy(softwareBitmap) : null; if (softwareBitmap != null) { softwareBitmap = Interlocked.Exchange(ref _backBuffer, softwareBitmap); var task = _imageElement.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async() => { if (_taskRunning) { return; } _taskRunning = true; SoftwareBitmap latestBitmap; while ((latestBitmap = Interlocked.Exchange(ref _backBuffer, null)) != null) { var imageSource = (SoftwareBitmapSource)_imageElement.Source; await imageSource.SetBitmapAsync(latestBitmap); latestBitmap.Dispose(); } _taskRunning = false; }); } return(softwareBitmap2); }
public Scenario2_FindAvailableSourceGroups() { InitializeComponent(); viewModel = new MainFrameViewModel(); DataContext = viewModel; _logger = new SimpleLogger(outputTextBlock); _frameRenderer = new FrameRenderer(PreviewImage); }
public Scenario2_FindAvailableSourceGroups() { InitializeComponent(); _logger = new SimpleLogger(outputTextBlock); _frameRenderer = new FrameRenderer(PreviewImage); _logger2 = new SimpleLogger(outputTextBlock2); _frameRenderer2 = new FrameRenderer(PreviewImage2); }
public Scenario2_ImageOperations() { this.InitializeComponent(); previewRenderer = new FrameRenderer(PreviewImage); outputRenderer = new FrameRenderer(OutputImage); fpsTimer = new DispatcherTimer() { Interval = TimeSpan.FromSeconds(1) }; fpsTimer.Tick += UpdateFps; }
public Scenario1_ExampleOperations() { this.InitializeComponent(); this.previewRenderer = new FrameRenderer(this.PreviewImage); this.outputRenderer = new FrameRenderer(this.OutputImage); this.fpsTimer = new DispatcherTimer() { Interval = TimeSpan.FromSeconds(1) }; this.fpsTimer.Tick += this.UpdateFPS; }
public Scenario1_ExampleOperations() { this.InitializeComponent(); _previewRenderer = new FrameRenderer(PreviewImage); _outputRenderer = new FrameRenderer(OutputImage); _helper = new OpenCVHelper(); _FPSTimer = new DispatcherTimer() { Interval = TimeSpan.FromSeconds(1) }; _FPSTimer.Tick += UpdateFPS; }
public async void ProcessFrame(MediaFrameReference frame) { var softwareBitmap = FrameRenderer.ConvertToDisplayableImage(frame?.VideoMediaFrame); _bytes = await Convert(softwareBitmap); if (softwareBitmap != null) { // Swap the processed frame to _backBuffer and trigger UI thread to render it softwareBitmap = Interlocked.Exchange(ref _backBuffer, softwareBitmap); // // UI thread always reset _backBuffer before using it. Unused bitmap should be disposed. softwareBitmap?.Dispose(); // Changes to xaml ImageElement must happen in UI thread through Dispatcher var task = _imageElement.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async() => { // Don't let two copies of this task run at the same time. if (_taskRunning) { return; } _taskRunning = true; // Keep draining frames from the backbuffer until the backbuffer is empty. SoftwareBitmap latestBitmap; while ((latestBitmap = Interlocked.Exchange(ref _backBuffer, null)) != null) { _bytes = await Convert(latestBitmap); var imageSource = (SoftwareBitmapSource)_imageElement.Source; await imageSource.SetBitmapAsync(latestBitmap); latestBitmap.Dispose(); } _taskRunning = false; }); } }
/// <summary> /// Starts reading from the newly selected source. /// </summary> private async void SourceComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { await StopReaderAsync(); if (SourceComboBox.SelectedItem != null) { await StartReaderAsync(); IEnumerable <FrameFormatModel> formats = null; if (_mediaCapture != null && _source != null) { formats = _source.SupportedFormats .Where(format => FrameRenderer.GetSubtypeForFrameReader(_source.Info.SourceKind, format) != null) .Select(format => new FrameFormatModel(format)); } FormatComboBox.ItemsSource = formats; } }
private async void SourceComboBox_SelectionChanged2(object sender, SelectionChangedEventArgs e) { await StopReaderAsync2(); if (SourceComboBox2.SelectedItem != null) { await StartReaderAsync2(); // Get the formats supported by the selected source into the FormatComboBox. IEnumerable <FrameFormatModel> formats = null; if (_mediaCapture2 != null && _source2 != null) { // Limit ourselves to formats that we can render. formats = _source2.SupportedFormats .Where(format => FrameRenderer.GetSubtypeForFrameReader(_source2.Info.SourceKind, format) != null) .Select(format => new FrameFormatModel(format)); } FormatComboBox2.ItemsSource = formats; } }
/// <summary> /// Creates a frame reader from the current frame source and registers to handle its frame events. /// </summary> private async Task CreateReaderAsync() { await InitializeCaptureAsync(); UpdateFrameSource(); if (_source != null) { string requestedSubtype = FrameRenderer.GetSubtypeForFrameReader(_source.Info.SourceKind, _source.CurrentFormat); if (requestedSubtype != null) { _reader = await _mediaCapture.CreateFrameReaderAsync(_source, requestedSubtype); _reader.FrameArrived += Reader_FrameArrived; _logger.Log($"Reader created on source: {_source.Info.Id}"); } else { _logger.Log($"Cannot render current format on source: {_source.Info.Id}"); } } }
private async Task CreateReaderAsync2() { await InitializeCaptureAsync2(); UpdateFrameSource2(); if (_source2 != null) { // Ask the MediaFrameReader to use a subtype that we can render. string requestedSubtype = FrameRenderer.GetSubtypeForFrameReader(_source2.Info.SourceKind, _source2.CurrentFormat); if (requestedSubtype != null) { _reader2 = await _mediaCapture2.CreateFrameReaderAsync(_source2, requestedSubtype); _reader2.FrameArrived += Reader_FrameArrived2; _logger2.Log($"Reader created on source: {_source2.Info.Id}"); } else { _logger.Log($"Cannot render current format on source: {_source2.Info.Id}"); } } }
public void ProcessFrame(MediaFrameReference frame) { var softwareBitmap = FrameRenderer.ConvertToDisplayableImage(frame?.VideoMediaFrame); RenderFrame(softwareBitmap); }
public Scenario2_FindAvailableSourceGroups() { InitializeComponent(); _logger = new SimpleLogger(outputTextBlock); _frameRenderer = new FrameRenderer(PreviewImage); }
/// <summary> /// Switches to the next camera source and starts reading frames. /// </summary> private async Task PickNextMediaSourceWorkerAsync() { await CleanupMediaCaptureAsync(); var allGroups = await MediaFrameSourceGroup.FindAllAsync(); if (allGroups.Count == 0) { _logger.Log("No source groups found."); return; } // Pick next group in the array after each time the Next button is clicked. _groupSelectionIndex = (_groupSelectionIndex + 1) % allGroups.Count; var selectedGroup = allGroups[_groupSelectionIndex]; _logger.Log($"Found {allGroups.Count} groups and selecting index [{_groupSelectionIndex}]: {selectedGroup.DisplayName}"); try { // Initialize MediaCapture with selected group. // This can raise an exception if the source no longer exists, // or if the source could not be initialized. await InitializeMediaCaptureAsync(selectedGroup); } catch (Exception exception) { _logger.Log($"MediaCapture initialization error: {exception.Message}"); await CleanupMediaCaptureAsync(); return; } // Set up frame readers, register event handlers and start streaming. var startedKinds = new HashSet <MediaFrameSourceKind>(); foreach (MediaFrameSource source in _mediaCapture.FrameSources.Values) { MediaFrameSourceKind kind = source.Info.SourceKind; // Ignore this source if we already have a source of this kind. if (startedKinds.Contains(kind)) { continue; } // Look for a format which the FrameRenderer can render. string requestedSubtype = null; foreach (MediaFrameFormat format in source.SupportedFormats) { requestedSubtype = FrameRenderer.GetSubtypeForFrameReader(kind, format); if (requestedSubtype != null) { // Tell the source to use the format we can render. await source.SetFormatAsync(format); break; } } if (requestedSubtype == null) { // No acceptable format was found. Ignore this source. continue; } MediaFrameReader frameReader = await _mediaCapture.CreateFrameReaderAsync(source, requestedSubtype); frameReader.FrameArrived += FrameReader_FrameArrived; _sourceReaders.Add(frameReader); MediaFrameReaderStartStatus status = await frameReader.StartAsync(); if (status == MediaFrameReaderStartStatus.Success) { _logger.Log($"Started {kind} reader."); startedKinds.Add(kind); } else { _logger.Log($"Unable to start {kind} reader. Error: {status}"); } } if (startedKinds.Count == 0) { _logger.Log($"No eligible sources in {selectedGroup.DisplayName}."); } }