Exemplo n.º 1
0
        private async Task <List <MediaFile> > HandleMediaFiles(List <MediaFile> mediaList, PickMediaOptions options = null)
        {
            if (mediaList == null)
            {
                return(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
            for (int i = 0; i < mediaList.Count; i++)
            {
                var media = mediaList[i];
                if (string.IsNullOrWhiteSpace(media?.Path) || media?.Type == Abstractions.MediaType.Video)
                {
                    continue;
                }

                try
                {
                    bool imageChanged     = false;
                    var  originalMetadata = new ExifInterface(media.Path);
                    if (options.RotateImage)
                    {
                        imageChanged = await FixOrientationAndResizeAsync(media.Path, options, originalMetadata);
                    }
                    else
                    {
                        imageChanged = await ResizeAsync(media.Path, options.PhotoSize, options.CompressionQuality, options.CustomPhotoSize, options.MaxWidthHeight, originalMetadata);
                    }
                    if (imageChanged)
                    {
                        var newExif = new ExifInterface(media.Path);
                        newExif.Copy(originalMetadata);
                        newExif.SaveAttributes();
                    }
                    else
                    {
                        originalMetadata.SaveAttributes();
                    }
                    UpdateMetadata(ref media, originalMetadata);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Unable to check orientation: " + ex);
                }
            }
            return(mediaList);
        }
Exemplo n.º 2
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()))
            {
                return(null);
            }


            VerifyOptions(options);

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

            if (mediaList == null)
            {
                return(null);
            }

            var media = mediaList.First();

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

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


            try
            {
                bool imageChanged = false;
                var  exif         = new ExifInterface(media.Path);
                exif.SetAttribute(ExifInterface.TagDatetime, DateTime.Now.ToString("yyyy:MM:dd HH:mm:ss"));
                exif.SetAttribute(ExifInterface.TagDatetimeOriginal, DateTime.Now.ToString("yyyy:MM:dd HH:mm:ss"));
                if (options.RotateImage)
                {
                    imageChanged = await FixOrientationAndResizeAsync(media.Path, options, exif);
                }
                else
                {
                    imageChanged = await ResizeAsync(media.Path, options.PhotoSize, options.CompressionQuality, options.CustomPhotoSize, options.MaxWidthHeight, exif);
                }
                SetMissingMetadata(exif, options.Location);
                if (imageChanged)
                {
                    var newExif = new ExifInterface(media.Path);
                    newExif.Copy(exif);
                    newExif.SaveAttributes();
                }
                else
                {
                    exif.SaveAttributes();
                }
                UpdateMetadata(ref media, exif);

                if (options.SaveToAlbum)
                {
                    SaveMediaToAlbum(options, media);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Unable to check orientation: " + ex);
            }

            return(media);
        }