/// <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); } return(photoFile); }
/// <summary> /// Creates a temporary file. /// </summary> /// <param name="folder"></param> /// <param name="extension"></param> /// <returns></returns> public static async Task <StorageFile> CreateTempFileAsync(this StorageFolder folder, string extension = ".tmp") { if (folder == null) { folder = ApplicationData.Current.TemporaryFolder; } var fileName = await folder.CreateTempFileNameAsync( extension, DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss.ffffff")); var file = await folder.CreateFileAsync( fileName, CreationCollisionOption.GenerateUniqueName); return(file); }
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); } _internalState = CameraCaptureControlStates.Recording; _recordingTaskSource = new TaskCompletionSource <bool>(false); if (_internalState != CameraCaptureControlStates.Shown) { await ShowAsync(); } 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); }