public void CapturarFoto() { var captura = new MediaPicker(); if (captura.IsCameraAvailable) { MediaPickerController controller = captura.GetTakePhotoUI(new StoreCameraMediaOptions { Name = string.Format("foto_{0}.jpg", DateTime.Now.ToString()), Directory = "Aplicativo" }); TaskScheduler.FromCurrentSynchronizationContext(); } }
async void TakePicture() { var picker = new MediaPicker(); if (picker.IsCameraAvailable) { MediaPickerController controller = picker.GetTakePhotoUI(new StoreCameraMediaOptions { Name = string.Format("{0}.jpg", Guid.NewGuid()), Directory = "Pictures" }); // On iPad, you'll use UIPopoverController to present the controller PresentViewController(controller, true, null); await controller.GetResultAsync().ContinueWith(t => { // We need to dismiss the controller ourselves DismissViewController(true, () => { // User canceled or something went wrong if (t.IsCanceled || t.IsFaulted) { return; } // We get back a MediaFile MediaFile media = t.Result; //ShowPhoto(media); MemoryStream mstr = new MemoryStream(); media.GetStream().CopyTo(mstr); if (_basePage != null) { _basePage.ImagePath = media.Path; _basePage.bytes = ResizeTheImage(mstr.ToArray(), 400, 400); _basePage.Source = ImageSource.FromStream(() => new MemoryStream(_basePage.bytes)); } }); // Make sure we use the UI thread to show our photo. }, TaskScheduler.FromCurrentSynchronizationContext()); } }
async void OnActionSheetClicked(object sender, UIButtonEventArgs e) { MediaPickerController controller = null; try { if (e.ButtonIndex == 0) { if (!picker.IsCameraAvailable) { new UIAlertView("Oops!", "Sorry, camera not supported on this device!", null, "Ok").Show(); return; } controller = picker.GetTakePhotoUI(new StoreCameraMediaOptions()); PresentViewController(controller, true, null); var file = await controller.GetResultAsync(); messageViewModel.Image = file.Path; Send(); } else if (e.ButtonIndex == 1) { controller = picker.GetPickPhotoUI(); PresentViewController(controller, true, null); var file = await controller.GetResultAsync(); messageViewModel.Image = file.Path; Send(); } } catch (TaskCanceledException) { //Means the user just cancelled } finally { controller?.DismissViewController(true, null); } }
void PresentCamera() { var picker = new MediaPicker(); MediaPickerController controller = picker.GetTakePhotoUI(new StoreCameraMediaOptions { Name = "scavengerhunt.jpg", Directory = "CouchbaseConnect" }); PresentViewController(controller, true, null); controller.GetResultAsync().ContinueWith(t => { // Dismiss the UI yourself controller.DismissViewController(true, () => { try { if (t.Status != TaskStatus.Canceled) { MediaFile file = t.Result; ((CameraContentPage)this.Element).Captured = file.Path; } } catch (AggregateException aggX) { if (aggX.InnerException is TaskCanceledException) { //nothing Console.WriteLine("Task Cancelled"); } else { throw; } } }); }, TaskScheduler.FromCurrentSynchronizationContext()); ((CameraContentPage)this.Element).IsPresented = false; }
public override void ViewDidLoad() { base.ViewDidLoad(); TakePictureButton.TouchUpInside += (object sender, EventArgs e) => { /* * if (!mediaPicker.IsCameraAvailable) * { * alertView = new UIAlertView ("Ray's Hot Dogs", "Sadly, you can't take pictures with your device :-(", * new UIAlertViewDelegate(), "OK"); * alertView.Show(); * * return; * } */ mediaPickerController = mediaPicker.GetTakePhotoUI(new StoreCameraMediaOptions { Name = "hotdogselfie.jpg", Directory = "RaysHotDogsSelfies" }); dialogController.PresentViewController(mediaPickerController, true, null); mediaPickerController.GetResultAsync().ContinueWith(t => { dialogController.DismissViewController(true, () => { if (t.IsCanceled || t.IsFaulted) { return; } MediaFile media = t.Result; ShowHotDogSelfie(media); }); }, uiScheduler); }; }
async void SelectFromGallery() { var picker = new MediaPicker(); MediaPickerController controller = picker.GetPickPhotoUI(); // On iPad, you'll use UIPopoverController to present the controller PresentViewController(controller, true, null); //var result = await controller.GetResultAsync(); //controller.DismissViewController(true, null); //MediaFile file = result; //_page.ImagePath = file.Path; await controller.GetResultAsync().ContinueWith(t => { // We need to dismiss the controller ourselves controller.DismissViewController(true, () => { // User canceled or something went wrong if (t.IsCanceled || t.IsFaulted) { return; } MediaFile media = t.Result; MemoryStream mstr = new MemoryStream(); media.GetStream().CopyTo(mstr); if (_basePage != null) { _basePage.ImagePath = media.Path; _basePage.bytes = ResizeTheImage(mstr.ToArray(), 800, 800); _basePage.Source = ImageSource.FromStream(() => new MemoryStream(_basePage.bytes)); } }); // Make sure we use the UI thread to show our photo. }, TaskScheduler.FromCurrentSynchronizationContext()); }
public void GetImageAsync(Action <MediaFile> imageData, bool fromCamera = true) { var picker = new MediaPicker(); MediaPickerController controller = null; if (fromCamera) { controller = picker.GetTakePhotoUI(new StoreCameraMediaOptions { Name = string.Format("vDoers_{0}.jpg", DateTime.Now.Ticks), Directory = "vDoersCamera" }); } else { controller = picker.GetPickPhotoUI(); } var window = UIApplication.SharedApplication.KeyWindow; var vc = window.RootViewController; if (vc != null) { // vc = vc.PresentedViewController; vc.PresentViewController(controller, true, null); } controller.GetResultAsync().ContinueWith(t => { // Dismiss the UI yourself controller.DismissViewController(true, () => { if (imageData != null) { imageData(t.Result); } }); }, TaskScheduler.FromCurrentSynchronizationContext()); }
// protected override void OnElementChanged (VisualElementChangedEventArgs e) // { // base.OnElementChanged (e); // // var view = NativeView; // var viewController = ViewController; // // // Get the device's display for width and height. // RectangleF screen = UIScreen.MainScreen.Bounds; // // } public override void ViewDidAppear(bool animated) { base.ViewDidAppear(animated); // Create media picker/camera var mediaPicker = new MediaPicker(); MediaPickerController cameraController = mediaPicker.GetTakePhotoUI(new StoreCameraMediaOptions { Name = "TestImage.jpg", Directory = "Couchbase Scavenger Hunt", DefaultCamera = CameraDevice.Rear }); PresentViewController(cameraController, true, null); cameraController.GetResultAsync().ContinueWith(t => { cameraController.DismissViewController(true, () => { MediaFile file = t.Result; Console.WriteLine("--> file: {0}", file.Path); }); }, TaskScheduler.FromCurrentSynchronizationContext()); }
public override bool FinishedLaunching(UIApplication app, NSDictionary options) { pickPhoto = new StringElement("Pick Photo"); pickPhoto.Tapped += () => { mediaPickerController = mediaPicker.GetPickPhotoUI(); dialogController.PresentViewController(mediaPickerController, true, null); mediaPickerController.GetResultAsync().ContinueWith(t => { // We need to dismiss the controller ourselves dialogController.DismissViewController(true, () => { // User canceled or something went wrong if (t.IsCanceled || t.IsFaulted) { return; } // We get back a MediaFile MediaFile media = t.Result; ShowPhoto(media); }); }, uiScheduler); // Make sure we use the UI thread to show our photo. }; takePhoto = new StringElement("Take Photo"); takePhoto.Tapped += () => { // Make sure we actually have a camera if (!mediaPicker.IsCameraAvailable) { ShowUnsupported(); return; } // When capturing new media, we can specify it's name and location mediaPickerController = mediaPicker.GetTakePhotoUI(new StoreCameraMediaOptions { Name = "test.jpg", Directory = "MediaPickerSample" }); dialogController.PresentViewController(mediaPickerController, true, null); mediaPickerController.GetResultAsync().ContinueWith(t => { // We need to dismiss the controller ourselves dialogController.DismissViewController(true, () => { // User canceled or something went wrong if (t.IsCanceled || t.IsFaulted) { return; } // We get back a MediaFile MediaFile media = t.Result; ShowPhoto(media); }); }, uiScheduler); // Make sure we use the UI thread to show our photo. }; takeVideo = new StringElement("Take Video"); takeVideo.Tapped += () => { // Make sure video is supported and a camera is available if (!mediaPicker.VideosSupported || !mediaPicker.IsCameraAvailable) { ShowUnsupported(); return; } // When capturing video, we can hint at the desired quality and length. // DesiredLength is only a hint, however, and the resulting video may // be longer than desired. mediaPickerController = mediaPicker.GetTakeVideoUI(new StoreVideoOptions { Quality = VideoQuality.Medium, DesiredLength = TimeSpan.FromSeconds(10), Directory = "MediaPickerSample", Name = "test.mp4" }); dialogController.PresentViewController(mediaPickerController, true, null); mediaPickerController.GetResultAsync().ContinueWith(t => { // We need to dismiss the controller ourselves dialogController.DismissViewController(true, () => { // User canceled or something went wrong if (t.IsCanceled || t.IsFaulted) { return; } // We get back a MediaFile MediaFile media = t.Result; ShowVideo(media); }); }, uiScheduler); // Make sure we use the UI thread to show our video. }; pickVideo = new StringElement("Pick Video"); pickVideo.Tapped += () => { if (!mediaPicker.VideosSupported) { ShowUnsupported(); return; } mediaPickerController = mediaPicker.GetPickVideoUI(); dialogController.PresentViewController(mediaPickerController, true, null); mediaPickerController.GetResultAsync().ContinueWith(t => { // We need to dismiss the controller ourselves dialogController.DismissViewController(true, () => { // User canceled or something went wrong if (t.IsCanceled || t.IsFaulted) { return; } // We get back a MediaFile MediaFile media = t.Result; ShowVideo(media); }); }, uiScheduler); // Make sure we use the UI thread to show our video. }; var root = new RootElement("Xamarin.Media Sample") { new Section("Picking media") { pickPhoto, pickVideo }, new Section("Capturing media") { takePhoto, takeVideo } }; dialogController = new DisposingMediaViewController(root); viewController = new UINavigationController(dialogController); window = new UIWindow(UIScreen.MainScreen.Bounds); window.RootViewController = viewController; window.MakeKeyAndVisible(); return(true); }
public override async void ViewDidLoad() { base.ViewDidLoad(); if (string.IsNullOrEmpty(fileName)) { fileName = "in-progress"; var picker = new MediaPicker(); if (!picker.IsCameraAvailable || !picker.PhotosSupported) { Console.WriteLine("No camera!"); } else { var options = new StoreCameraMediaOptions { Name = DateTime.Now.ToString("yyyyMMddHHmmss"), Directory = "MediaPickerSample" }; pickerController = picker.GetTakePhotoUI(options); PresentViewController(pickerController, true, null); MediaFile media; try { var pickerTask = pickerController.GetResultAsync(); await pickerTask; // User canceled or something went wrong if (pickerTask.IsCanceled || pickerTask.IsFaulted) { fileName = string.Empty; return; } media = pickerTask.Result; fileName = media.Path; // We need to dismiss the controller ourselves await DismissViewControllerAsync(true); // woot! async-ified iOS method } catch (AggregateException ae) { fileName = string.Empty; Console.WriteLine("Error while huh: {0}", ae.Message); } catch (Exception e) { fileName = string.Empty; Console.WriteLine("Error while cancelling: {0}", e.Message); } if (String.IsNullOrEmpty(fileName)) { await DismissViewControllerAsync(true); } else { PhotoImageView.Image = new UIImage(fileName); SavePicture(fileName); } } } else if (fileName == "cancelled") { NavigationController.PopToRootViewController(true); } else { // populate screen with existing item PhotoImageView.Image = FileExists(fileName) ? new UIImage(fileName) : null; LocationText.Text = location; } var locator = new Geolocator { DesiredAccuracy = 50 }; var position = await locator.GetPositionAsync(new CancellationToken(), false); Console.WriteLine("Position Latitude: {0}", position.Latitude); Console.WriteLine("Position Longitude: {0}", position.Longitude); location = string.Format("{0},{1}", position.Latitude, position.Longitude); LocationText.Text = location; }
public WelcomePage() { Title = "QuickUI Demo"; Content = mainLayout = new StackLayout { Padding = new Thickness(20, 20, 20, 100), Spacing = 10, Orientation = StackLayout.StackOrientation.Vertical, VerticalOptions = LayoutOptions.Center }; mainLayout.Add(new Label { Text = "Welcome to the QuickUI Demo App!", XAlign = TextAlignment.Center }); mainLayout.Add(cameraButton1 = new Button() { Text = "Take Picture With MediaPickerController", IsEnabled = true, MinimumWidthRequest = 100, MinimumHeightRequest = 100 }); mainLayout.Add(cameraButton2 = new Button() { Text = "Take Picture With MediaPicker TakePhotoAsync.", IsEnabled = true, MinimumWidthRequest = 100, MinimumHeightRequest = 100 }); cameraButton1.Activated += (sender, args) => { var picker = new MediaPicker(); MediaPickerController controller = picker.GetTakePhotoUI(new StoreCameraMediaOptions { Name = "test.jpg", Directory = "MediaPickerSample" }); controller.ModalPresentationStyle = UIModalPresentationStyle.FullScreen; controller.ModalTransitionStyle = UIModalTransitionStyle.CoverVertical; var pageController = this.GetViewController(); //I tried to add the controller as a child to the pagecontroller, but it hangs the app //pageController.AddChildViewController(controller); pageController.PresentViewController(controller, true, null); }; cameraButton2.Activated += (sender, args) => { var picker = new MediaPicker(); picker.TakePhotoAsync(new StoreCameraMediaOptions() { Name = "test.jpg" }).ContinueWith(t => { MediaFile file = t.Result; Console.WriteLine(file.Path); }, TaskScheduler.FromCurrentSynchronizationContext()); }; }
public override async void ViewWillAppear(bool animated) { base.ViewWillAppear(animated); if (fileName == "") { fileName = "in-progress"; var picker = new MediaPicker(); // new MediaPicker (this); on Android if (!picker.IsCameraAvailable || !picker.PhotosSupported) { Console.WriteLine("No camera!"); } else { var options = new StoreCameraMediaOptions { Name = DateTime.Now.ToString("yyyyMMddHHmmss"), Directory = "MediaPickerSample" }; #if !VISUALSTUDIO #region new style pickerController = picker.GetTakePhotoUI(options); PresentViewController(pickerController, true, null); var pickerTask = pickerController.GetResultAsync(); await pickerTask; // We need to dismiss the controller ourselves await DismissViewControllerAsync(true); // woot! async-ified iOS method // User canceled or something went wrong if (pickerTask.IsCanceled || pickerTask.IsFaulted) { return; } // We get back a MediaFile MediaFile media = pickerTask.Result; fileName = media.Path; PhotoImageView.Image = new UIImage(fileName); SavePicture(fileName); #endregion #else #region old style (deprecated) var t = picker.TakePhotoAsync(options); //.ContinueWith (t => { await t; if (t.IsCanceled) { Console.WriteLine("User canceled"); fileName = "cancelled"; //InvokeOnMainThread(() =>{ NavigationController.PopToRootViewController(false); //}); return; } Console.WriteLine(t.Result.Path); fileName = t.Result.Path; //InvokeOnMainThread(() =>{ PhotoImageView.Image = new UIImage(fileName); //}); SavePicture(fileName); //}); #endregion #endif } } else if (fileName == "cancelled") { NavigationController.PopToRootViewController(true); } else { // populate screen with existing item PhotoImageView.Image = new UIImage(fileName); LocationText.Text = location; } var locator = new Geolocator { DesiredAccuracy = 50 }; // new Geolocator (this) { ... }; on Android var position = await locator.GetPositionAsync(timeout : 10000); //.ContinueWith (p => { Console.WriteLine("Position Latitude: {0}", position.Latitude); Console.WriteLine("Position Longitude: {0}", position.Longitude); location = string.Format("{0},{1}", position.Latitude, position.Longitude); LocationText.Text = location; }
public override bool FinishedLaunching(UIApplication app, NSDictionary options) { pickPhoto = new StringElement("Pick Photo"); pickPhoto.Tapped += () => { mediaPickerController = mediaPicker.GetPickPhotoUI(); dialogController.PresentViewController(mediaPickerController, true, null); mediaPickerController.GetResultAsync().ContinueWith(t => { // We need to dismiss the controller ourselves dialogController.DismissViewController(true, () => { // User canceled or something went wrong if (t.IsCanceled || t.IsFaulted) { return; } // We get back a MediaFile MediaFile media = t.Result; ShowPhoto(media); }); }, uiScheduler); // Make sure we use the UI thread to show our photo. }; takePhoto = new StringElement("Take Photo"); takePhoto.Tapped += () => { // Make sure we actually have a camera if (!mediaPicker.IsCameraAvailable) { ShowUnsupported(); return; } // When capturing new media, we can specify it's name and location mediaPickerController = mediaPicker.GetTakePhotoUI(new StoreCameraMediaOptions { Name = "test.jpg", Directory = "MediaPickerSample" }); dialogController.PresentViewController(mediaPickerController, true, null); mediaPickerController.GetResultAsync().ContinueWith(t => { // We need to dismiss the controller ourselves dialogController.DismissViewController(true, () => { // User canceled or something went wrong if (t.IsCanceled || t.IsFaulted) { return; } // We get back a MediaFile MediaFile media = t.Result; ShowPhoto(media); }); }, uiScheduler); // Make sure we use the UI thread to show our photo. }; takeVideo = new StringElement("Take Video"); takeVideo.Tapped += () => { // Make sure video is supported and a camera is available if (!mediaPicker.VideosSupported || !mediaPicker.IsCameraAvailable) { ShowUnsupported(); return; } // When capturing video, we can hint at the desired quality and length. // DesiredLength is only a hint, however, and the resulting video may // be longer than desired. mediaPickerController = mediaPicker.GetTakeVideoUI(new StoreVideoOptions { Quality = VideoQuality.Medium, DesiredLength = TimeSpan.FromSeconds(10), Directory = "MediaPickerSample", Name = "test.mp4" }); dialogController.PresentViewController(mediaPickerController, true, null); mediaPickerController.GetResultAsync().ContinueWith(t => { // We need to dismiss the controller ourselves dialogController.DismissViewController(true, () => { // User canceled or something went wrong if (t.IsCanceled || t.IsFaulted) { return; } // We get back a MediaFile MediaFile media = t.Result; ShowVideo(media); }); }, uiScheduler); // Make sure we use the UI thread to show our video. }; pickVideo = new StringElement("Pick Video"); pickVideo.Tapped += () => { if (!mediaPicker.VideosSupported) { ShowUnsupported(); return; } mediaPickerController = mediaPicker.GetPickVideoUI(); dialogController.PresentViewController(mediaPickerController, true, null); mediaPickerController.GetResultAsync().ContinueWith(t => { // We need to dismiss the controller ourselves dialogController.DismissViewController(true, () => { // User canceled or something went wrong if (t.IsCanceled || t.IsFaulted) { return; } // We get back a MediaFile MediaFile media = t.Result; ShowVideo(media); }); }, uiScheduler); // Make sure we use the UI thread to show our video. }; Action pickPhotoAction = async() => { try { var mediaFile = await mediaPicker.PickPhotoAsync(); ShowPhoto(mediaFile); } catch (TaskCanceledException) { } }; Action takePhotoAction = async() => { if (!mediaPicker.VideosSupported || !mediaPicker.IsCameraAvailable) { ShowUnsupported(); return; } try { var mediaFile = await mediaPicker.TakePhotoAsync(new StoreCameraMediaOptions { Name = "test.jpg", Directory = "MediaPickerSample" }); ShowPhoto(mediaFile); } catch (TaskCanceledException) { } }; useActionSheet = new StringElement("Use Action Sheet"); useActionSheet.Tapped += () => { if (UIDevice.CurrentDevice.CheckSystemVersion(8, 0)) { var alertContoller = UIAlertController.Create("Show Photo From?", string.Empty, UIAlertControllerStyle.ActionSheet); alertContoller.AddAction(UIAlertAction.Create("Pick Photo", UIAlertActionStyle.Default, (action) => pickPhotoAction())); alertContoller.AddAction(UIAlertAction.Create("Take Photo", UIAlertActionStyle.Default, (action) => takePhotoAction())); if (UIDevice.CurrentDevice.UserInterfaceIdiom != UIUserInterfaceIdiom.Pad) { alertContoller.AddAction(UIAlertAction.Create("Cancel", UIAlertActionStyle.Cancel, null)); } if (alertContoller.PopoverPresentationController != null) { alertContoller.PopoverPresentationController.PermittedArrowDirections = 0; var rect = viewController.View.Bounds; alertContoller.PopoverPresentationController.SourceRect = rect; alertContoller.PopoverPresentationController.SourceView = viewController.View; } viewController.PresentViewController(alertContoller, true, null); } else { var actionSheet = new UIActionSheet("Show Photo From?"); actionSheet.AddButton("Pick Photo"); actionSheet.AddButton("Take Photo"); if (UIDevice.CurrentDevice.UserInterfaceIdiom != UIUserInterfaceIdiom.Pad) { actionSheet.AddButton("Cancel"); actionSheet.CancelButtonIndex = 2; } actionSheet.Clicked += (object sender, UIButtonEventArgs e) => { if (e.ButtonIndex == 0) { pickPhotoAction(); } if (e.ButtonIndex == 1) { takePhotoAction(); } }; actionSheet.ShowInView(viewController.View); } }; var root = new RootElement("Xamarin.Media Sample") { new Section("Picking media") { pickPhoto, pickVideo }, new Section("Capturing media") { takePhoto, takeVideo }, new Section("Action Sheets") { useActionSheet } }; dialogController = new DisposingMediaViewController(root); viewController = new UINavigationController(dialogController); window = new UIWindow(UIScreen.MainScreen.Bounds); window.RootViewController = viewController; window.MakeKeyAndVisible(); return(true); }