public async Task StartStreamAsync(bool isForRealTimeProcessing = false) { try { if (captureManager == null || captureManager.CameraStreamState == CameraStreamState.Shutdown || captureManager.CameraStreamState == CameraStreamState.NotStreaming) { if (captureManager != null) { captureManager.Dispose(); } captureManager = new MediaCapture(); MediaCaptureInitializationSettings settings = new MediaCaptureInitializationSettings(); var allCameras = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture); await captureManager.InitializeAsync(settings); await SetVideoEncodingToHighestResolution(isForRealTimeProcessing); this.webCamCaptureElement.Source = captureManager; } if (captureManager.CameraStreamState == CameraStreamState.NotStreaming) { if (this.faceTracker == null) { this.faceTracker = await FaceTracker.CreateAsync(); } this.videoProperties = this.captureManager.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview) as VideoEncodingProperties; await captureManager.StartPreviewAsync(); if (this.frameProcessingTimer != null) { this.frameProcessingTimer.Cancel(); frameProcessingSemaphore.Release(); } TimeSpan timerInterval = TimeSpan.FromMilliseconds(66); //15fps this.frameProcessingTimer = ThreadPoolTimer.CreatePeriodicTimer(new TimerElapsedHandler(ProcessCurrentVideoFrame), timerInterval); this.cameraControlSymbol.Symbol = Symbol.Camera; this.webCamCaptureElement.Visibility = Visibility.Visible; } } catch (Exception ex) { await CoreUtil.GenericApiCallExceptionHandler(ex, "Lỗi khởi động camera."); } }
private async void OnDataContextChanged(FrameworkElement sender, DataContextChangedEventArgs args) { ImageAnalyzer dataContext = this.DataContext as ImageAnalyzer; if (this.currentImage != dataContext) { this.currentImage = dataContext; } else { // Windows sometimes fires multiple DataContextChanged events. // If we are here that is one of those cases, and since we already set // the data context to this value we can ignore it return; } foreach (var child in this.hostGrid.Children.Where(c => !(c is Image)).ToArray()) { this.hostGrid.Children.Remove(child); } // remove the current source this.bitmapImage.UriSource = null; if (dataContext != null) { try { if (dataContext.ImageUrl != null) { this.bitmapImage.UriSource = new Uri(dataContext.ImageUrl); } else if (dataContext.GetImageStreamCallback != null) { await this.bitmapImage.SetSourceAsync((await dataContext.GetImageStreamCallback()).AsRandomAccessStream()); } } catch (Exception ex) { // If we fail to load the image we will just not display it this.bitmapImage.UriSource = null; if (this.ShowDialogOnApiErrors) { await CoreUtil.GenericApiCallExceptionHandler(ex, "Error loading captured image."); } } } }
public async Task ProcessingStreamAsync() { try { while (isProcessingLoopInProgress) { await ProcessCurrentVideoFrame(); await Task.Delay(TimeDelay); } } catch (Exception ex) { await CoreUtil.GenericApiCallExceptionHandler(ex, "Lỗi khởi động camera."); } }