示例#1
0
 private void VerifyOptions(StoreMediaOptions options)
 {
     if (options == null)
         throw new ArgumentNullException("options");
     if (Path.IsPathRooted(options.Directory))
         throw new ArgumentException("options.Directory must be a relative path", "options");
 }
示例#2
0
        internal static Camera CreateCamera(OperationType operationType, StoreMediaOptions storeOptions)
        {
            if (Cameras.TryGetValue(operationType, out Camera camera))
            {
                return(camera);
            }

            switch (operationType)
            {
            case OperationType.Photo:
                camera = new PhotoCamera(storeOptions);
                Cameras[OperationType.Photo] = camera;
                break;

            case OperationType.Video:
                camera = new VideoCamera(storeOptions);
                Cameras[OperationType.Video] = camera;
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(operationType), operationType, null);
            }

            return(camera);
        }
示例#3
0
        private Intent CreateMediaIntent(int id, string type, string action, StoreMediaOptions options, bool tasked = true)
        {
            Intent pickerIntent = new Intent(this.context, typeof(MediaPickerActivity));
            pickerIntent.PutExtra(MediaPickerActivity.ExtraId, id);
            pickerIntent.PutExtra(MediaPickerActivity.ExtraType, type);
            pickerIntent.PutExtra(MediaPickerActivity.ExtraAction, action);
            pickerIntent.PutExtra(MediaPickerActivity.ExtraTasked, tasked);

            if (options != null)
            {
                pickerIntent.PutExtra(MediaPickerActivity.ExtraPath, options.Directory);
                pickerIntent.PutExtra(MediaStore.Images.ImageColumns.Title, options.Name);


                var cameraOptions = (options as StoreCameraMediaOptions);
                if (cameraOptions != null)
                    pickerIntent.PutExtra(MediaPickerActivity.ExtraSaveToAlbum, cameraOptions.SaveToAlbum);

                var vidOptions = (options as StoreVideoOptions);
                if (vidOptions != null)
                {
                    pickerIntent.PutExtra(MediaStore.ExtraDurationLimit, (int)vidOptions.DesiredLength.TotalSeconds);
                    pickerIntent.PutExtra(MediaStore.ExtraVideoQuality, (int)vidOptions.Quality);
                }
            }
            //pickerIntent.SetFlags(ActivityFlags.ClearTop);
            pickerIntent.SetFlags(ActivityFlags.NewTask);
            return pickerIntent;
        }
        private async Task TakeFrontPhotoAsync()
        {
            var cameras     = this.mediaAccess.Cameras.ToList();
            var frontCamera = cameras.FirstOrDefault(c => c.CameraFacingDirection == CameraFacingDirection.Front) as IPhotoCamera;

            if (frontCamera != null)
            {
                var options = new StoreMediaOptions("testimage" + Guid.NewGuid() + ".jpg", "Front Camera Photos");
                options.AllowCropping      = true;
                options.PhotoSize          = PhotoSize.Small;
                options.CompressionQuality = 10;

                var mediaFile = await frontCamera.TakePhotoAsync(options);

                if (mediaFile != null)
                {
                    await this.mediaAccess.MediaLibrary.SaveToCameraRoll(mediaFile);

                    await this.DisplayAlert("SaveToCameraRoll", $"Your photo has been saved to '{mediaFile.Path}'", "OK");

                    this.SetImageSource(mediaFile);
                }
                else
                {
                    await this.DisplayAlert("TakePhotoAsync", "Failed to take picture.", "OK");
                }
            }
        }
