internal MediaPickerDelegate (UIViewController viewController, UIImagePickerControllerSourceType sourceType, StoreCameraMediaOptions options) { this.viewController = viewController; this.source = sourceType; this.options = options ?? new StoreCameraMediaOptions(); if (viewController != null) { UIDevice.CurrentDevice.BeginGeneratingDeviceOrientationNotifications(); this.observer = NSNotificationCenter.DefaultCenter.AddObserver (UIDevice.OrientationDidChangeNotification, DidRotate); } }
/// <summary> /// Take a photo async with specified options /// </summary> /// <param name="options">Camera Media Options</param> /// <returns>Media file of photo or null if canceled</returns> public async Task<MediaFile> TakePhotoAsync(StoreCameraMediaOptions options) { if (!IsCameraAvailable) throw new NotSupportedException(); options.VerifyOptions(); var capture = new CameraCaptureUI(); var result = await capture.CaptureFileAsync(CameraCaptureUIMode.Photo, options); if (result == null) return null; StorageFolder folder = ApplicationData.Current.LocalFolder; string 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); string filename = Path.GetFileName(path); var file = await result.CopyAsync(folder, filename, NameCollisionOption.GenerateUniqueName).AsTask(); return new MediaFile(file.Path, () => file.OpenStreamForReadAsync().Result); }
/// <summary> /// Take a photo async with specified options /// </summary> /// <param name="options">Camera Media Options</param> /// <returns>Media file of photo or null if canceled</returns> public Task<MediaFile> TakePhotoAsync(StoreCameraMediaOptions options) { if (!IsCameraAvailable) throw new NotSupportedException(); options.VerifyOptions(); var ntcs = new TaskCompletionSource<MediaFile>(options); if (Interlocked.CompareExchange(ref completionSource, ntcs, null) != null) throw new InvalidOperationException("Only one operation can be active at a time"); this.cameraCapture.Show(); return ntcs.Task; }
private Task<MediaFile> GetMediaAsync(UIImagePickerControllerSourceType sourceType, string mediaType, StoreCameraMediaOptions options = null) { UIWindow window = UIApplication.SharedApplication.KeyWindow; if (window == null) throw new InvalidOperationException("There's no current active window"); UIViewController viewController = window.RootViewController; if (viewController == null) { window = UIApplication.SharedApplication.Windows.OrderByDescending(w => w.WindowLevel).FirstOrDefault(w => w.RootViewController != null); if (window == null) throw new InvalidOperationException("Could not find current view controller"); else viewController = window.RootViewController; } while (viewController.PresentedViewController != null) viewController = viewController.PresentedViewController; MediaPickerDelegate ndelegate = new MediaPickerDelegate(viewController, sourceType, options); var od = Interlocked.CompareExchange(ref this.pickerDelegate, ndelegate, null); if (od != null) throw new InvalidOperationException("Only one operation can be active at at time"); var picker = SetupController(ndelegate, sourceType, mediaType, options); if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Pad && sourceType == UIImagePickerControllerSourceType.PhotoLibrary) { ndelegate.Popover = new UIPopoverController(picker); ndelegate.Popover.Delegate = new MediaPickerPopoverDelegate(ndelegate, picker); ndelegate.DisplayPopover(); } else viewController.PresentViewController(picker, true, null); return ndelegate.Task.ContinueWith(t => { if (this.popover != null) { this.popover.Dispose(); this.popover = null; } Interlocked.Exchange(ref this.pickerDelegate, null); return t; }).Unwrap(); }
/// <summary> /// This method takes a picture. /// Right now the parameter is not evaluated. /// </summary> /// <param name="mode"></param> /// <param name="options"></param> /// <returns></returns> public async Task<StorageFile> CaptureFileAsync(CameraCaptureUIMode mode, StoreCameraMediaOptions options) { var t = IsStopped(); this.options = options; // Create new MediaCapture MyMediaCapture = new MediaCapture(); var videoDevices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture); var backCamera = videoDevices.FirstOrDefault( item => item.EnclosureLocation != null && item.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Back); var frontCamera = videoDevices.FirstOrDefault( item => item.EnclosureLocation != null && item.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Front); var captureSettings = new MediaCaptureInitializationSettings(); if(options.DefaultCamera == CameraDevice.Front && frontCamera != null) { captureSettings.VideoDeviceId = frontCamera.Id; } else if(options.DefaultCamera == CameraDevice.Rear && backCamera != null) { captureSettings.VideoDeviceId = backCamera.Id; ; } await MyMediaCapture.InitializeAsync(captureSettings); // Assign to Xaml CaptureElement.Source and start preview myCaptureElement.Source = MyMediaCapture; // show preview await MyMediaCapture.StartPreviewAsync(); // now wait until stopflag shows that someone took a picture await t; // picture has been taken // stop preview await CleanUpAsync(); // go back CurrentWindow.Content = originalFrame; mainGrid.Children.Remove(this); return file; }
private void VerifyCameraOptions(StoreCameraMediaOptions options) { VerifyOptions(options); if (!Enum.IsDefined(typeof(CameraDevice), options.DefaultCamera)) throw new ArgumentException("options.Camera is not a member of CameraDevice"); }
private static MediaPickerController SetupController(MediaPickerDelegate mpDelegate, UIImagePickerControllerSourceType sourceType, string mediaType, StoreCameraMediaOptions options = null) { var picker = new MediaPickerController(mpDelegate); picker.MediaTypes = new[] { mediaType }; picker.SourceType = sourceType; if (sourceType == UIImagePickerControllerSourceType.Camera) { picker.CameraDevice = GetUICameraDevice(options.DefaultCamera); if (mediaType == TypeImage) picker.CameraCaptureMode = UIImagePickerControllerCameraCaptureMode.Photo; else if (mediaType == TypeMovie) { StoreVideoOptions voptions = (StoreVideoOptions)options; picker.CameraCaptureMode = UIImagePickerControllerCameraCaptureMode.Video; picker.VideoQuality = GetQuailty(voptions.Quality); picker.VideoMaximumDuration = voptions.DesiredLength.TotalSeconds; } } return picker; }
/// <summary> /// Take a photo async with specified options /// </summary> /// <param name="options">Camera Media Options</param> /// <returns>Media file of photo or null if canceled</returns> public Task<MediaFile> TakePhotoAsync(StoreCameraMediaOptions options) { if (!IsTakePhotoSupported) throw new NotSupportedException(); if (!IsCameraAvailable) throw new NotSupportedException(); VerifyCameraOptions(options); return GetMediaAsync(UIImagePickerControllerSourceType.Camera, TypeImage, options); }
/// <summary> /// /// </summary> /// <param name="options"></param> /// <returns></returns> public MediaPickerController GetTakePhotoUI(StoreCameraMediaOptions options) { if (!IsTakePhotoSupported) throw new NotSupportedException(); if (!IsCameraAvailable) throw new NotSupportedException(); VerifyCameraOptions(options); var d = new MediaPickerDelegate(null, UIImagePickerControllerSourceType.PhotoLibrary, options); return SetupController(d, UIImagePickerControllerSourceType.Camera, TypeImage, options); }
public Task<MediaFile> TakePhotoAsync(StoreCameraMediaOptions options) { return _media.TakePhotoAsync(options); }
/// <summary> /// /// </summary> /// <param name="options"></param> /// <returns></returns> public Intent GetTakePhotoUI (StoreCameraMediaOptions options) { if (!IsCameraAvailable) throw new NotSupportedException(); VerifyOptions (options); int id = GetRequestId(); return CreateMediaIntent (id, "image/*", MediaStore.ActionImageCapture, options, tasked: false); }
/// <summary> /// Take a photo async with specified options /// </summary> /// <param name="options">Camera Media Options</param> /// <returns>Media file of photo or null if canceled</returns> public Task<Media.Plugin.Abstractions.MediaFile> TakePhotoAsync(StoreCameraMediaOptions options) { if (!IsCameraAvailable) throw new NotSupportedException(); VerifyOptions (options); return TakeMediaAsync ("image/*", MediaStore.ActionImageCapture, options); }