コード例 #1
0
        /// <summary>
        /// Captures a photo. Photo data is stored to DataContext.ImageStream, and application
        /// is navigated to the preview page after capturing.
        /// </summary>
        private async Task Capture()
        {
            try
            {
                bool goToPreview = false;

                var selfieBitmap = new BitmapImage();

                if (!_capturing)
                {
                    _capturing = true;

                    var stream = new MemoryStream();

                    CameraCaptureSequence sequence = PhotoCaptureDevice.CreateCaptureSequence(1);
                    sequence.Frames[0].CaptureStream = stream.AsOutputStream();

                    await PhotoCaptureDevice.PrepareCaptureSequenceAsync(sequence);

                    await sequence.StartCaptureAsync();

                    selfieBitmap = new BitmapImage();
                    selfieBitmap.SetSource(stream);

                    _capturing = false;

                    // Defer navigation as it will release the camera device and the
                    // following Device calls must still work.
                    goToPreview = true;
                }

                _manuallyFocused = false;

                if (PhotoCaptureDevice.IsFocusRegionSupported(PhotoCaptureDevice.SensorLocation))
                {
                    PhotoCaptureDevice.FocusRegion = null;
                }

                FocusIndicator.SetValue(VisibilityProperty, Visibility.Collapsed);
                PhotoCaptureDevice.SetProperty(KnownCameraPhotoProperties.LockedAutoFocusParameters, AutoFocusParameters.None);

                if (goToPreview)
                {
                    NavigateService.NavigateTo(typeof(PreviewSelfiePage), NavigationParameter.Normal, selfieBitmap);
                }
            }
            catch (Exception e)
            {
                Deployment.Current.Dispatcher.BeginInvoke(() =>
                {
                    new CustomMessageDialog(
                        AppMessages.CapturePhotoVideoFailed_Title,
                        String.Format(AppMessages.CapturePhotoVideoFailed, e.Message),
                        App.AppInformation,
                        MessageDialogButtons.Ok).ShowDialog();
                });
            }
        }
コード例 #2
0
        /// <summary>
        /// Half-pressing the shutter key initiates autofocus.
        /// </summary>
        private async void ShutterKeyHalfPressed(object sender, EventArgs e)
        {
            if (_manuallyFocused)
            {
                _manuallyFocused = false;
            }

            FocusIndicator.SetValue(Canvas.VisibilityProperty, Visibility.Collapsed);
            await AutoFocus();
        }
コード例 #3
0
        /// <summary>
        /// Set autofocus area to tap location and refocus.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void videoCanvas_Tap(object sender, GestureEventArgs e)
        {
            System.Windows.Point uiTapPoint = e.GetPosition(VideoCanvas);

            if (PhotoCaptureDevice.IsFocusRegionSupported(_dataContext.Device.SensorLocation) && _focusSemaphore.WaitOne(0))
            {
                // Get tap coordinates as a foundation point
                Windows.Foundation.Point tapPoint = new Windows.Foundation.Point(uiTapPoint.X, uiTapPoint.Y);

                double xRatio = VideoCanvas.ActualHeight / _dataContext.Device.PreviewResolution.Width;
                double yRatio = VideoCanvas.ActualWidth / _dataContext.Device.PreviewResolution.Height;

                // adjust to center focus on the tap point
                Windows.Foundation.Point displayOrigin = new Windows.Foundation.Point(
                    tapPoint.Y - _focusRegionSize.Width / 2,
                    (VideoCanvas.ActualWidth - tapPoint.X) - _focusRegionSize.Height / 2);

                // adjust for resolution difference between preview image and the canvas
                Windows.Foundation.Point viewFinderOrigin = new Windows.Foundation.Point(displayOrigin.X / xRatio, displayOrigin.Y / yRatio);
                Windows.Foundation.Rect  focusrect        = new Windows.Foundation.Rect(viewFinderOrigin, _focusRegionSize);

                // clip to preview resolution
                Windows.Foundation.Rect viewPortRect = new Windows.Foundation.Rect(0, 0, _dataContext.Device.PreviewResolution.Width, _dataContext.Device.PreviewResolution.Height);
                focusrect.Intersect(viewPortRect);

                _dataContext.Device.FocusRegion = focusrect;

                // show a focus indicator
                FocusIndicator.SetValue(Shape.StrokeProperty, _notFocusedBrush);
                FocusIndicator.SetValue(Canvas.LeftProperty, uiTapPoint.X - _focusRegionSize.Width / 2);
                FocusIndicator.SetValue(Canvas.TopProperty, uiTapPoint.Y - _focusRegionSize.Height / 2);
                FocusIndicator.SetValue(Canvas.VisibilityProperty, Visibility.Visible);

                CameraFocusStatus status = await _dataContext.Device.FocusAsync();

                if (status == CameraFocusStatus.Locked)
                {
                    FocusIndicator.SetValue(Shape.StrokeProperty, _focusedBrush);
                    _manuallyFocused = true;
                    _dataContext.Device.SetProperty(KnownCameraPhotoProperties.LockedAutoFocusParameters,
                                                    AutoFocusParameters.Exposure & AutoFocusParameters.Focus & AutoFocusParameters.WhiteBalance);
                }
                else
                {
                    _manuallyFocused = false;
                    _dataContext.Device.SetProperty(KnownCameraPhotoProperties.LockedAutoFocusParameters, AutoFocusParameters.None);
                }

                _focusSemaphore.Release();
            }
        }