示例#5
0
        private Task <MediaFile> TakeMediaAsync(string type, string action, StoreMediaOptions options, CancellationToken token = default(CancellationToken))
        {
            int id = GetRequestId();

            if (token.IsCancellationRequested)
            {
                return(Task.FromResult((MediaFile)null));
            }

            var ntcs = new TaskCompletionSource <MediaFile>(id);

            if (Interlocked.CompareExchange(ref completionSource, ntcs, null) != null)
            {
                throw new InvalidOperationException("Only one operation can be active at a time");
            }

            context.StartActivity(CreateMediaIntent(id, type, action, options));

            EventHandler <MediaPickedEventArgs> handler = null;

            handler = (s, e) =>
            {
                var tcs = Interlocked.Exchange(ref this.completionSource, null);

                MediaPickerActivity.MediaPicked -= handler;

                if (e.RequestId != id)
                {
                    return;
                }

                if (e.IsCanceled)
                {
                    tcs.SetResult(null);
                }
                else if (e.Error != null)
                {
                    tcs.SetException(e.Error);
                }
                else
                {
                    tcs.SetResult(e.Media);
                }
            };

            token.Register(() =>
            {
                var tcs = Interlocked.Exchange(ref this.completionSource, null);

                MediaPickerActivity.MediaPicked -= handler;
                CancelRequested?.Invoke(null, EventArgs.Empty);
                CancelRequested = null;

                tcs.SetResult(null);
            });

            MediaPickerActivity.MediaPicked += handler;

            return(completionSource.Task);
        }
        Task <List <MediaFile> > TakeMediasAsync(string type, string action, StoreMediaOptions options, CancellationToken token = default(CancellationToken))
        {
            var id = GetRequestId();

            if (token.IsCancellationRequested)
            {
                return(Task.FromResult((List <MediaFile>)null));
            }

            var ntcs = new TaskCompletionSource <List <MediaFile> >(id);

            if (Interlocked.CompareExchange(ref CompletionSourceMulti, ntcs, null) != null)
            {
                throw new InvalidOperationException("Only one operation can be active at a time");
            }

            context.StartActivity(CreateMediaIntent(id, type, action, options));

            void handler(object s, MediaPickedEventArgs e)
            {
                var tcs = Interlocked.Exchange(ref CompletionSourceMulti, null);

                MediaPickerActivity.MediaPicked -= handler;

                if (e.RequestId != id)
                {
                    return;
                }

                if (e.IsCanceled)
                {
                    tcs.SetResult(null);
                }
                else if (e.Error != null)
                {
                    tcs.SetException(e.Error);
                }
                else
                {
                    tcs.SetResult(e.Media);
                }
            }

            token.Register(() =>
            {
                var tcs = Interlocked.Exchange(ref CompletionSourceMulti, null);
                MediaPickerActivity.MediaPicked -= handler;
                CancelRequested?.Invoke(null, EventArgs.Empty);
                CancelRequested = null;

                tcs.SetResult(null);
            });

            MediaPickerActivity.MediaPicked += handler;

            return(CompletionSourceMulti.Task);
        }
示例#7
0
        internal PhotoCamera(StoreMediaOptions storeOptions) : base(storeOptions)
        {
            CameraOperationType = OperationType.Photo;

            // Factory's instance created to store itself in static field
            new CaptureStateFactory(RunPreCaptureSequence, CaptureStillPhoto);

            _imageAvailableHandler = new ImageAvailableHandler(StoreOptions, CameraBackgroundHandler, MediaPickerActivity);

            CameraParameters = ComputerParametersFactory.CreateCameraParameters(CameraOperationType);
        }
示例#8
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Camera"/> class.
        /// </summary>
        /// <param name="storeOptions">The store options.</param>
        /// <param name="visitable">The visitable.</param>
        protected Camera(StoreMediaOptions storeOptions)
        {
            StoreOptions = storeOptions as StoreCameraMediaOptions;

            Manager = (CameraManager)CrossCurrentActivity.Current.Activity.GetSystemService(Context.CameraService);

            CameraDeviceStateHandler                = new CameraDeviceStateHandler(this);
            CameraCaptureSessionStateHandler        = new CameraCaptureSessionStateHandler(this);
            CameraCaptureSessionCaptureHandler      = new CameraCaptureSessionCaptureHandler <ICaptureState>();
            CameraCaptureSessionPhotoCaptureHandler = new CameraCaptureSessionPhotoCaptureHandler(this, UnlockFocus);
        }
