// Must be called on the UI thread private async Task DisposeCaptureAsync() { Preview.Source = null; if (autoFocus != null) { autoFocus.Dispose(); autoFocus = null; } MediaCapture mediaCapture; lock (this) { mediaCapture = this.mediaCapture; this.mediaCapture = null; } if (mediaCapture != null) { mediaCapture.Failed -= OnMediaCaptureFailed; await mediaCapture.StopPreviewAsync(); mediaCapture.Dispose(); } }
// Must be called on the UI thread private async Task DisposeCaptureAsync() { Preview.Source = null; if (m_autoFocus != null) { m_autoFocus.Dispose(); m_autoFocus = null; } MediaCapture capture; lock (this) { capture = m_capture; m_capture = null; } if (capture != null) { capture.Failed -= capture_Failed; await capture.StopPreviewAsync(); capture.Dispose(); } }
/// <summary> /// Initializes the UI, camera and start scanning for QR codes. /// </summary> private async Task InitializeAndStartScanningAsync() { SetupUi(); await InitializeCameraAsync(); if (_isInitialized) { FocusControl focusControl = _mediaCapture.VideoDeviceController.FocusControl; if (focusControl != null) { _continuousAutoFocus = await ContinuousAutoFocus.StartAsync(focusControl); } } StartScanningForQrCodeAsync(); }
private async Task InitializeCaptureAsync() { if (m_initializing || (m_capture != null)) { return; } m_initializing = true; try { var settings = new MediaCaptureInitializationSettings { VideoDeviceId = await GetBackOrDefaulCameraIdAsync(), StreamingCaptureMode = StreamingCaptureMode.Video }; var capture = new MediaCapture(); await capture.InitializeAsync(settings); // Select the capture resolution closest to screen resolution var formats = capture.VideoDeviceController.GetAvailableMediaStreamProperties(MediaStreamType.VideoPreview); var format = (VideoEncodingProperties)formats.OrderBy((item) => { var props = (VideoEncodingProperties)item; return Math.Abs(props.Width - this.ActualWidth) + Math.Abs(props.Height - this.ActualHeight); }).First(); await capture.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.VideoPreview, format); // Make the preview full screen var scale = Math.Min(this.ActualWidth / format.Width, this.ActualHeight / format.Height); Preview.Width = format.Width; Preview.Height = format.Height; Preview.RenderTransformOrigin = new Point(.5, .5); Preview.RenderTransform = new ScaleTransform { ScaleX = scale, ScaleY = scale }; BarcodeOutline.Width = format.Width; BarcodeOutline.Height = format.Height; BarcodeOutline.RenderTransformOrigin = new Point(.5, .5); BarcodeOutline.RenderTransform = new ScaleTransform { ScaleX = scale, ScaleY = scale }; // Enable QR code detection var definition = new LumiaAnalyzerDefinition(ColorMode.Yuv420Sp, 640, AnalyzeBitmap); await capture.AddEffectAsync(MediaStreamType.VideoPreview, definition.ActivatableClassId, definition.Properties); // Start preview m_time.Restart(); Preview.Source = capture; await capture.StartPreviewAsync(); capture.Failed += capture_Failed; m_autoFocus = await ContinuousAutoFocus.StartAsync(capture.VideoDeviceController.FocusControl); m_capture = capture; } catch (Exception e) { TextLog.Text = String.Format("Failed to start the camera: {0}", e.Message); } m_initializing = false; }
private async Task InitializeCaptureAsync() { if (isMediaCaptureInitializing || (mediaCapture != null)) { return; } isMediaCaptureInitializing = true; try { var settings = new MediaCaptureInitializationSettings { VideoDeviceId = await GetBackOrDefaulCameraIdAsync(), StreamingCaptureMode = StreamingCaptureMode.Video }; var newMediaCapture = new MediaCapture(); await newMediaCapture.InitializeAsync(settings); // Select the capture resolution closest to screen resolution var formats = newMediaCapture.VideoDeviceController.GetAvailableMediaStreamProperties(MediaStreamType.VideoPreview); var format = (VideoEncodingProperties)formats.OrderBy((item) => { var props = (VideoEncodingProperties)item; return(Math.Abs(props.Width - this.ActualHeight) + Math.Abs(props.Height - this.ActualWidth)); }).First(); Debug.WriteLine("{0} x {1}", format.Width, format.Height); await newMediaCapture.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.VideoPreview, format); // Disable flash control if supported if (newMediaCapture.VideoDeviceController.FlashControl.Supported) { newMediaCapture.VideoDeviceController.FlashControl.Enabled = false; } // Prepare bitmap for reports bitmapWithBarcode = new WriteableBitmap((int)format.Width, (int)format.Height); // Make the preview full screen Preview.Width = this.ActualHeight; Preview.Height = this.ActualWidth; // Enable QR code detection var definition = new LumiaAnalyzerDefinition(ColorMode.Yuv420Sp, Math.Min(format.Width, 800), AnalyzeBitmap); await newMediaCapture.AddEffectAsync(MediaStreamType.VideoPreview, definition.ActivatableClassId, definition.Properties); // Start preview Preview.Source = newMediaCapture; await newMediaCapture.StartPreviewAsync(); newMediaCapture.Failed += OnMediaCaptureFailed; autoFocus = await ContinuousAutoFocus.StartAsync(newMediaCapture.VideoDeviceController.FocusControl); mediaCapture = newMediaCapture; } catch (Exception e) { Debug.WriteLine("Failed to start the camera: {0}", e.Message); } isMediaCaptureInitializing = false; }
private async Task InitializeCaptureAsync() { if (m_initializing || (m_capture != null)) { return; } m_initializing = true; try { var settings = new MediaCaptureInitializationSettings { VideoDeviceId = await GetBackOrDefaulCameraIdAsync(), StreamingCaptureMode = StreamingCaptureMode.Video }; var capture = new MediaCapture(); await capture.InitializeAsync(settings); // Select the capture resolution closest to screen resolution var formats = capture.VideoDeviceController.GetAvailableMediaStreamProperties(MediaStreamType.VideoPreview); var format = (VideoEncodingProperties)formats.OrderBy((item) => { var props = (VideoEncodingProperties)item; return(Math.Abs(props.Width - this.ActualWidth) + Math.Abs(props.Height - this.ActualHeight)); }).First(); await capture.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.VideoPreview, format); // Make the preview full screen var scale = Math.Min(this.ActualWidth / format.Width, this.ActualHeight / format.Height); Preview.Width = format.Width; Preview.Height = format.Height; Preview.RenderTransformOrigin = new Point(.5, .5); Preview.RenderTransform = new ScaleTransform { ScaleX = scale, ScaleY = scale }; BarcodeOutline.Width = format.Width; BarcodeOutline.Height = format.Height; BarcodeOutline.RenderTransformOrigin = new Point(.5, .5); BarcodeOutline.RenderTransform = new ScaleTransform { ScaleX = scale, ScaleY = scale }; // Enable QR code detection var definition = new LumiaAnalyzerDefinition(ColorMode.Yuv420Sp, 640, AnalyzeBitmap); await capture.AddEffectAsync(MediaStreamType.VideoPreview, definition.ActivatableClassId, definition.Properties); // Start preview m_time.Restart(); Preview.Source = capture; await capture.StartPreviewAsync(); capture.Failed += capture_Failed; m_autoFocus = await ContinuousAutoFocus.StartAsync(capture.VideoDeviceController.FocusControl); m_capture = capture; } catch (Exception e) { TextLog.Text = String.Format("Failed to start the camera: {0}", e.Message); } m_initializing = false; }