コード例 #4
0
        /// <summary>
        /// Clicking on sensor button disables camera capturing controls, uninitializes old
        /// camera instance and initializes new camera instance using the other sensor. On-screen
        /// controls and listening to hardware shutter release key is disabled while initializing the
        /// sensor because capturing a photo is not possible at that time.
        /// </summary>
        private async void sensorButton_Click(object sender, EventArgs e)
        {
            FocusIndicator.SetValue(Canvas.VisibilityProperty, Visibility.Collapsed);
            _manuallyFocused = false;

            SetScreenButtonsEnabled(false);
            SetCameraButtonsEnabled(false);

            ShowProgress("Initializing camera...");

            videoBrush.Opacity = 0.25;

            overlayComboBox.Opacity = 0;

            _dataContext.Device.Dispose();
            _dataContext.Device = null;

            IReadOnlyList <CameraSensorLocation> sensorLocations = PhotoCaptureDevice.AvailableSensorLocations;

            if (_sensorLocation == sensorLocations[1])
            {
                _sensorLocation = sensorLocations[0];
            }
            else
            {
                _sensorLocation = sensorLocations[1];
            }

            await InitializeCamera(_sensorLocation);

            videoBrush.RelativeTransform = new CompositeTransform()
            {
                CenterX  = 0.5,
                CenterY  = 0.5,
                Rotation = _dataContext.Device != null && _dataContext.Device.SensorLocation == CameraSensorLocation.Back ?
                           _dataContext.Device.SensorRotationInDegrees :
                           -_dataContext.Device.SensorRotationInDegrees
            };

            videoBrush.SetSource(_dataContext.Device);
            videoBrush.Opacity = 1;

            overlayComboBox.Opacity = 1;

            HideProgress();

            SetScreenButtonsEnabled(true);
            SetCameraButtonsEnabled(true);
        }
コード例 #5
0
        /// <summary>
        /// Captures a photo. Photo data is stored to DataContext.ImageStream, and application
        /// is navigated to the preview page after capturing.
        /// </summary>
        private async Task Capture()
        {
            bool goToPreview = false;

            if (!_capturing)
            {
                _capturing = true;

                MemoryStream stream = new MemoryStream();

                CameraCaptureSequence sequence = _dataContext.Device.CreateCaptureSequence(1);
                sequence.Frames[0].CaptureStream = stream.AsOutputStream();

                await _dataContext.Device.PrepareCaptureSequenceAsync(sequence);

                await sequence.StartCaptureAsync();

                _dataContext.ImageStream = stream;

                _capturing = false;

                // Defer navigation as it will release the camera device and the
                // following Device calls must still work.
                goToPreview = true;
            }

            _manuallyFocused = false;

            if (PhotoCaptureDevice.IsFocusRegionSupported(_dataContext.Device.SensorLocation))
            {
                _dataContext.Device.FocusRegion = null;
            }

            FocusIndicator.SetValue(Canvas.VisibilityProperty, Visibility.Collapsed);
            _dataContext.Device.SetProperty(KnownCameraPhotoProperties.LockedAutoFocusParameters, AutoFocusParameters.None);

            if (goToPreview)
            {
                NavigationService.Navigate(new Uri("/PreviewPage.xaml", UriKind.Relative));
            }
        }
