예제 #1
0
        public override bool FinishedLaunching(UIApplication app, NSDictionary options)
        {
            global::Xamarin.Forms.Forms.Init();

            LoadApplication(new App());

            //Add the camera options for ios after LoadApplication

            //var imagePicker = new UIImagePickerController { SourceType = UIImagePickerControllerSourceType.Camera };
            UIImagePickerControllerCameraDevice cameraDevice = new UIImagePickerControllerCameraDevice();

            if (UIImagePickerController.IsCameraDeviceAvailable(cameraDevice))
            {
                var imagePicker = new UIImagePickerController {
                    SourceType = UIImagePickerControllerSourceType.Camera
                };


                (Xamarin.Forms.Application.Current as App).ShouldTakePicture += () =>
                                                                                app.KeyWindow.RootViewController.PresentViewController(imagePicker, true, null);

                //Save the picture to the MyDocuments folder and call the shared "ShowImage" method
                imagePicker.FinishedPickingMedia += (sender, e) =>
                {
                    var filepath = Path.Combine(Environment.GetFolderPath(
                                                    Environment.SpecialFolder.MyDocuments), "tmp.png");
                    var image = (UIImage)e.Info.ObjectForKey(new NSString("UIImagePickerControllerOriginalImage"));
                    InvokeOnMainThread(() =>
                    {
                        image.AsPNG().Save(filepath, false);
                        (Xamarin.Forms.Application.Current as App).ShowImage(filepath);
                    });
                    app.KeyWindow.RootViewController.DismissViewController(true, null);                     //changed the name of UIApplication to app
                };

                imagePicker.Canceled += (sender, e) => app.KeyWindow.RootViewController.DismissViewController(true, null);
            }
            else //no camera available
            {
                //create a new message alert
                UIAlertView alert = new UIAlertView()
                {
                    Title   = "Camera Error",
                    Message = "No camera available"
                };
                alert.AddButton("OK");
                alert.Show();
            }



            return(base.FinishedLaunching(app, options));
        }
