async private Task <PhotoCaptureDevice> InitCamera(CameraPreviewSettings settings, CameraSensorLocation loc) { PhotoCaptureDevice camera = await OpenCamera(settings.Width, settings.Height, loc); UInt32 iso = Helper.GetISO((UInt32)settings.ISO, loc); Nullable <UInt32> wb = Helper.GetWhiteBalance(settings.WhiteBalance, loc); CameraCapturePropertyRange focusRange = PhotoCaptureDevice.GetSupportedPropertyRange(loc, KnownCameraGeneralProperties.ManualFocusPosition); UInt32 focusDistance = settings.FocusDistance.Equals("min", StringComparison.OrdinalIgnoreCase) ? (UInt32)focusRange.Min : (UInt32)focusRange.Max; camera.SetProperty(KnownCameraPhotoProperties.ExposureCompensation, 0); camera.SetProperty(KnownCameraPhotoProperties.FlashMode, FlashState.Off); camera.SetProperty(KnownCameraPhotoProperties.FocusIlluminationMode, 0); if (wb.HasValue) { camera.SetProperty(KnownCameraPhotoProperties.WhiteBalancePreset, wb.Value); _waitForWb = true; Debug.WriteLine("Using WB value: {0}", wb.Value); } camera.SetProperty(KnownCameraPhotoProperties.Iso, iso); camera.SetProperty(KnownCameraGeneralProperties.ManualFocusPosition, focusDistance); Debug.WriteLine("Using ISO: {0}, Using Focus Distance: {1}", camera.GetProperty(KnownCameraPhotoProperties.Iso), camera.GetProperty(KnownCameraGeneralProperties.ManualFocusPosition)); return(camera); }
// Gets the configuration of the specified camera private async Task <CameraConfig> GetCameraConfiguration( CameraType cameraType, CameraSensorLocation sensorLocation) { CameraConfig cameraConfig = null; PhotoCaptureDevice photoCaptureDevice = null; FlashState flash = FlashState.Off; List <CameraResolution> resolutions = new List <CameraResolution>(); try { // Read supported resolutions System.Collections.Generic.IReadOnlyList <Windows.Foundation.Size> SupportedResolutions = PhotoCaptureDevice.GetAvailableCaptureResolutions(sensorLocation); resolutions = (from resolution in SupportedResolutions select new CameraResolution { Width = resolution.Width, Height = resolution.Height }).ToList(); // Read flash support. // Opening camera is required to read flash, request to open with min resolution var minResolution = SupportedResolutions.OrderBy(size => size.Width).First(); photoCaptureDevice = await PhotoCaptureDevice.OpenAsync(sensorLocation, minResolution); Enum.TryParse <FlashState>( photoCaptureDevice.GetProperty(KnownCameraPhotoProperties.FlashMode).ToString(), true, out flash); // Create the camera config cameraConfig = new CameraConfig { Type = (int)cameraType, Sizes = resolutions, Flash = flash != FlashState.Off ? true : false }; } catch (Exception e) { Logger.Error("Error in Camera get configuration. Reason - " + e.Message); } finally { if (photoCaptureDevice != null) { try { photoCaptureDevice.Dispose(); } catch { } } } return(cameraConfig); }
private async void AutoFocusTimer_Tick(object sender, EventArgs e) { ThrowIfDisposed(); if (!_inited) { return; } if (_focusTimer.Interval == TimeSpan.FromSeconds(AutoFocusIntervalInSecondsFrist)) { _focusTimer.Interval = TimeSpan.FromSeconds(AutoFocusIntervalInSeconds); } if (!_capturing && !_focusing) { // disable flash when auto focus object oldFlashMode = null; try { if (IsFocusIlluminationModeSupported) { oldFlashMode = _device.GetProperty(KnownCameraPhotoProperties.FocusIlluminationMode); _device.SetProperty(KnownCameraPhotoProperties.FocusIlluminationMode, FocusIlluminationMode.Off); } await FocusAsyncInternal(); ThrowIfDisposed(); } catch { if (this.IsDisposed) { throw new CardCaptureDeviceDisposedException(); } else { Debug.Assert(false); } } finally { if (!IsDisposed && oldFlashMode != null) { _device.SetProperty(KnownCameraPhotoProperties.FocusIlluminationMode, oldFlashMode); } } } }