コード例 #6
0
        private async void VideoCanvasOnTap(object sender, GestureEventArgs e)
        {
            try
            {
                System.Windows.Point uiTapPoint = e.GetPosition(VideoCanvas);

                if (PhotoCaptureDevice.IsFocusRegionSupported(PhotoCaptureDevice.SensorLocation) && _focusSemaphore.WaitOne(0))
                {
                    // Get tap coordinates as a foundation point
                    var tapPoint = new Windows.Foundation.Point(uiTapPoint.X, uiTapPoint.Y);

                    double xRatio = VideoCanvas.ActualHeight / PhotoCaptureDevice.PreviewResolution.Width;
                    double yRatio = VideoCanvas.ActualWidth / PhotoCaptureDevice.PreviewResolution.Height;

                    // adjust to center focus on the tap point
                    var displayOrigin = new Windows.Foundation.Point(
                        tapPoint.Y - _focusRegionSize.Width / 2,
                        (VideoCanvas.ActualWidth - tapPoint.X) - _focusRegionSize.Height / 2);

                    // adjust for resolution difference between preview image and the canvas
                    var viewFinderOrigin = new Windows.Foundation.Point(displayOrigin.X / xRatio, displayOrigin.Y / yRatio);
                    var focusrect        = new Windows.Foundation.Rect(viewFinderOrigin, _focusRegionSize);

                    // clip to preview resolution
                    var viewPortRect = new Windows.Foundation.Rect(0, 0, PhotoCaptureDevice.PreviewResolution.Width,
                                                                   PhotoCaptureDevice.PreviewResolution.Height);
                    focusrect.Intersect(viewPortRect);

                    PhotoCaptureDevice.FocusRegion = focusrect;

                    // show a focus indicator
                    FocusIndicator.SetValue(Shape.StrokeProperty, _notFocusedBrush);
                    FocusIndicator.SetValue(Canvas.LeftProperty, uiTapPoint.X - _focusRegionSize.Width / 2);
                    FocusIndicator.SetValue(Canvas.TopProperty, uiTapPoint.Y - _focusRegionSize.Height / 2);
                    FocusIndicator.SetValue(VisibilityProperty, Visibility.Visible);

                    CameraFocusStatus status = await PhotoCaptureDevice.FocusAsync();

                    if (status == CameraFocusStatus.Locked)
                    {
                        FocusIndicator.SetValue(Shape.StrokeProperty, _focusedBrush);
                        _manuallyFocused = true;
                        PhotoCaptureDevice.SetProperty(KnownCameraPhotoProperties.LockedAutoFocusParameters,
                                                       AutoFocusParameters.Exposure & AutoFocusParameters.Focus & AutoFocusParameters.WhiteBalance);
                    }
                    else
                    {
                        _manuallyFocused = false;
                        PhotoCaptureDevice.SetProperty(KnownCameraPhotoProperties.LockedAutoFocusParameters, AutoFocusParameters.None);
                    }

                    _focusSemaphore.Release();
                }

                await Capture();
            }
            catch (Exception exception)
            {
                Deployment.Current.Dispatcher.BeginInvoke(() =>
                {
                    new CustomMessageDialog(
                        AppMessages.CaptureVideoFailed_Title,
                        String.Format(AppMessages.CaptureVideoFailed, exception.Message),
                        App.AppInformation,
                        MessageDialogButtons.Ok).ShowDialog();
                });
            }
        }