// 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();
            }
        }
        // Must be called on the UI thread
        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)
            {
                MessageDialog dialog = new MessageDialog(string.Format("Failed to start the camera: {0}", e.Message));
                await dialog.ShowAsync();
            }

            m_initializing = false;
        }
Example #3
0
        public static async Task <ContinuousAutoFocus> StartAsync(FocusControl control)
        {
            var autoFocus = new ContinuousAutoFocus(control);

#if WINDOWS_PHONE_APP
            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(); });
            }
#else
            if (control.SupportedPresets.Contains(FocusPreset.Auto))
            {
                await control.SetPresetAsync(FocusPreset.Auto, /*completeBeforeFocus*/ true);
            }
#endif

            return(autoFocus);
        }