/// <summary> /// Take a video with specified options /// </summary> /// <param name="options">Video Media Options</param> /// <param name="token">Cancellation token (currently ignored)</param> /// <returns>Media file of new video or null if canceled</returns> public async Task <MediaFile> TakeVideoAsync(StoreVideoOptions options, CancellationToken token = default(CancellationToken)) { if (!initialized) { await Initialize(); } if (!IsCameraAvailable) { throw new NotSupportedException(); } options.VerifyOptions(); var capture = new CameraCaptureUI(); capture.VideoSettings.MaxResolution = GetResolutionFromQuality(options.Quality); capture.VideoSettings.AllowTrimming = options?.AllowCropping ?? true; if (capture.VideoSettings.AllowTrimming) { capture.VideoSettings.MaxDurationInSeconds = (float)options.DesiredLength.TotalSeconds; } capture.VideoSettings.Format = CameraCaptureUIVideoFormat.Mp4; var result = await capture.CaptureFileAsync(CameraCaptureUIMode.Video); if (result == null) { return(null); } string aPath = null; if (options?.SaveToAlbum ?? false) { try { var fileNameNoEx = Path.GetFileNameWithoutExtension(result.Path); var copy = await result.CopyAsync(KnownFolders.VideosLibrary, fileNameNoEx + result.FileType, NameCollisionOption.GenerateUniqueName); aPath = copy.Path; } catch (Exception ex) { Debug.WriteLine("unable to save to album:" + ex); } } return(new MediaFile(result.Path, () => result.OpenStreamForReadAsync().Result, albumPath: aPath)); }
/// <summary> /// Take a photo async with specified options /// </summary> /// <param name="options">Camera Media Options</param> /// <param name="token">Cancellation token (currently ignored)</param> /// <returns>Media file of photo or null if canceled</returns> public async Task <MediaFile> TakePhotoAsync(StoreCameraMediaOptions options, CancellationToken token = default(CancellationToken)) { if (!initialized) { await Initialize(); } if (!IsCameraAvailable) { throw new NotSupportedException(); } options.VerifyOptions(); var capture = new CameraCaptureUI(); //capture.PhotoSettings.CroppedAspectRatio = new Size(16, 9); capture.PhotoSettings.Format = CameraCaptureUIPhotoFormat.Jpeg; capture.PhotoSettings.MaxResolution = GetMaxResolution(options.PhotoSize, options.CustomPhotoSize, options.MaxWidthHeight ?? 0); if (options.AllowCropping ?? false) { capture.PhotoSettings.AllowCropping = true; capture.PhotoSettings.CroppedAspectRatio = new Size(16, 9); } else { capture.PhotoSettings.AllowCropping = false; } var result = await capture.CaptureFileAsync(CameraCaptureUIMode.Photo); if (result == null) { return(null); } if (options.SaveToAlbum) { await SaveToAlbum(result, KnownFolders.SavedPictures, options.Directory, options.Name); } if (IsValidFileName(options.Name)) { var name = EnsureCorrectExtension(options.Name, result.FileType); await result.RenameAsync(name, NameCollisionOption.GenerateUniqueName); } await ResizeAsync(result, options); return(MediaFileFromFile(result)); }
/// <summary> /// Take a video with specified options /// </summary> /// <param name="options">Video Media Options</param> /// <param name="token">Cancellation token (currently ignored)</param> /// <returns>Media file of new video or null if canceled</returns> public async Task <MediaFile> TakeVideoAsync(StoreVideoOptions options, CancellationToken token = default(CancellationToken)) { if (!initialized) { await Initialize(); } if (!IsCameraAvailable) { throw new NotSupportedException(); } options.VerifyOptions(); var capture = new CameraCaptureUI(); capture.VideoSettings.MaxResolution = GetResolutionFromQuality(options.Quality); capture.VideoSettings.AllowTrimming = options?.AllowCropping ?? true; if (capture.VideoSettings.AllowTrimming) { capture.VideoSettings.MaxDurationInSeconds = (float)options.DesiredLength.TotalSeconds; } capture.VideoSettings.Format = CameraCaptureUIVideoFormat.Mp4; var result = await capture.CaptureFileAsync(CameraCaptureUIMode.Video); if (result == null) { return(null); } string aPath = null; if (options.SaveToAlbum) { await SaveToAlbum(result, KnownFolders.VideosLibrary, options.Directory, options.Name); } return(new MediaFile(result.Path, () => result.OpenStreamForReadAsync().Result, albumPath: aPath)); }
/// <summary> /// Take a photo async with specified options /// </summary> /// <param name="options">Camera Media Options</param> /// <param name="token">Cancellation token (currently ignored)</param> /// <returns>Media file of photo or null if canceled</returns> public async Task <MediaFile> TakePhotoAsync(StoreCameraMediaOptions options, CancellationToken token = default(CancellationToken)) { if (!initialized) { await Initialize(); } if (!IsCameraAvailable) { throw new NotSupportedException(); } options.VerifyOptions(); var capture = new CameraCaptureUI(); capture.PhotoSettings.Format = CameraCaptureUIPhotoFormat.Jpeg; capture.PhotoSettings.MaxResolution = GetMaxResolution(options?.PhotoSize ?? PhotoSize.Full, options?.CustomPhotoSize ?? 100); //we can only disable cropping if resolution is set to max if (capture.PhotoSettings.MaxResolution == CameraCaptureUIMaxPhotoResolution.HighestAvailable) { capture.PhotoSettings.AllowCropping = options?.AllowCropping ?? true; } var result = await capture.CaptureFileAsync(CameraCaptureUIMode.Photo); if (result == null) { return(null); } var folder = ApplicationData.Current.LocalFolder; var path = options.GetFilePath(folder.Path); var directoryFull = Path.GetDirectoryName(path); var newFolder = directoryFull.Replace(folder.Path, string.Empty); if (!string.IsNullOrWhiteSpace(newFolder)) { await folder.CreateFolderAsync(newFolder, CreationCollisionOption.OpenIfExists); } folder = await StorageFolder.GetFolderFromPathAsync(directoryFull); var filename = Path.GetFileName(path); string aPath = null; if (options?.SaveToAlbum ?? false) { try { var fileNameNoEx = Path.GetFileNameWithoutExtension(path); var copy = await result.CopyAsync(KnownFolders.PicturesLibrary, fileNameNoEx + result.FileType, NameCollisionOption.GenerateUniqueName); aPath = copy.Path; } catch (Exception ex) { Debug.WriteLine("unable to save to album:" + ex); } } var file = await result.CopyAsync(folder, filename, NameCollisionOption.GenerateUniqueName).AsTask(); return(new MediaFile(file.Path, () => file.OpenStreamForReadAsync().Result, albumPath: aPath)); }