示例#9
0
        private void VerifyOptions(StoreMediaOptions options)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            if (options.Directory != null && Path.IsPathRooted(options.Directory))
            {
                throw new ArgumentException("options.Directory must be a relative path", nameof(options));
            }
        }
        Intent CreateMediaIntent(int id, string type, string action, StoreMediaOptions options, bool tasked = true)
        {
            var pickerIntent = new Intent(this.context, typeof(MediaPickerActivity));

            pickerIntent.PutExtra(MediaPickerActivity.ExtraId, id);
            pickerIntent.PutExtra(MediaPickerActivity.ExtraType, type);
            pickerIntent.PutExtra(MediaPickerActivity.ExtraAction, action);
            pickerIntent.PutExtra(MediaPickerActivity.ExtraTasked, tasked);

            if (options != null)
            {
                pickerIntent.PutExtra(MediaPickerActivity.ExtraPath, options.Directory);
                pickerIntent.PutExtra(MediaStore.Images.ImageColumns.Title, options.Name);

                var pickerOptions = (options as StorePickerMediaOptions);
                if (pickerOptions != null)
                {
                    pickerIntent.PutExtra(MediaPickerActivity.ExtraMultiSelect, pickerOptions.MultiPicker);
                }

                var cameraOptions = (options as StoreCameraMediaOptions);
                if (cameraOptions != null)
                {
                    if (cameraOptions.DefaultCamera == CameraDevice.Front)
                    {
                        pickerIntent.PutExtra("android.intent.extras.CAMERA_FACING", 1);
                    }
                    pickerIntent.PutExtra(MediaPickerActivity.ExtraSaveToAlbum, cameraOptions.SaveToAlbum);
                }
                var vidOptions = (options as StoreVideoOptions);
                if (vidOptions != null)
                {
                    if (vidOptions.DefaultCamera == CameraDevice.Front)
                    {
                        pickerIntent.PutExtra("android.intent.extras.CAMERA_FACING", 1);
                    }
                    pickerIntent.PutExtra(MediaStore.ExtraDurationLimit, (int)vidOptions.DesiredLength.TotalSeconds);
                    pickerIntent.PutExtra(MediaStore.ExtraVideoQuality, (int)vidOptions.Quality);
                    if (vidOptions.DesiredSize != 0)
                    {
                        pickerIntent.PutExtra(MediaStore.ExtraSizeLimit, vidOptions.DesiredSize);
                    }
                }
            }
            //pickerIntent.SetFlags(ActivityFlags.ClearTop);
            pickerIntent.SetFlags(ActivityFlags.NewTask);
            return(pickerIntent);
        }
        Task <List <MediaFile> > TakeMediaAsync(string type, string actions, StoreMediaOptions options)
        {
            int id = GetRequestId();

            var ntcs = new TaskCompletionSource <List <MediaFile> >(id);

            if (Interlocked.CompareExchange(ref completionSource, ntcs, null) != null)
            {
                throw new InvalidOperationException("Only one operation can be active at a time");
            }

            context.StartActivity(CreateMediaIntent(id, type, actions, options));

            EventHandler <MediaPickedEventArgs> handler = null;

            handler = (s, e) =>
            {
                var tcs = Interlocked.Exchange(ref completionSource, null);

                MediaPickerActivity.MediaPicked -= handler;

                if (e.RequestId != id)
                {
                    return;
                }

                if (e.IsCanceled)
                {
                    tcs.SetResult(null);
                }
                else if (e.Error != null)
                {
                    tcs.SetException(e.Error);
                }
                else
                {
                    tcs.SetResult(e.Media);
                }
            };

            MediaPickerActivity.MediaPicked += handler;

            return(completionSource.Task);
        }
