/// <summary> /// Captures the photo to storage file asynchronously. /// </summary> /// <param name="folder">The folder.</param> /// <param name="fileName">Name of the file.</param> /// <param name="defaultExtension">The default extension.</param> /// <returns></returns> public async Task<StorageFile> CapturePhotoToStorageFileAsync(StorageFolder folder = null, string fileName = null, string defaultExtension = ".jpg") { if (_countdownControl != null && this.PhotoCaptureCountdownSeconds > 0) { #pragma warning disable 4014 _countdownControl.FadeInCustom(); await _countdownControl.StartCountdownAsync(this.PhotoCaptureCountdownSeconds); _countdownControl.FadeOutCustom(); #pragma warning restore 4014 } if (_flashAnimation != null) _flashAnimation.Begin(); if (folder == null) { folder = KnownFolders.PicturesLibrary; } if (fileName == null) { fileName = await folder.CreateTempFileNameAsync(defaultExtension); } var photoFile = await folder.CreateFileAsync( fileName, CreationCollisionOption.FailIfExists); ImageEncodingProperties imageEncodingProperties; switch (Path.GetExtension(fileName)) { case ".png": imageEncodingProperties = ImageEncodingProperties.CreatePng(); break; default: imageEncodingProperties = ImageEncodingProperties.CreateJpeg(); break; } try { await MediaCapture.CapturePhotoToStorageFileAsync(imageEncodingProperties, photoFile); } catch { OnCameraFailed(null, null); return null; } //using (var stream = await photoFile.OpenStreamForWriteAsync()) //using (var yas = stream.AsRandomAccessStream()) //{ // try // { // await MediaCapture.CapturePhotoToStreamAsync(imageEncodingProperties, yas); // } // catch // { // OnCameraFailed(null, null); // return null; // } //} return photoFile; }
/// <summary> /// Starts the video capture asynchronously. /// </summary> /// <param name="folder">The folder.</param> /// <param name="fileName">Name of the file.</param> /// <returns></returns> /// <exception cref="System.InvalidOperationException"></exception> public async Task<StorageFile> StartVideoCaptureAsync(StorageFolder folder = null, string fileName = null) { if (_internalState == CameraCaptureControlStates.Recording) { // Note: Already recording - ignoring the folder and filename and returning result of previous request. await _recordingTaskSource.Task; return _videoFile; } if (_internalState != CameraCaptureControlStates.Shown) { var result = await ShowAsync(); if (!result.Success) { //TODO: Add error handling here. return null; } } _internalState = CameraCaptureControlStates.Recording; _recordingTaskSource = new TaskCompletionSource<bool>(false); if (MediaCapture == null) { throw new InvalidOperationException(); } if (folder == null) { folder = KnownFolders.VideosLibrary; } if (fileName == null) { fileName = await folder.CreateTempFileNameAsync(".mp4"); } _videoFile = await folder.CreateFileAsync( fileName, CreationCollisionOption.FailIfExists); if (_recordingIndicator != null) { _recordingIndicator.Visibility = Visibility.Visible; } if (_recordingAnimation != null) { _recordingAnimation.Begin(); } var encodingProfile = Path.GetExtension(fileName).Equals(".wmv", StringComparison.OrdinalIgnoreCase) ? MediaEncodingProfile.CreateWmv(VideoEncodingQuality) : MediaEncodingProfile.CreateMp4(VideoEncodingQuality); await MediaCapture.StartRecordToStorageFileAsync(encodingProfile, _videoFile); await _recordingTaskSource.Task; _recordingTaskSource = null; if (_recordingAnimation != null) { _recordingAnimation.Stop(); } if (_recordingIndicator != null) { _recordingIndicator.Visibility = Visibility.Collapsed; } _internalState = CameraCaptureControlStates.Shown; return _videoFile; }
public async Task<StorageFile> CapturePhotoToStorageFileAsync(StorageFolder folder = null, string fileName = null) { if (_countdownControl != null && this.PhotoCaptureCountdownSeconds > 0) { _countdownControl.FadeInCustom(); await _countdownControl.StartCountdown(this.PhotoCaptureCountdownSeconds); _countdownControl.FadeOutCustom(); } if (_flashAnimation != null) _flashAnimation.Begin(); if (folder == null) { folder = KnownFolders.PicturesLibrary; } if (fileName == null) { fileName = await folder.CreateTempFileNameAsync(".png"); } var photoFile = await folder.CreateFileAsync( fileName, CreationCollisionOption.FailIfExists); ImageEncodingProperties imageEncodingProperties; switch (Path.GetExtension(fileName)) { case ".png": imageEncodingProperties = ImageEncodingProperties.CreatePng(); break; default: imageEncodingProperties = ImageEncodingProperties.CreateJpeg(); break; } await _mediaCapture.CapturePhotoToStorageFileAsync(imageEncodingProperties, photoFile); return photoFile; }