Exemplo n.º 1
0
        async Task FixOrientationAndResize(PickMediaOptions options, MediaFile media)
        {
            var originalMetadata = new ExifInterface(media.Path);

            if (options.RotateImage)
            {
                await FixOrientationAndResizeAsync(media.Path, options, originalMetadata);
            }
            else
            {
                await ResizeAsync(media.Path, options, originalMetadata);
            }

            if (options.SaveMetaData && IsValidExif(originalMetadata))
            {
                try
                {
                    originalMetadata?.SaveAttributes();
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"Unable to save exif {ex}");
                }
            }

            originalMetadata?.Dispose();
        }
Exemplo n.º 2
0
        /// <summary>
        /// Picks a photo from the default gallery
        /// </summary>
        /// <returns>Media file or null if canceled</returns>
        public async Task <MediaFile> PickPhotoAsync(PickMediaOptions options = null)
        {
            if (!(await RequestStoragePermission()))
            {
                throw new MediaPermissionException(Permission.Storage);
            }
            var media = await TakeMediaAsync("image/*", Intent.ActionPick, null);

            if (options == null)
            {
                options = new PickMediaOptions();
            }

            //check to see if we picked a file, and if so then try to fix orientation and resize
            if (!string.IsNullOrWhiteSpace(media?.Path))
            {
                try
                {
                    var originalMetadata = new ExifInterface(media.Path);

                    if (options.RotateImage)
                    {
                        await FixOrientationAndResizeAsync(media.Path, options, originalMetadata);
                    }
                    else
                    {
                        await ResizeAsync(media.Path, options.PhotoSize, options.CompressionQuality, options.CustomPhotoSize, originalMetadata);
                    }
                    if (options.SaveMetaData && IsValidExif(originalMetadata))
                    {
                        try
                        {
                            originalMetadata?.SaveAttributes();
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine($"Unable to save exif {ex}");
                        }
                    }

                    originalMetadata?.Dispose();
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Unable to check orientation: " + ex);
                }
            }

            return(media);
        }
Exemplo n.º 3
0
        public override async Task ResizeAndRotateAsync(MediaFile file, MediaOptions mediaOptions, DeviceRotation currentRotaion = DeviceRotation.Unknown)
        {
            var originalMetadata = new ExifInterface(file.Path);

            var transformVM = new TransformViewModel
            {
                FilePath        = file.Path,
                MediaOptions    = mediaOptions,
                Exif            = originalMetadata,
                CurrentRotation = currentRotaion
            };

            await ResizeAndRotateAsync(transformVM);

            originalMetadata?.Dispose();

            GC.Collect();
        }
