Ejemplo n.º 1
0
        /// <summary>
        /// Setting Camera Options for Take Photo
        /// In Tizen, the following options are not available.
        /// - options.PhotoSize
        /// - options.SaveToAlbum
        ///	- options.Location
        /// - options.CompressionQuality
        /// </summary>
        /// <param name="options">StoreCameraMediaOptions from Media.Plugin</param>
        private void SetOptions(StoreCameraMediaOptions options, ref AppControl appControl)
        {
            if (appControl == null)
            {
                throw new ObjectDisposedException("AppControl");
            }
            options.VerifyOptions();
            appControl.ExtraData.Add(EX_KEY_ALLOW_SWITCH, EX_VAL_FALSE);
            appControl.ExtraData.Add(EX_KEY_REQ_DESTORY, EX_VAL_TRUE);

            if (options.DefaultCamera == Abstractions.CameraDevice.Front)
            {
                appControl.ExtraData.Add(EX_KEY_SELFIE_MODE, EX_VAL_TRUE);
            }
            else
            {
                appControl.ExtraData.Add(EX_KEY_SELFIE_MODE, EX_VAL_FALSE);
            }

            if (options.AllowCropping.HasValue)
            {
                if (options.AllowCropping.Value)
                {
                    appControl.ExtraData.Add(EX_KEY_CROP, EX_VAL_FIT_TO_SCREEN);
                }
                else
                {
                    appControl.ExtraData.Add(EX_KEY_CROP, EX_VAL_1X1_FIX_RATIO);
                }
            }
        }
Ejemplo n.º 2
0
        /// <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);
            }

            return(await MediaFileFromFile(result));
        }
Ejemplo n.º 3
0
        /// <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 (!initialized)
            {
                await Initialize();
            }

            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);
            string aPath    = null;

            if (options?.SaveToAlbum ?? false)
            {
                try
                {
                    string 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));
        }
Ejemplo n.º 4
0
        /// <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));
        }
Ejemplo n.º 5
0
        /// <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);
        }
Ejemplo n.º 6
0
        /// <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();

            capture.PhotoSettings.Format        = CameraCaptureUIPhotoFormat.Jpeg;
            capture.PhotoSettings.MaxResolution = CameraCaptureUIMaxPhotoResolution.HighestAvailable;

            var result = await capture.CaptureFileAsync(CameraCaptureUIMode.Photo);

            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>
        /// <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));
        }