private async Task InitializeCamera()
        {
            if (Options.UseFrontCameraIfAvailable.HasValue && Options.UseFrontCameraIfAvailable.Value)
            {
                try
                {
                    var frontCameraCaptureResolutions = AudioVideoCaptureDevice.GetAvailableCaptureResolutions(CameraSensorLocation.Front);
                    var resolution = GetResolution(frontCameraCaptureResolutions.OrderBy(r => r.Width * r.Height));
                    _photoCamera = await AudioVideoCaptureDevice.OpenForVideoOnlyAsync(CameraSensorLocation.Front, resolution);
                }
                catch (Exception ex)
                {
                    MobileBarcodeScanner.Log("Failed to create front facing camera: {0}", ex);
                }
            }

            MobileBarcodeScanner.Log("InitializeCamera");

            if (_photoCamera == null)
            {
                var backCameraCaptureResolutions = AudioVideoCaptureDevice.GetAvailableCaptureResolutions(CameraSensorLocation.Back);
                var resolution = GetResolution(backCameraCaptureResolutions.OrderBy(r => r.Width * r.Height));
                _photoCamera = await AudioVideoCaptureDevice.OpenForVideoOnlyAsync(CameraSensorLocation.Back, resolution);
            }

            IsAnalyzing = true;

            OnPhotoCameraInitialized(this, new CameraOperationCompletedEventArgs(true, null));

            MobileBarcodeScanner.Log("Wired up Initizialied");
        }
Example #2
0
        async static public Task <bool> Init()
        {
            if (isExist)
            {
                return(true);
            }

            for (int i = 0; i < 5; i++)
            {
                try
                {
                    CamVideo = await AudioVideoCaptureDevice.OpenForVideoOnlyAsync(CameraSensorLocation.Back, new Windows.Foundation.Size(640, 480));

                    CamVideo.GetProperty(KnownCameraAudioVideoProperties.VideoTorchMode);
                    break;
                }
                catch
                {
                }
            }

            if (CamVideo == null)
            {
                MessageBox.Show(AppResources.Text_Error_Light, AppResources.Text_Error_Title, MessageBoxButton.OK);
            }

            return(isExist);
        }
        public async Task InitAsync()
        {
            Debug.Assert(_status == StatusEnum.Uninitialized);

            if (AudioVideoCaptureDevice.AvailableSensorLocations.Contains(_location))
            {
                var resolutions = AudioVideoCaptureDevice.GetAvailableCaptureResolutions(_location);

                if (resolutions.Count > 0)
                {
                    var resolution = resolutions.First();

                    _device = await AudioVideoCaptureDevice.OpenForVideoOnlyAsync(_location, resolution);

                    if ((object)_device != null)
                    {
                        var values = AudioVideoCaptureDevice.GetSupportedPropertyValues(_location, KnownCameraAudioVideoProperties.VideoTorchMode);

                        if (values.Contains((UInt32)VideoTorchMode.On))
                        {
                            // Done, it looks like we have it!

                            _status = StatusEnum.Ready;
                        }
                        else
                        {
                            _status = StatusEnum.CannotFindTorchOnDevice;
                            _device.Dispose();
                            _device = null;
                        }
                    }
                    else
                    {
                        _status = StatusEnum.CannotAccessCameraDevice;
                    }
                }
                else
                {
                    _status = StatusEnum.CannotFindValidResolutionForCamera;
                }
            }
            else
            {
                _status = StatusEnum.CannotFindBackCamera;
            }
        }
        private async void StartInternal()
        {
            var locations = GetCameraSensorLocations();
            var location  = locations[0];

            if (CaptureArgs.DeviceNumber.HasValue && CaptureArgs.DeviceNumber.Value < locations.Length)
            {
                location = locations[CaptureArgs.DeviceNumber.Value];
            }
            else if (DesiredDeviceNumber.HasValue && DesiredDeviceNumber.Value < locations.Length)
            {
                location     = locations[DesiredDeviceNumber.Value];
                DeviceNumber = DesiredDeviceNumber.Value;
            }

            Label = GetCameraSensorLocationName(location);

            Capture = await AudioVideoCaptureDevice.OpenForVideoOnlyAsync(location, GetInitialResolution(location, CaptureArgs.Width, CaptureArgs.Height));

            Capture.SetProperty(KnownCameraAudioVideoProperties.VideoFrameRate, CaptureArgs.FrameRate);

            Capture.PreviewFrameAvailable += Capture_PreviewFrameAvailable;
            if (Preview != null)
            {
                Preview.LoadCamera(Capture);
                RunOnUIThread(() =>
                {
                    SetRotation(Page.Orientation);
                    Preview.Rotate(BufferRotate);
                });
            }

            RunOnUIThread(() =>
            {
                XnaTimer          = new DispatcherTimer();
                XnaTimer.Interval = TimeSpan.FromMilliseconds(10);
                XnaTimer.Tick    += XnaTimer_Tick;
                XnaTimer.Start();
            });
        }
 private async Task <AudioVideoCaptureDevice> GetCaptureDevice()
 {
     return(await AudioVideoCaptureDevice.OpenForVideoOnlyAsync(sensorLocation, AudioVideoCaptureDevice.GetAvailableCaptureResolutions(sensorLocation).First()));
 }