/// <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(); }); } }
private async Task StartCapturingAsync() { CameraCaptureSequence sequence = device.CreateCaptureSequence(1); var memoryStream = new MemoryStream(); sequence.Frames[0].CaptureStream = memoryStream.AsOutputStream(); device.SetProperty(KnownCameraPhotoProperties.FlashMode, FlashState.Off); device.SetProperty(KnownCameraPhotoProperties.SceneMode, CameraSceneMode.Macro); await device.PrepareCaptureSequenceAsync(sequence); }
private async Task StartCapturingAsync() { CameraCaptureSequence sequence = PhotoCaptureDevice.CreateCaptureSequence(1); var memoryStream = new MemoryStream(); sequence.Frames[0].CaptureStream = memoryStream.AsOutputStream(); try { PhotoCaptureDevice.SetProperty(KnownCameraPhotoProperties.FlashMode, FlashState.Off); PhotoCaptureDevice.SetProperty(KnownCameraPhotoProperties.SceneMode, CameraSceneMode.Macro); } catch { // one or more properties not supported } await PhotoCaptureDevice.PrepareCaptureSequenceAsync(sequence); }
/// <summary> /// Captures a photo. Photo data is stored to ImageStream, and /// application is navigated to the preview page after capturing. /// </summary> private async Task Capture() { bool goToPreview = false; if (!_capturing) { _capturing = true; DataContext dataContext = FilterEffects.DataContext.Singleton; // Reset the streams dataContext.ImageStream.Seek(0, SeekOrigin.Begin); dataContext.ThumbStream.Seek(0, SeekOrigin.Begin); CameraCaptureSequence sequence = _photoCaptureDevice.CreateCaptureSequence(1); sequence.Frames[0].CaptureStream = dataContext.ImageStream.AsOutputStream(); sequence.Frames[0].ThumbnailStream = dataContext.ThumbStream.AsOutputStream(); await _photoCaptureDevice.PrepareCaptureSequenceAsync(sequence); await sequence.StartCaptureAsync(); // Get the storyboard from application resources Storyboard sb = (Storyboard)Resources["CaptureAnimation"]; sb.Begin(); _capturing = false; goToPreview = true; } _photoCaptureDevice.SetProperty( KnownCameraPhotoProperties.LockedAutoFocusParameters, AutoFocusParameters.None); if (goToPreview) { NavigationService.Navigate(new Uri("/FilterPreviewPage.xaml", UriKind.Relative)); } }
private async void CapturePhoto() { try { CameraCaptureSequence sequence = captureDevice.CreateCaptureSequence(1); captureDevice.SetProperty(KnownCameraGeneralProperties.AutoFocusRange, AutoFocusRange.Infinity); captureDevice.SetProperty(KnownCameraGeneralProperties.PlayShutterSoundOnCapture, true); //captureDevice.SetProperty(KnownCameraPhotoProperties.FlashMode, FlashState.On); MemoryStream captureStream1 = new MemoryStream(); sequence.Frames[0].CaptureStream = captureStream1.AsOutputStream(); await captureDevice.PrepareCaptureSequenceAsync(sequence); await sequence.StartCaptureAsync(); captureStream1.Seek(0, SeekOrigin.Begin); MediaLibrary library = new MediaLibrary(); string filename = "IMG_" + saveCounter; Picture pic1 = library.SavePictureToCameraRoll(filename, captureStream1); //Save picture in cameraroll //Update Background Cameraroll Button BitmapImage bitImage = new BitmapImage(); bitImage.SetSource(pic1.GetThumbnail()); ImageBrush imgButton = new ImageBrush(); imgButton.ImageSource = bitImage; CameraRollButton.Background = imgButton; saveCounter++; } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }
/// <summary> /// Asynchronously captures a frame for further usage. /// </summary> private async Task CaptureAsync() { if (!_focusing && !_capturing) { _capturing = true; ProgressBar.IsIndeterminate = true; ProgressBar.Visibility = Visibility.Visible; var stream = new MemoryStream(); try { var sequence = _device.CreateCaptureSequence(1); sequence.Frames[0].CaptureStream = stream.AsOutputStream(); await _device.PrepareCaptureSequenceAsync(sequence); // Freeze preview image to avoid viewfinder showing live feed during/after capture var freezeBitmap = new WriteableBitmap((int)_device.PreviewResolution.Width, (int)_device.PreviewResolution.Height); _device.GetPreviewBufferArgb(freezeBitmap.Pixels); freezeBitmap.Invalidate(); FreezeImage.Source = freezeBitmap; FreezeImage.Visibility = Visibility.Visible; await sequence.StartCaptureAsync(); } catch (Exception) { stream.Close(); } ProgressBar.Visibility = Visibility.Collapsed; ProgressBar.IsIndeterminate = false; _capturing = false; if (stream.CanRead) { PhotoModel.Singleton.FromNewImage(stream, PhotoOrigin.Camera); if (_picker) { NavigationService.GoBack(); } else { if (_lens) { NavigationService.Navigate(new Uri("/Pages/MagnifierPage.xaml?editor", UriKind.Relative)); } else { NavigationService.Navigate(new Uri("/Pages/MagnifierPage.xaml", UriKind.Relative)); } } } else { FreezeImage.Visibility = Visibility.Collapsed; FreezeImage.Source = null; } } }
public async Task <Stream> CaptureAsync(bool focusBeforeCapture = true) { ThrowIfDisposed(); if (!_inited) { throw new InvalidOperationException("not inited"); } if (_capturing) { throw new InvalidOperationException("is already capturing"); } MemoryStream stream = null; _capturing = true; try { CameraCaptureSequence seq = _device.CreateCaptureSequence(1); stream = new MemoryStream(); seq.Frames[0].CaptureStream = stream.AsOutputStream(); await _device.PrepareCaptureSequenceAsync(seq); ThrowIfDisposed(); if (focusBeforeCapture) { await FocusAsyncInternal(); // focus before capture } ThrowIfDisposed(); #region Wait for auto focus hack if (_focusing) { await AwaitWhenNotFocusing(); ThrowIfDisposed(); } #endregion // Wait for auto focus hack await seq.StartCaptureAsync(); } catch (ObjectDisposedException) { throw new CardCaptureDeviceDisposedException(); } catch (Exception ex) { ThrowExceptionOrDisposed(new CaptureFailedException(ex.Message), false); } finally { _capturing = false; } ThrowIfDisposed(); return(stream); }
private async Task CapturePhoto() { try { if (Camera == null) { return; } m_canTakePicture = false; int encodedOrientation = 0; int sensorOrientation = (Int32)Camera.SensorRotationInDegrees; switch (this.Orientation) { // Camera hardware shutter button up. case PageOrientation.LandscapeLeft: encodedOrientation = -90 + sensorOrientation; break; // Camera hardware shutter button down. case PageOrientation.LandscapeRight: encodedOrientation = 90 + sensorOrientation; break; // Camera hardware shutter button right. case PageOrientation.PortraitUp: encodedOrientation = 0 + sensorOrientation; break; // Camera hardware shutter button left. case PageOrientation.PortraitDown: encodedOrientation = 180 + sensorOrientation; break; } // Apply orientation to image encoding. Camera.SetProperty(KnownCameraGeneralProperties.EncodeWithOrientation, encodedOrientation); MemoryStream stream = new MemoryStream(); MemoryStream thumbStream = new MemoryStream(); var sequence = Camera.CreateCaptureSequence(1); sequence.Frames[0].CaptureStream = stream.AsOutputStream(); sequence.Frames[0].ThumbnailStream = thumbStream.AsOutputStream(); try { await Camera.PrepareCaptureSequenceAsync(sequence); await sequence.StartCaptureAsync(); } catch (OperationCanceledException) { if (stream != null) { stream.Close(); stream.Dispose(); } if (thumbStream != null) { thumbStream.Close(); thumbStream.Dispose(); } m_canTakePicture = true; return; } catch (InvalidOperationException) { if (stream != null) { stream.Close(); stream.Dispose(); } if (thumbStream != null) { thumbStream.Close(); thumbStream.Dispose(); } m_canTakePicture = true; return; } stream.Seek(0, SeekOrigin.Begin); thumbStream.Seek(0, SeekOrigin.Begin); try { if (App.SaveToCameraRollEnabled) { SaveImageToCameraRoll(stream, thumbStream); } else { SaveImageToIsoStore(stream, thumbStream); } } catch (Exception ex) { MessageBox.Show("Cannot save captured photo. Error=" + ex.Message); if (stream != null) { stream.Close(); stream.Dispose(); } if (thumbStream != null) { thumbStream.Close(); thumbStream.Dispose(); } } m_canTakePicture = true; } catch (Exception exp) { MessageBox.Show("Capturing photo failed. Please retry. Error=" + exp.Message); m_canTakePicture = true; } }
private async void TakeSnapShot() { lock (_lockObject) { _cameraInUse = true; TakingPictureText = AppResources.TakingPictureText; } try { CameraCaptureSequence seq = _cameraCaptureDevice.CreateCaptureSequence(1); if (!FrontCamera) { if (FlashOn) { _cameraCaptureDevice.SetProperty(KnownCameraPhotoProperties.FlashMode, FlashState.On); } else { _cameraCaptureDevice.SetProperty(KnownCameraPhotoProperties.FlashMode, FlashState.Off); } } if (SoundOn) { _cameraCaptureDevice.SetProperty(KnownCameraGeneralProperties.PlayShutterSoundOnCapture, true); } else { _cameraCaptureDevice.SetProperty(KnownCameraGeneralProperties.PlayShutterSoundOnCapture, false); } await _cameraCaptureDevice.SetCaptureResolutionAsync(SelectedResolution.CameraResolution); MemoryStream stream = new MemoryStream(); seq.Frames[0].CaptureStream = stream.AsOutputStream(); if (FocusOn) { await _cameraCaptureDevice.FocusAsync(); } await _cameraCaptureDevice.PrepareCaptureSequenceAsync(seq); await seq.StartCaptureAsync(); int countOfPictures = _mediaLibrary.SavedPictures.Count(); //Not sure what this is, but we need it to save to library stream.Seek(0, SeekOrigin.Begin); _mediaLibrary.SavePictureToCameraRoll("spycam" + countOfPictures, stream); stream.Position = 0; var imageSource = new BitmapImage(); imageSource.SetSource(stream); SetCenterXAndCenterY(imageSource); LastPictureTaken = imageSource; } catch (Exception ex) { } lock (_lockObject) { _cameraInUse = false; PictureCount = PictureCount + 1; TakingPictureText = string.Empty; } }
private async void InitializeCameraAsync(CameraSensorLocation _sensor = CameraSensorLocation.Back) { SupportedResolutions = PhotoCaptureDevice.GetAvailableCaptureResolutions(_sensor).ToList(); CurrentResolution = SupportedResolutions[0]; cam = await PhotoCaptureDevice.OpenAsync(_sensor, CurrentResolution); // Enable shutter sound cam.SetProperty(KnownCameraGeneralProperties.PlayShutterSoundOnCapture, true); // Create capture sequence seq = cam.CreateCaptureSequence(1); // White balance IReadOnlyList<object> wbList = PhotoCaptureDevice.GetSupportedPropertyValues(_sensor, KnownCameraPhotoProperties.WhiteBalancePreset); SupportedWhiteBalances = new List<object>(); SupportedWhiteBalances.Add(ProCamConstraints.PROCAM_AUTO_WHITE_BALANCE); foreach (object rawValue in wbList) { SupportedWhiteBalances.Add((WhiteBalancePreset)(uint)rawValue); } OSD.WhiteBalanceOSD.SupportedWhiteBalances = SupportedWhiteBalances; OSD.WhiteBalanceOSD.CurrentWhiteBalanceIndex = 0; // EV SupportedEVValues = new List<int>(); CameraCapturePropertyRange evRange = PhotoCaptureDevice.GetSupportedPropertyRange(_sensor, KnownCameraPhotoProperties.ExposureCompensation); int minEV = (int)evRange.Min; int maxEV = (int)evRange.Max; for (int i = minEV; i <= maxEV; i++) { SupportedEVValues.Add(i); } EVDialer.SupportedValues = SupportedEVValues; // ISO SupportedISOValues = new List<uint>(); SupportedISOValues.Add(ProCamConstraints.PROCAM_AUTO_ISO); CameraCapturePropertyRange isoRange = PhotoCaptureDevice.GetSupportedPropertyRange(_sensor, KnownCameraPhotoProperties.Iso); var minISO = (uint)isoRange.Min; var maxISO = (uint)isoRange.Max; foreach(var fixture in _supportedISOFixtures) { if(fixture >= minISO && fixture <= maxISO) { SupportedISOValues.Add(fixture); } } ISODialer.SupportedValues = SupportedISOValues; // Flash IReadOnlyList<object> flashList = PhotoCaptureDevice.GetSupportedPropertyValues(_sensor, KnownCameraPhotoProperties.FlashMode); SupportedFlashModes = new List<FlashState>(); foreach (object rawValue in flashList) { SupportedFlashModes.Add((FlashState)(uint)rawValue); } // Resolution OSD.MainOSD.SupportedResolutions = SupportedResolutions; OSD.MainOSD.CurrentResolution = CurrentResolution; // Scene modes SupportedSceneModes = new List<CameraSceneMode>(); IReadOnlyList<object> sceneList = PhotoCaptureDevice.GetSupportedPropertyValues(_sensor, KnownCameraPhotoProperties.SceneMode); foreach (object rawValue in sceneList) { SupportedSceneModes.Add((CameraSceneMode)(uint)rawValue); } OSD.SceneOSD.SupportedSceneModes = SupportedSceneModes; OSD.SceneOSD.CurrentIndex = 0; // Focus assist IReadOnlyList<object> focusList = PhotoCaptureDevice.GetSupportedPropertyValues(_sensor, KnownCameraPhotoProperties.FocusIlluminationMode); SupportedFocusAssistModes = new List<FocusIlluminationMode>(); foreach (object rawValue in focusList) { SupportedFocusAssistModes.Add((FocusIlluminationMode)(uint)rawValue); } OSD.FocusAssistOSD.SupportedModes = SupportedFocusAssistModes; OSD.FocusAssistOSD.CurrentIndex = 0; // Enable shutter sound cam.SetProperty(KnownCameraGeneralProperties.PlayShutterSoundOnCapture, true); // Create capture sequence seq = cam.CreateCaptureSequence(1); // Set video brush source ViewfinderBrush.SetSource(cam); CorrectViewfinderOrientation(Orientation); // Show UI chrome HideLoadingView(); // Events CameraButtons.ShutterKeyHalfPressed += OnShutterHalfPress; CameraButtons.ShutterKeyPressed += OnShutterFullPress; CameraButtons.ShutterKeyReleased += OnShutterReleased; }