예제 #1
0
        /// <summary>
        /// Reads supported white balance preset values from Parameter.Device and populates
        /// ArrayParameter.Options accordingly. Sets the SelectedOption as well.
        ///
        /// White balace preset auto value is set by setting the value in PhotoCaptureDevice API to
        /// null, therefore the separate handling for option "Auto".
        /// </summary>
        protected override void PopulateOptions()
        {
            ArrayParameterOption option         = new ArrayParameterOption(null, "Auto");
            ArrayParameterOption selectedOption = option;

            Options.Add(option);

            IReadOnlyList <object> supportedValues = PhotoCaptureDevice.GetSupportedPropertyValues(Device.SensorLocation, PropertyId);
            object value = Device.GetProperty(PropertyId);

            foreach (dynamic i in supportedValues)
            {
                WhiteBalancePreset wbp = (WhiteBalancePreset)i;

                option = new ArrayParameterOption(wbp, wbp.EnumerationToParameterName <WhiteBalancePreset>());

                Options.Add(option);

                if (i.Equals(value))
                {
                    selectedOption = option;
                }
            }

            SelectedOption = selectedOption;
        }
예제 #2
0
        public IReadOnlyList <FlashState> GetAvailableFlashStates(CameraSensorLocation cameraSensorLocation)
        {
            IReadOnlyList <object> rawValueList = PhotoCaptureDevice.GetSupportedPropertyValues(cameraSensorLocation, KnownCameraPhotoProperties.FlashMode);
            List <FlashState>      flashStates  = new List <FlashState>(rawValueList.Count);

            foreach (object rawValue in rawValueList)
            {
                flashStates.Add((FlashState)(uint)rawValue);
            }

            return(flashStates.AsReadOnly());
        }
예제 #3
0
        public static Nullable <UInt32> GetWhiteBalance(string wb, CameraSensorLocation loc)
        {
            var supportedPresets     = PhotoCaptureDevice.GetSupportedPropertyValues(loc, KnownCameraPhotoProperties.WhiteBalancePreset);
            Nullable <UInt32> preset = null;

            if (wb.Equals("daylight", StringComparison.CurrentCultureIgnoreCase))
            {
                preset = (UInt32)WhiteBalancePreset.Daylight;
            }

            return(preset.HasValue && supportedPresets.Contains(preset.Value) ? preset : null);
        }
예제 #4
0
        /// <summary>
        /// Initializes camera.
        /// </summary>
        /// <param name="sensorLocation">Camera sensor to initialize</param>
        private async Task InitializeCamera(CameraSensorLocation cameraSensorLocation)
        {
            Windows.Foundation.Size initialResolution =
                new Windows.Foundation.Size(Constant.DefaultCameraResolutionWidth,
                                            Constant.DefaultCameraResolutionHeight);
            Windows.Foundation.Size previewResolution =
                new Windows.Foundation.Size(Constant.DefaultCameraResolutionWidth,
                                            Constant.DefaultCameraResolutionHeight);

            // Find out the largest 4:3 capture resolution available on device
            IReadOnlyList <Windows.Foundation.Size> availableResolutions =
                PhotoCaptureDevice.GetAvailableCaptureResolutions(cameraSensorLocation);

            Windows.Foundation.Size captureResolution = new Windows.Foundation.Size(0, 0);
            for (int i = 0; i < availableResolutions.Count; i++)
            {
                double ratio = availableResolutions[i].Width / availableResolutions[i].Height;
                if (ratio > 1.32 && ratio < 1.34)
                {
                    if (captureResolution.Width < availableResolutions[i].Width)
                    {
                        captureResolution = availableResolutions[i];
                    }
                }
            }

            PhotoCaptureDevice device =
                await PhotoCaptureDevice.OpenAsync(cameraSensorLocation, initialResolution);

            await device.SetCaptureResolutionAsync(captureResolution);

            await device.SetPreviewResolutionAsync(previewResolution);

            _photoCaptureDevice = device;

            IReadOnlyList <object> supportedFlashmodes = PhotoCaptureDevice.GetSupportedPropertyValues(CameraSensorLocation.Back, KnownCameraPhotoProperties.FlashMode);

            if (supportedFlashmodes.Count > 1)
            {
                _photoCaptureDevice.SetProperty(KnownCameraPhotoProperties.FlashMode, FlashMode.Off);
            }
            else
            {
                TxtBlock_Flash.Visibility = Visibility.Collapsed;
            }

            SetOrientation(this.Orientation);
        }
