예제 #1
0
        /// <summary>
        /// Initialze MediaCapture
        /// </summary>
        /// <param name="panel"></param>
        /// <returns></returns>
        public async Task InitializeCapture(Windows.Devices.Enumeration.Panel panel = Windows.Devices.Enumeration.Panel.Back)
        {
            mediaCapture      = new MediaCapture();
            this.currentPanel = panel;
            MediaCaptureInitializationSettings setting = await InitializeSettings(panel);

            if (setting != null)
            {
                await mediaCapture.InitializeAsync(setting);
            }
            else
            {
                await mediaCapture.InitializeAsync();
            }
            var resolutions = this.mediaCapture.VideoDeviceController.GetAvailableMediaStreamProperties(MediaStreamType.Photo);

            if (resolutions.Count >= 1)
            {
                var hires = resolutions.OrderByDescending(item => ((VideoEncodingProperties)item).Width).First();
                await mediaCapture.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.Photo, hires);
            }
            if (!this.isExternalCamera)
            {
                var previewRotation = (this.currentPanel == Windows.Devices.Enumeration.Panel.Back) ? VideoRotation.Clockwise90Degrees : VideoRotation.Clockwise270Degrees;
                try
                {
                    mediaCapture.SetPreviewRotation(previewRotation);
                    mediaCapture.SetRecordRotation(previewRotation);
                }
                catch (Exception e)
                {
                    Debug.WriteLine(e.Message);
                }
            }
        }
예제 #2
0
        public async Task <MediaCapture> Initialize()
        {
            var cameraDeviceInfos = (await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture)).Where(d => d.IsEnabled && d.EnclosureLocation != null);

            var frontCamDeviceInfo = cameraDeviceInfos.FirstOrDefault(d => d.EnclosureLocation.Panel == Panel.Front);

            var mediaCaptureInitialization = new MediaCaptureInitializationSettings
            {
                PhotoCaptureSource = PhotoCaptureSource.Photo,
                AudioDeviceId      = string.Empty,
                VideoDeviceId      = frontCamDeviceInfo.Id,
            };

            mediaCapture = new MediaCapture();
            await mediaCapture.InitializeAsync(mediaCaptureInitialization);

            mediaCapture.VideoDeviceController.PrimaryUse = CaptureUse.Photo;

            mediaCapture.SetPreviewRotation(VideoRotation.Clockwise270Degrees);
            mediaCapture.SetRecordRotation(VideoRotation.Clockwise270Degrees);

            // Create photo encoding properties as JPEG and set the size that should be used for photo capturing
            imgEncodingProperties        = ImageEncodingProperties.CreateJpeg();
            imgEncodingProperties.Height = 480;
            imgEncodingProperties.Width  = 640;


            return(mediaCapture);
        }
 private void DisplayInfo_OrientationChanged(DisplayInformation sender, object args)
 {
     if (mediaCapture != null)
     {
         rotation = VideoRotationLookup(sender.CurrentOrientation, false);
         mediaCapture.SetPreviewRotation(rotation);
         mediaCapture.SetRecordRotation(rotation);
     }
 }
예제 #4
0
        public async Task InitializeCameraAsync()
        {
            try
            {
                if (_mediaCapture == null)
                {
                    _mediaCapture         = new MediaCapture();
                    _mediaCapture.Failed += MediaCapture_Failed;

                    _cameraDevices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);

                    if (_cameraDevices == null || !_cameraDevices.Any())
                    {
                        throw new NotSupportedException();
                    }

                    var device = _cameraDevices.FirstOrDefault(camera => camera.EnclosureLocation?.Panel == Panel);

                    var cameraId = device?.Id ?? _cameraDevices.First().Id;

                    await _mediaCapture.InitializeAsync(new MediaCaptureInitializationSettings { VideoDeviceId = cameraId });

                    if (Panel == Panel.Back)
                    {
                        _mediaCapture.SetRecordRotation(VideoRotation.Clockwise90Degrees);
                        _mediaCapture.SetPreviewRotation(VideoRotation.Clockwise90Degrees);
                        _mirroringPreview = false;
                    }
                    else
                    {
                        _mirroringPreview = true;
                    }

                    IsInitialized = true;
                    CanSwitch     = _cameraDevices?.Count > 1;
                    RegisterOrientationEventHandlers();
                    await StartPreviewAsync();
                }
            }
            catch (UnauthorizedAccessException)
            {
                errorMessage.Text = "Camera_Exception_UnauthorizedAccess".GetLocalized();
            }
            catch (NotSupportedException)
            {
                errorMessage.Text = "Camera_Exception_NotSupported".GetLocalized();
            }
            catch (TaskCanceledException)
            {
                errorMessage.Text = "Camera_Exception_InitializationCanceled".GetLocalized();
            }
            catch (Exception)
            {
                errorMessage.Text = "Camera_Exception_InitializationError".GetLocalized();
            }
        }
        private void DisplayInfo_OrientationChanged(DisplayInformation sender, object args)
        {
            if (_mediaCapture == null)
            {
                return;
            }

            _mediaCapture.SetPreviewRotation(_cam
                    ? VideoRotationLookup(sender.CurrentOrientation, true)
                    : VideoRotationLookup(sender.CurrentOrientation, false));
            var rotation = VideoRotationLookup(sender.CurrentOrientation, false);

            _mediaCapture.SetRecordRotation(rotation);
        }