예제 #2
0
파일: GUIXT.cs 프로젝트: zhengyudian/u3dxt
        /// <summary>
        /// Shows the image picker.
        /// </summary>
        public static void ShowImagePicker(UIImagePickerControllerSourceType source         = UIImagePickerControllerSourceType.PhotoLibrary,
                                           UIImagePickerControllerCameraDevice cameraDevice = UIImagePickerControllerCameraDevice.Rear)
        {
            _picker = new UIImagePickerController();
//			_picker.AllowsEditing = true;
            _picker.sourceType = source;
//			_picker.Delegate = ImagePickerControllerDelegate.instance;
            _picker.DidFinishPickingMediaWithInfo += _OnPickedMedia;
            _picker.DidCancel += _OnCancelledPick;
            if (source == UIImagePickerControllerSourceType.Camera)
            {
                _picker.cameraDevice = cameraDevice;
            }

            var rootVc = UIApplication.deviceRootViewController;

            if (CoreXT.IsiPad && (source != UIImagePickerControllerSourceType.Camera))
            {
                if (_popover == null)
                {
                    _popover                      = new UIPopoverController(_picker);
                    _popover.DidDismiss          += _OnCancelledPick;
                    _popover.shouldDismissHandler = _ShouldDismissPopover;
                }
                else
                {
                    _popover.contentViewController = _picker;
                }

                var rect = rootVc.view.bounds;
                rect.x      = rect.width / 2;
                rect.y      = rect.height;
                rect.width  = 1;
                rect.height = 1;
                _popover.PresentPopover(
                    rect,
                    rootVc.view,
                    UIPopoverArrowDirection.Down,
                    true);
            }
            else
            {
                rootVc.PresentViewController(_picker, true, null);
            }
        }
 public static bool IsFlashAvailableForCameraDevice(UIImagePickerControllerCameraDevice cameraDevice)
 {
     return(default(bool));
 }
 public static AnyObject[] AvailableCaptureModesForCameraDevice(UIImagePickerControllerCameraDevice cameraDevice)
 {
     return(default(AnyObject[]));
 }
        /// <summary>
        /// Opens a camera capture view controller.
        /// </summary>
        /// <param name="parent">The parent view controller that invoked the method.</param>
        /// <param name="callback">Action to call after photo is captured.</param>
        /// <param name="onCancel">Action to call if canceling from camera.</param>
        /// <param name="cameraToUse">Can set to default the front or rear camera.</param>
        public static void TakePicture <TViewModel>(BaseViewController <TViewModel> parent, Action <NSDictionary> callback, Action onCancel, UIImagePickerControllerCameraDevice cameraToUse = UIImagePickerControllerCameraDevice.Front)
            where TViewModel : BaseViewModel
        {
            var status = AVCaptureDevice.GetAuthorizationStatus(AVMediaType.Video);

            if (status == AVAuthorizationStatus.NotDetermined)
            {
                AVCaptureDevice.RequestAccessForMediaType(AVMediaType.Video, (accessGranted) =>
                {
                    if (accessGranted)
                    {
                        // Open the camera
                        parent.InvokeOnMainThread(delegate
                        {
                            InitializeCamera(parent, callback, onCancel, cameraToUse);
                        });
                    }
                    // If they have said no do nothing.
                });
            }
            else if (status == AVAuthorizationStatus.Authorized)
            {
                // Open the camera
                InitializeCamera(parent, callback, onCancel, cameraToUse);
            }
            else
            {
                // They said no in the past, so show them an alert to direct them to the app's settings.
                OpenSettingsAlert(parent, "Camera Access Required", "Authorization to access the camera is required to use this feature of the app.");
            }
        }
        /// <summary>
        /// Initializes the camera.
        /// </summary>
        /// <param name="parent">Parent view controller.</param>
        /// <param name="callback">Callback once photo is taken</param>
        /// <param name="onCancel">On cancel action</param>
        /// <param name="cameraToUse">Camera to use.</param>
        private static void InitializeCamera <TViewModel>(BaseViewController <TViewModel> parent, Action <NSDictionary> callback, Action onCancel, UIImagePickerControllerCameraDevice cameraToUse)
            where TViewModel : BaseViewModel
        {
            _callback = callback;
            _onCancel = onCancel;
            Init();
            picker.SourceType = UIImagePickerControllerSourceType.Camera;
            var cameraAvailable = UIImagePickerController.IsCameraDeviceAvailable(cameraToUse);

            if (cameraAvailable)
            {
                picker.CameraDevice = cameraToUse;
                parent.PresentViewController(picker, true, null);
            }
            else
            {
                parent.ShowAlert("The camera is unavailable", "No Camera");
            }
        }
예제 #7
0
파일: GUIXT.cs 프로젝트: BiDuc/u3dxt
        /// <summary>
        /// Shows the image picker.
        /// </summary>
        public static void ShowImagePicker(UIImagePickerControllerSourceType source = UIImagePickerControllerSourceType.PhotoLibrary,
		                                   UIImagePickerControllerCameraDevice cameraDevice = UIImagePickerControllerCameraDevice.Rear)
        {
            _picker = new UIImagePickerController();
            //			_picker.AllowsEditing = true;
            _picker.sourceType = source;
            //			_picker.Delegate = ImagePickerControllerDelegate.instance;
            _picker.DidFinishPickingMediaWithInfo += _OnPickedMedia;
            _picker.DidCancel += _OnCancelledPick;
            if (source == UIImagePickerControllerSourceType.Camera)
                _picker.cameraDevice = cameraDevice;

            var rootVc = UIApplication.deviceRootViewController;
            if (CoreXT.IsiPad && (source != UIImagePickerControllerSourceType.Camera)) {
                if (_popover == null) {
                    _popover = new UIPopoverController(_picker);
                    _popover.DidDismiss += _OnCancelledPick;
                    _popover.shouldDismissHandler = _ShouldDismissPopover;
                } else {
                    _popover.contentViewController = _picker;
                }

                var rect = rootVc.view.bounds;
                rect.x = rect.width / 2;
                rect.y = rect.height;
                rect.width = 1;
                rect.height = 1;
                _popover.PresentPopover(
                    rect,
                    rootVc.view,
                    UIPopoverArrowDirection.Down,
                    true);
            } else {
                rootVc.PresentViewController(_picker, true, null);
            }
        }