예제 #5
0
        protected override void PopulateOptions()
        {
            IReadOnlyList <object> supportedValues = PhotoCaptureDevice.GetSupportedPropertyValues(
                Device.SensorLocation, ParameterId);
            object currentValue = Device.GetProperty(ParameterId);

            foreach (dynamic mode in supportedValues)
            {
                var csm    = (CameraSceneMode)mode;
                var option = new ArrayParameterOption <CameraSceneMode>(csm, Enum.GetName(typeof(CameraSceneMode), mode));
                Options.Add(option);

                if (mode.Equals(currentValue))
                {
                    SelectedOption = option;
                }
            }
        }
예제 #6
0
        /// <summary>
        /// Reads supported autofocus range values from Parameter.Device and populates
        /// ArrayParameter.Options accordingly. Sets the SelectedOption as well.
        /// </summary>
        protected override void PopulateOptions()
        {
            IReadOnlyList <object> supportedValues = PhotoCaptureDevice.GetSupportedPropertyValues(Device.SensorLocation, PropertyId);
            object value = Device.GetProperty(PropertyId);

            foreach (dynamic i in supportedValues)
            {
                AutoFocusRange afr = (AutoFocusRange)i;

                ArrayParameterOption option = new ArrayParameterOption(afr, afr.EnumerationToParameterName <AutoFocusRange>());

                Options.Add(option);

                if (i.Equals(value))
                {
                    SelectedOption = option;
                }
            }
        }
예제 #7
0
        /// <summary>
        /// Reads supported flash mode values from Parameter.Device and populates
        /// ArrayParameter.Options accordingly. Sets the SelectedOption as well.
        /// </summary>
        protected override void PopulateOptions()
        {
            IReadOnlyList <object> supportedValues = PhotoCaptureDevice.GetSupportedPropertyValues(Device.SensorLocation, PropertyId);
            object value = Device.GetProperty(PropertyId);

            foreach (dynamic i in supportedValues)
            {
                FlashState fm = (FlashState)i;

                ArrayParameterOption option = new ArrayParameterOption(fm, fm.EnumerationToParameterName <FlashState>(), "Assets/Icons/overlay.flashmode." + fm.ToString().ToLower() + ".png");

                Options.Add(option);

                if (i.Equals(value))
                {
                    SelectedOption = option;
                }
            }
        }
예제 #8
0
        public ViewfinderPage()
        {
            InitializeComponent();

            _photoChooserTask.Completed += PhotoChooserTask_Completed;

            // Flash toggle button

            if (PhotoCaptureDevice.GetSupportedPropertyValues(SENSOR_LOCATION, KnownCameraPhotoProperties.FlashMode).Count > 1)
            {
                _flashButton = new ApplicationBarIconButton()
                {
                    IconUri = new Uri("/Assets/Icons/flash_auto.png", UriKind.Relative),
                    Text    = AppResources.ViewfinderPage_FlashButton_Text
                };

                _flashButton.Click += FlashButton_Click;

                ApplicationBar.Buttons.Add(_flashButton);

                ApplicationBar.IsVisible = true;
            }
        }