Exemplo n.º 4
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();
            }

            if (!(await RequestCameraPermissions()))
            {
                throw new MediaPermissionException(Permission.Camera);
            }


            VerifyOptions(options);

            var media = await TakeMediaAsync("image/*", MediaStore.ActionImageCapture, options);

            if (string.IsNullOrWhiteSpace(media?.Path))
            {
                return(media);
            }

            if (options.SaveToAlbum)
            {
                try
                {
                    var fileName  = System.IO.Path.GetFileName(media.Path);
                    var publicUri = MediaPickerActivity.GetOutputMediaFile(context, options.Directory ?? "temp", fileName, true, true);
                    using (System.IO.Stream input = File.OpenRead(media.Path))
                        using (System.IO.Stream output = File.Create(publicUri.Path))
                            input.CopyTo(output);

                    media.AlbumPath = publicUri.Path;

                    var f = new Java.IO.File(publicUri.Path);

                    //MediaStore.Images.Media.InsertImage(context.ContentResolver,
                    //    f.AbsolutePath, f.Name, null);

                    try
                    {
                        Android.Media.MediaScannerConnection.ScanFile(context, new[] { f.AbsolutePath }, null, context as MediaPickerActivity);

                        ContentValues values = new ContentValues();
                        values.Put(MediaStore.Images.Media.InterfaceConsts.Title, System.IO.Path.GetFileNameWithoutExtension(f.AbsolutePath));
                        values.Put(MediaStore.Images.Media.InterfaceConsts.Description, string.Empty);
                        values.Put(MediaStore.Images.Media.InterfaceConsts.DateTaken, Java.Lang.JavaSystem.CurrentTimeMillis());
                        values.Put(MediaStore.Images.ImageColumns.BucketId, f.ToString().ToLowerInvariant().GetHashCode());
                        values.Put(MediaStore.Images.ImageColumns.BucketDisplayName, f.Name.ToLowerInvariant());
                        values.Put("_data", f.AbsolutePath);

                        var cr = context.ContentResolver;
                        cr.Insert(MediaStore.Images.Media.ExternalContentUri, values);
                    }
                    catch (Exception ex1)
                    {
                        Console.WriteLine("Unable to save to scan file: " + ex1);
                    }

                    var contentUri      = Android.Net.Uri.FromFile(f);
                    var mediaScanIntent = new Intent(Intent.ActionMediaScannerScanFile, contentUri);
                    context.SendBroadcast(mediaScanIntent);
                }
                catch (Exception ex2)
                {
                    Console.WriteLine("Unable to save to gallery: " + ex2);
                }
            }

            //check to see if we need to rotate if success


            try
            {
                var exif = new ExifInterface(media.Path);
                if (options.RotateImage)
                {
                    await FixOrientationAndResizeAsync(media.Path, options, exif);
                }
                else
                {
                    await ResizeAsync(media.Path, options.PhotoSize, options.CompressionQuality, options.CustomPhotoSize, exif);
                }

                if (options.SaveMetaData && IsValidExif(exif))
                {
                    SetMissingMetadata(exif, options.Location);

                    try
                    {
                        exif?.SaveAttributes();
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine($"Unable to save exif {ex}");
                    }
                }

                exif?.Dispose();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Unable to check orientation: " + ex);
            }

            return(media);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Take a photo async with specified options
        /// </summary>
        /// <param name="options">Camera Media Options</param>
        /// <param name="token">Cancellation token</param>
        /// <returns>Media file of photo or null if canceled</returns>
        public async Task <MediaFile> TakePhotoAsync(StoreCameraMediaOptions options, CancellationToken token = default(CancellationToken))
        {
            if (!IsCameraAvailable)
            {
                throw new NotSupportedException();
            }

            if (!(await RequestCameraPermissions()))
            {
                throw new MediaPermissionException(nameof(Permissions.Camera));
            }

            VerifyOptions(options);

            var media = await TakeMediaAsync("image/*", MediaStore.ActionImageCapture, options, token);

            if (string.IsNullOrWhiteSpace(media?.Path))
            {
                return(media);
            }

            if (options.SaveToAlbum)
            {
                var fileName = System.IO.Path.GetFileName(media.Path);
                var uri      = MediaPickerActivity.GetOutputMediaFile(context, options.Directory ?? "temp", fileName, true, true);
                var f        = new Java.IO.File(uri.Path);
                media.AlbumPath = uri.ToString();

                try
                {
                    var values = new ContentValues();
                    values.Put(MediaStore.Audio.Media.InterfaceConsts.DisplayName, System.IO.Path.GetFileNameWithoutExtension(f.AbsolutePath));
                    values.Put(MediaStore.Audio.Media.InterfaceConsts.MimeType, "image/jpeg");
                    values.Put(MediaStore.Images.Media.InterfaceConsts.Description, string.Empty);
                    values.Put(MediaStore.Images.Media.InterfaceConsts.DateTaken, Java.Lang.JavaSystem.CurrentTimeMillis());
                    values.Put(MediaStore.Images.ImageColumns.BucketId, f.ToString().ToLowerInvariant().GetHashCode());
                    values.Put(MediaStore.Images.ImageColumns.BucketDisplayName, f.Name.ToLowerInvariant());

                    var cr       = context.ContentResolver;
                    var albumUri = cr.Insert(MediaStore.Images.Media.ExternalContentUri, values);

                    using (System.IO.Stream input = File.OpenRead(media.Path))
                        using (System.IO.Stream output = cr.OpenOutputStream(albumUri))
                            input.CopyTo(output);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Unable to save to gallery: " + ex);
                }
            }

            //check to see if we need to rotate if success
            try
            {
                var exif = new ExifInterface(media.Path);
                if (options.RotateImage)
                {
                    await FixOrientationAndResizeAsync(media.Path, options, exif);
                }
                else
                {
                    await ResizeAsync(media.Path, options, exif);
                }

                if (options.SaveMetaData && IsValidExif(exif))
                {
                    SetMissingMetadata(exif, options.Location);

                    try
                    {
                        exif?.SaveAttributes();
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine($"Unable to save exif {ex}");
                    }
                }

                exif?.Dispose();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Unable to check orientation: " + ex);
            }

            return(media);
        }