예제 #6
0
        /// <summary>
        /// Initializes the MediaCapture, registers events, gets camera device information for mirroring and rotating, starts preview and unlocks the UI
        /// </summary>
        /// <returns>Task</returns>
        private async Task InitializeCameraAsync()
        {
            if (_mediaCapture == null)
            {
                _mediaCapture         = new MediaCapture();
                _mediaCapture.Failed += MediaCapture_Failed;

                _cameraDevices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);

                if (_cameraDevices == null)
                {
                    throw new NotSupportedException();
                }

                try
                {
                    var device = _cameraDevices.FirstOrDefault(camera => camera.EnclosureLocation?.Panel == Panel);

                    var cameraId = device != null ? device.Id : _cameraDevices.First().Id;

                    await _mediaCapture.InitializeAsync(new MediaCaptureInitializationSettings { VideoDeviceId = cameraId });

                    if (Panel == Panel.Back)
                    {
                        _mediaCapture.SetRecordRotation(VideoRotation.Clockwise90Degrees);
                        _mediaCapture.SetPreviewRotation(VideoRotation.Clockwise90Degrees);
                        _mirroringPreview = false;
                    }
                    else
                    {
                        _mirroringPreview = true;
                    }

                    IsInitialized = true;
                }
                catch (UnauthorizedAccessException ex)
                {
                    throw ex;
                }

                if (IsInitialized)
                {
                    CanSwitch             = _cameraDevices?.Count > 1;
                    PreviewControl.Source = _mediaCapture;
                    RegisterOrientationEventHandlers();
                    await StartPreviewAsync();
                }
            }
        }
예제 #7
0
        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.
        /// This parameter is typically used to configure the page.</param>
        protected override async void OnNavigatedTo(NavigationEventArgs e)
        {
            var myArgs = (ImageCapturePageArguments)e.Parameter;

            _project     = myArgs.Project;
            _targetFrame = myArgs.TargetFrame;
            await _mediaCapture.InitializeAsync();

            _imageCapture.Source = _mediaCapture;
            _mediaCapture.SetPreviewRotation(VideoRotation.Clockwise90Degrees);
            _mediaCapture.SetRecordRotation(VideoRotation.Clockwise90Degrees);
            await _mediaCapture.StartPreviewAsync();

            HardwareButtons.BackPressed += (sender, args) => Frame.Navigate(typeof(FramingPage), _project);
        }
예제 #8
0
        private async Task InitializeQrCode()
        {
            string error = null;

            try
            {
                //if (_mediaCapture == null)
                //{
                // Find all available webcams
                DeviceInformationCollection webcamList = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);


                // Get the proper webcam (default one)
                DeviceInformation backWebcam = (from webcam in webcamList where webcam.IsEnabled select webcam).FirstOrDefault();


                // Initializing MediaCapture


                _mediaCapture = new MediaCapture();
                await _mediaCapture.InitializeAsync(new MediaCaptureInitializationSettings
                {
                    VideoDeviceId        = backWebcam.Id,
                    AudioDeviceId        = "",
                    StreamingCaptureMode = StreamingCaptureMode.Video,
                    PhotoCaptureSource   = PhotoCaptureSource.VideoPreview
                });


                // Adjust camera rotation for Phone
                _mediaCapture.SetPreviewRotation(VideoRotation.Clockwise90Degrees);
                _mediaCapture.SetRecordRotation(VideoRotation.Clockwise90Degrees);


                // Set the source of CaptureElement to MediaCapture
                captureElement.Source = _mediaCapture;
                await _mediaCapture.StartPreviewAsync();


                // _mediaCapture.FocusChanged += _mediaCapture_FocusChanged; // ToDo:


                // Seetting Focus & Flash(if Needed)


                var torch = _mediaCapture.VideoDeviceController.TorchControl;
                if (torch.Supported)
                {
                    torch.Enabled = true;
                }


                await _mediaCapture.VideoDeviceController.FocusControl.UnlockAsync();

                var focusSettings = new FocusSettings();
                focusSettings.AutoFocusRange        = AutoFocusRange.FullRange;
                focusSettings.Mode                  = FocusMode.Continuous;
                focusSettings.WaitForFocus          = true;
                focusSettings.DisableDriverFallback = false;
                _mediaCapture.VideoDeviceController.FocusControl.Configure(focusSettings);
                await _mediaCapture.VideoDeviceController.FocusControl.FocusAsync();


                //}
            }
            catch (Exception ex)
            {
                dialog = new MessageDialog("Error: " + ex.Message);
                dialog.ShowAsync();
            }
        }