예제 #9
0
        private async Task InitializeCamera(CameraSensorLocation sensorLocation)
        {
            activeThreads = 0;
            isClosing     = false;
            Windows.Foundation.Size captureResolution = new Windows.Foundation.Size(1280, 720);
            Windows.Foundation.Size previewResolution = new Windows.Foundation.Size(1280, 720);

            IReadOnlyList <Windows.Foundation.Size> prevSizes = PhotoCaptureDevice.GetAvailablePreviewResolutions(sensorLocation);
            IReadOnlyList <Windows.Foundation.Size> captSizes = PhotoCaptureDevice.GetAvailableCaptureResolutions(sensorLocation);


            double bestAspect = 1000;

            int bestAspectResIndex = 0;


            double aspect = Application.Current.Host.Content.ActualHeight / Application.Current.Host.Content.ActualWidth;


            for (int i = 0; i < captSizes.Count; i++)
            {
                double w = captSizes[i].Width;
                double h = captSizes[i].Height;

                double resAspect = w / h;

                double diff = aspect - resAspect;
                if (diff < 0)
                {
                    diff = -diff;
                }


                if (diff < bestAspect)
                {
                    bestAspect         = diff;
                    bestAspectResIndex = i;
                }
            }

            if (bestAspectResIndex >= 0)
            {
                captureResolution.Width  = captSizes[bestAspectResIndex].Width;
                captureResolution.Height = captSizes[bestAspectResIndex].Height;
            }

            Windows.Foundation.Size initialResolution = captureResolution;

            try
            {
                PhotoCaptureDevice d = null;


                System.Diagnostics.Debug.WriteLine("Settinge camera initial resolution: " + initialResolution.Width + "x" + initialResolution.Height + "......");

                bool initialized = false;

                try
                {
                    d = await PhotoCaptureDevice.OpenAsync(sensorLocation, initialResolution);

                    System.Diagnostics.Debug.WriteLine("Success " + initialResolution);
                    initialized       = true;
                    captureResolution = initialResolution;
                }
                catch (Exception e)
                {
                    System.Diagnostics.Debug.WriteLine("Failed to set initial resolution: " + initialResolution + " error:" + e.Message);
                }

                if (!initialized)
                {
                    try
                    {
                        d = await PhotoCaptureDevice.OpenAsync(sensorLocation, captSizes.ElementAt <Windows.Foundation.Size>(0));

                        System.Diagnostics.Debug.WriteLine("Success " + captSizes.ElementAt <Windows.Foundation.Size>(0));
                        initialized       = true;
                        captureResolution = captSizes.ElementAt <Windows.Foundation.Size>(0);
                    }
                    catch
                    {
                        System.Diagnostics.Debug.WriteLine("Failed to set initial resolution: " + captSizes.ElementAt <Windows.Foundation.Size>(0));
                    }
                }

                //try to not use too high resolution

                if (param_EnableHiRes)
                {
                    MAX_RESOLUTION = 1280 * 800;
                }
                else
                {
                    MAX_RESOLUTION = 800 * 480;
                }


                if (d.PreviewResolution.Height * d.PreviewResolution.Width > MAX_RESOLUTION)
                {
                    bestAspectResIndex = -1;

                    aspect = (double)captureResolution.Width / captureResolution.Height;

                    for (int i = 0; i < prevSizes.Count; i++)
                    {
                        double w = prevSizes[i].Width;
                        double h = prevSizes[i].Height;

                        double resAspect = w / h;

                        double diff = aspect - resAspect;
                        if (diff < 0.01 && diff > -0.01)
                        {
                            if (w * h <= MAX_RESOLUTION)
                            {
                                previewResolution  = prevSizes.ElementAt <Windows.Foundation.Size>(i);
                                bestAspectResIndex = i;
                                break;
                            }
                        }
                    }


                    if (bestAspectResIndex >= 0)
                    {
                        try
                        {
                            await d.SetPreviewResolutionAsync(previewResolution);
                        }
                        finally
                        {
                        }
                    }
                }

                System.Diagnostics.Debug.WriteLine("Preview resolution: " + d.PreviewResolution);

                d.SetProperty(KnownCameraGeneralProperties.EncodeWithOrientation,
                              d.SensorLocation == CameraSensorLocation.Back ?
                              d.SensorRotationInDegrees : -d.SensorRotationInDegrees);


                cameraDevice = d;


                cameraDevice.PreviewFrameAvailable += previewFrameHandler;

                IReadOnlyList <object> flashProperties = PhotoCaptureDevice.GetSupportedPropertyValues(sensorLocation, KnownCameraAudioVideoProperties.VideoTorchMode);

                if (param_EnableFlash)
                {
                    if (flashProperties.ToList().Contains((UInt32)VideoTorchMode.On))
                    {
                        flashAvailable = true;

                        if (param_DefaultFlashOn)
                        {
                            flashActive = true;
                            cameraDevice.SetProperty(KnownCameraAudioVideoProperties.VideoTorchMode, VideoTorchMode.On);
                            flashButtonImage.Source = new BitmapImage(new Uri("/Plugins/com.manateeworks.barcodescanner/flashbuttonon.png", UriKind.Relative));
                        }
                        else
                        {
                            flashActive = false;
                            cameraDevice.SetProperty(KnownCameraAudioVideoProperties.VideoTorchMode, VideoTorchMode.Off);
                            flashButtonImage.Source = new BitmapImage(new Uri("/Plugins/com.manateeworks.barcodescanner/flashbuttonoff.png", UriKind.Relative));
                        }

                        flashButton.Visibility = System.Windows.Visibility.Visible;
                    }
                    else
                    {
                        flashAvailable         = false;
                        flashButton.Visibility = System.Windows.Visibility.Collapsed;
                    }
                }
                else
                {
                    flashButton.Visibility = System.Windows.Visibility.Collapsed;
                }

                videoBrush.SetSource(cameraDevice);
                focusTimer          = new DispatcherTimer();
                focusTimer.Interval = TimeSpan.FromSeconds(3);
                focusTimer.Tick    += delegate
                {
                    cameraDevice.FocusAsync();
                };
                focusTimer.Start();
            }
            catch (Exception e)
            {
                Debug.WriteLine("Camera initialization error: " + e.Message);
            }
        }