示例#12
0
        private Task <MediaFile> TakeMediaAsync(string type, string action, StoreMediaOptions options)
        {
            var id = GetRequestId();

            var ntcs = new TaskCompletionSource <MediaFile>(id);

            if (Interlocked.CompareExchange(ref _CompletionSource, ntcs, null) != null)
            {
                throw new InvalidOperationException("Only one operation can be active at a time");
            }

            _Context.StartActivity(CreateMediaIntent(id, type, action, options));

            void Handler(object s, MediaPickedEventArgs e)
            {
                var tcs = Interlocked.Exchange(ref _CompletionSource, null);

                MediaPickerActivity.MediaPicked -= Handler;

                if (e.RequestId != id)
                {
                    return;
                }

                if (e.IsCanceled)
                {
                    tcs.SetResult(null);
                }
                else if (e.Error != null)
                {
                    tcs.SetException(e.Error);
                }
                else
                {
                    tcs.SetResult(e.Media);
                }
            }

            MediaPickerActivity.MediaPicked += Handler;

            return(_CompletionSource.Task);
        }
示例#13
0
        private void VerifyOptions(StoreMediaOptions options)
        {
            if (options == null)
            {
                throw new ArgumentNullException("options");
            }
            if (System.IO.Path.IsPathRooted(options.Directory))
            {
                throw new ArgumentException("options.Directory must be a relative path", "options");
            }

            if (!string.IsNullOrWhiteSpace(options.Name))
            {
                options.Name = Regex.Replace(options.Name, IllegalCharacters, string.Empty).Replace(@"\", string.Empty);
            }

            if (!string.IsNullOrWhiteSpace(options.Directory))
            {
                options.Directory = Regex.Replace(options.Directory, IllegalCharacters, string.Empty).Replace(@"\", string.Empty);
            }
        }
示例#14
0
 internal ImageAvailableHandler(StoreMediaOptions storeOptions, Handler cameraBackgroundHandler, MediaPickerActivity mediaPickerActivity)
 {
     _storeOptions            = (StoreCameraMediaOptions)storeOptions;
     _cameraBackgroundHandler = cameraBackgroundHandler;
     _mediaPickerActivity     = mediaPickerActivity;
 }
示例#15
0
 /// <summary>
 /// Initializes Common Camera members..
 /// </summary>
 /// <param name="options">Camera options.</param>
 /// <param name="cameraOperationType">The camera operation type (photo or video).</param>
 protected BaseVisitor(StoreMediaOptions options, OperationType cameraOperationType)
 {
     Options             = options;
     CameraOperationType = cameraOperationType;
 }
示例#16
0
        internal VideoCamera(StoreMediaOptions storeOptions) : base(storeOptions)
        {
            CameraOperationType = OperationType.Video;

            CameraParameters = ComputerParametersFactory.CreateCameraParameters(CameraOperationType);
        }
示例#17
0
        private Intent CreateMediaIntent(int id, string type, string action, StoreMediaOptions options, bool tasked = true)
        {
            Intent pickerIntent = new Intent(this.context, typeof(MediaPickerActivity));

            pickerIntent.PutExtra(MediaPickerActivity.ExtraId, id);
            pickerIntent.PutExtra(MediaPickerActivity.ExtraType, type);
            pickerIntent.PutExtra(MediaPickerActivity.ExtraAction, action);
            pickerIntent.PutExtra(MediaPickerActivity.ExtraTasked, tasked);


            if (options != null)
            {
                pickerIntent.PutExtra(MediaPickerActivity.ExtraPath, options.Directory);
                pickerIntent.PutExtra(MediaStore.Images.ImageColumns.Title, options.Name);

                var cameraOptions = (options as StoreCameraMediaOptions);
                if (cameraOptions != null)
                {
                    if (cameraOptions.DefaultCamera == CameraDevice.Front)
                    {
                        pickerIntent.PutExtra(MediaPickerActivity.ExtraFront, true);
                        pickerIntent.PutExtra("android.intent.extras.CAMERA_FACING", (int)CameraFacing.Front);

                        // Android API 25 and up
                        pickerIntent.PutExtra("android.intent.extras.LENS_FACING_FRONT", 1);
                        pickerIntent.PutExtra("android.intent.extra.USE_FRONT_CAMERA", true);
                    }
                    else if (cameraOptions.DefaultCamera == CameraDevice.Rear)
                    {
                        pickerIntent.PutExtra(MediaPickerActivity.ExtraFront, false);
                        pickerIntent.PutExtra("android.intent.extras.CAMERA_FACING", (int)CameraFacing.Back);

                        // Android API 25 and up
                        pickerIntent.PutExtra("android.intent.extras.LENS_FACING_BACK", 1);
                        pickerIntent.PutExtra("android.intent.extra.USE_FRONT_CAMERA", false);
                    }

                    if (cameraOptions.FlashMode == CameraFlash.On)
                    {
                        pickerIntent.PutExtra(MediaPickerActivity.ExtraFlashON, true);
                        pickerIntent.PutExtra(MediaPickerActivity.ExtraFlashOFF, false);
                        pickerIntent.PutExtra("android.intent.extras.FLASH_MODE_ON", (int)CameraFlash.On);
                    }
                    else if (cameraOptions.FlashMode == CameraFlash.Off)
                    {
                        pickerIntent.PutExtra(MediaPickerActivity.ExtraFlashON, false);
                        pickerIntent.PutExtra(MediaPickerActivity.ExtraFlashOFF, true);
                        pickerIntent.PutExtra("android.intent.extras.FLASH_MODE_ON", (int)CameraFlash.On);
                    }
                    else if (cameraOptions.FlashMode == CameraFlash.Default)
                    {
                        pickerIntent.PutExtra(MediaPickerActivity.ExtraFlashAuto, true);
                    }

                    pickerIntent.PutExtra(MediaPickerActivity.ExtraSaveToAlbum, cameraOptions.SaveToAlbum);
                }
                var vidOptions = (options as StoreVideoOptions);
                if (vidOptions != null)
                {
                    if (vidOptions.DefaultCamera == CameraDevice.Front)
                    {
                        pickerIntent.PutExtra("android.intent.extras.CAMERA_FACING", 1);
                    }
                    pickerIntent.PutExtra(MediaStore.ExtraDurationLimit, (int)vidOptions.DesiredLength.TotalSeconds);
                    pickerIntent.PutExtra(MediaStore.ExtraVideoQuality, (int)vidOptions.Quality);
                    if (vidOptions.DesiredSize != 0)
                    {
                        pickerIntent.PutExtra(MediaStore.ExtraSizeLimit, vidOptions.DesiredSize);
                    }
                }
            }
            //pickerIntent.SetFlags(ActivityFlags.ClearTop);
            pickerIntent.SetFlags(ActivityFlags.NewTask);
            return(pickerIntent);
        }
示例#18
0
        private Task<Plugin.Media.Abstractions.MediaFile> TakeMediaAsync(string type, string action, StoreMediaOptions options)
        {
            int id = GetRequestId();

            var ntcs = new TaskCompletionSource<Plugin.Media.Abstractions.MediaFile>(id);
            if (Interlocked.CompareExchange(ref this.completionSource, ntcs, null) != null)
                throw new InvalidOperationException("Only one operation can be active at a time");

            this.context.StartActivity(CreateMediaIntent(id, type, action, options));

            EventHandler<MediaPickedEventArgs> handler = null;
            handler = (s, e) =>
            {
                var tcs = Interlocked.Exchange(ref this.completionSource, null);

                MediaPickerActivity.MediaPicked -= handler;

                if (e.RequestId != id)
                    return;

                if (e.Error != null)
                    tcs.SetResult(null);
                else if (e.IsCanceled)
                    tcs.SetResult(null);
                else
                    tcs.SetResult(e.Media);
            };

            MediaPickerActivity.MediaPicked += handler;

            return ntcs.Task;
        }
示例#19
0
 /// <inheritdoc />
 /// <summary>
 /// Initializes Common Camera members..
 /// </summary>
 /// <param name="options">Camera options.</param>
 /// <param name="cameraOperationType">The camera operation type (photo or video).</param>
 protected AndroidVisitor(StoreMediaOptions options, OperationType cameraOperationType) :
     base(options, cameraOperationType) => Camera = CameraFactory.CreateCamera(CameraOperationType, options);