private async Task InitiateCameraCaptureObject(Panel deviceLocation) { try { if (_bInitializingCamera || _cameraCapture != null) return; _bInitializingCamera = true; await InitCaptureSettings(deviceLocation); _cameraCapture = new MediaCapture(); await _cameraCapture.InitializeAsync(_captureInitSettings); //Enable QR Detector if (_qrDetectionModeEnabled) { var formats = _cameraCapture.VideoDeviceController.GetAvailableMediaStreamProperties(MediaStreamType.Photo); var format = (VideoEncodingProperties)formats.OrderBy((item) => { var props = (VideoEncodingProperties)item; return Math.Abs(props.Height - ActualWidth) + Math.Abs(props.Width - ActualHeight); }).First(); await _cameraCapture.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.Photo, format); var definition = new LumiaAnalyzerDefinition(ColorMode.Yuv420Sp, format.Width >= format.Height ? format.Width : format.Height, AnalyzeImage); await _cameraCapture.AddEffectAsync(MediaStreamType.VideoPreview, definition.ActivatableClassId, definition.Properties); _barcodeReader = _barcodeReader ?? new BarcodeReader { Options = new DecodingOptions { PossibleFormats = new[] { BarcodeFormat.QR_CODE, BarcodeFormat.CODE_128 }, TryHarder = true } }; } PhotoPreview.Source = _cameraCapture; await _cameraCapture.StartPreviewAsync(); _cameraCapture.Failed += CameraCaptureOnFailed; _scannerAutoFocus = await ScannerAutoFocus.StartAsync(_cameraCapture.VideoDeviceController.FocusControl); _cameraCapture.SetPreviewRotation(VideoRotation.Clockwise90Degrees); } catch (Exception ex) { WindowsPhoneUtils.Log(ex.Message); } _bInitializingCamera = false; }
private async Task DisposeCaptureAsync() { _cameraCapture.Failed -= CameraCaptureOnFailed; await _cameraCapture.ClearEffectsAsync(MediaStreamType.VideoPreview); PhotoPreview.Source = null; _barcodeReader = null; if (_scannerAutoFocus != null) { _scannerAutoFocus.Dispose(); _scannerAutoFocus = null; } MediaCapture capture; lock (this) { capture = _cameraCapture; _cameraCapture = null; } if (capture != null) { capture.Failed -= CameraCaptureOnFailed; capture.Dispose(); } }
public static async Task<ScannerAutoFocus> StartAsync(FocusControl control) { var autoFocus = new ScannerAutoFocus(control); AutoFocusRange range; if (control.SupportedFocusRanges.Contains(AutoFocusRange.FullRange)) { range = AutoFocusRange.FullRange; } else if (control.SupportedFocusRanges.Contains(AutoFocusRange.Normal)) { range = AutoFocusRange.Normal; } else { // Auto-focus disabled return autoFocus; } FocusMode mode; if (control.SupportedFocusModes.Contains(FocusMode.Continuous)) { mode = FocusMode.Continuous; } else if (control.SupportedFocusModes.Contains(FocusMode.Single)) { mode = FocusMode.Single; } else { // Auto-focus disabled return autoFocus; } if (mode == FocusMode.Continuous) { // True continuous auto-focus var settings = new FocusSettings() { AutoFocusRange = range, Mode = mode, WaitForFocus = false, DisableDriverFallback = false }; control.Configure(settings); await control.FocusAsync(); } else { // Simulated continuous auto-focus var settings = new FocusSettings() { AutoFocusRange = range, Mode = mode, WaitForFocus = true, DisableDriverFallback = false }; control.Configure(settings); var ignore = Task.Run(async () => { await autoFocus.DriveAutoFocusAsync(); }); } return autoFocus; }