Skip to content
This repository has been archived by the owner on Jul 5, 2024. It is now read-only.

Commit

Permalink
Merge branch 'master' into cancellation
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesmontemagno committed Dec 21, 2018
2 parents d2a6711 + 9c19e16 commit 71d5dac
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 47 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
* Fix for #514: Videos on Android 8.1 stop after short time.
* Upgrade to CurrentActivityPlugin 2.0 and Permissions 3.0
* Ability on iOS to specify pop over style
* Fix for #642: Android dock/undock issue
* Fix for #639: Null reference sometimes when can't accessing file on Android.
* Fix for #553: When not rotating image on android ensure we take max width/height into consideration.
* Fix for #545: iOS error sometimes when saving metadata.
* Fix for #608: Possible null on iOS when picking video sometimes.

### [3.1.3]
* Remove need for Android Target versions (always use File Provider via #442 and @ddobrev)
Expand Down
62 changes: 49 additions & 13 deletions src/Media.Plugin/Android/MediaImplementation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ bool IsValidExif(ExifInterface exif)
}
else
{
await ResizeAsync(media.Path, options.PhotoSize, options.CompressionQuality, options.CustomPhotoSize, originalMetadata);
await ResizeAsync(media.Path, options, originalMetadata);
}
if (options.SaveMetaData && IsValidExif(originalMetadata))
{
Expand Down Expand Up @@ -218,7 +218,7 @@ bool IsValidExif(ExifInterface exif)
}
else
{
await ResizeAsync(media.Path, options.PhotoSize, options.CompressionQuality, options.CustomPhotoSize, exif);
await ResizeAsync(media.Path, options, exif);
}

if (options.SaveMetaData && IsValidExif(exif))
Expand Down Expand Up @@ -705,22 +705,49 @@ int CalculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHe
return inSampleSize;
}

/// <summary>
/// Resize Image Async
/// </summary>
/// <param name="filePath">The file image path</param>
/// <param name="photoSize">Photo size to go to.</param>
/// <param name="quality">Image quality (1-100)</param>
/// <param name="customPhotoSize">Custom size in percent</param>
/// <param name="exif">original metadata</param>
/// <returns>True if rotation or compression occured, else false</returns>
public Task<bool> ResizeAsync(string filePath, PhotoSize photoSize, int quality, int customPhotoSize, ExifInterface exif)

/// <summary>
/// Rotate an image if required and saves it back to disk.
/// </summary>
/// <param name="filePath">The file image path</param>
/// <param name="mediaOptions">The options.</param>
/// <param name="exif">original metadata</param>
/// <returns>True if rotation or compression occured, else false</returns>
public Task<bool> ResizeAsync(string filePath, PickMediaOptions mediaOptions, ExifInterface exif)
{
return ResizeAsync(
filePath,
new StoreCameraMediaOptions
{
PhotoSize = mediaOptions.PhotoSize,
CompressionQuality = mediaOptions.CompressionQuality,
CustomPhotoSize = mediaOptions.CustomPhotoSize,
MaxWidthHeight = mediaOptions.MaxWidthHeight,
RotateImage = mediaOptions.RotateImage,
SaveMetaData = mediaOptions.SaveMetaData
},
exif);
}

/// <summary>
/// Resize Image Async
/// </summary>
/// <param name="filePath">The file image path</param>
/// <param name="photoSize">Photo size to go to.</param>
/// <param name="quality">Image quality (1-100)</param>
/// <param name="customPhotoSize">Custom size in percent</param>
/// <param name="exif">original metadata</param>
/// <returns>True if rotation or compression occured, else false</returns>
public Task<bool> ResizeAsync(string filePath, StoreCameraMediaOptions mediaOptions, ExifInterface exif)
{
if (string.IsNullOrWhiteSpace(filePath))
return Task.FromResult(false);

try
{
var photoSize = mediaOptions.PhotoSize;
var customPhotoSize = mediaOptions.CustomPhotoSize;
var quality = mediaOptions.CompressionQuality;
return Task.Run(() =>
{
try
Expand Down Expand Up @@ -755,7 +782,16 @@ public Task<bool> ResizeAsync(string filePath, PhotoSize photoSize, int quality,
//already on background task
BitmapFactory.DecodeFile(filePath, options);

var finalWidth = (int)(options.OutWidth * percent);
if (mediaOptions.PhotoSize == PhotoSize.MaxWidthHeight && mediaOptions.MaxWidthHeight.HasValue)
{
var max = Math.Max(options.OutWidth, options.OutHeight);
if (max > mediaOptions.MaxWidthHeight)
{
percent = (float)mediaOptions.MaxWidthHeight / (float)max;
}
}

var finalWidth = (int)(options.OutWidth * percent);
var finalHeight = (int)(options.OutHeight * percent);

//set scaled image dimensions
Expand Down
6 changes: 3 additions & 3 deletions src/Media.Plugin/Android/MediaPickerActivity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace Plugin.Media
/// <summary>
/// Picker
/// </summary>
[Activity(ConfigurationChanges= ConfigChanges.Orientation | ConfigChanges.ScreenSize)]
[Activity(ConfigurationChanges= ConfigChanges.Orientation | ConfigChanges.ScreenSize | ConfigChanges.UiMode)]
public class MediaPickerActivity
: Activity, Android.Media.MediaScannerConnection.IOnScanCompletedListener
{
Expand Down Expand Up @@ -315,9 +315,9 @@ internal static Task<MediaPickedEventArgs> GetMediaFileAsync(Context context, in
return pathFuture.ContinueWith(t =>
{

var resultPath = t.Result.Item1;
var resultPath = t?.Result?.Item1;
var aPath = originalPath;
if (resultPath != null && File.Exists(t.Result.Item1))
if (resultPath != null && File.Exists(resultPath))
{
var mf = new MediaFile(resultPath, () =>
{
Expand Down
17 changes: 11 additions & 6 deletions src/Media.Plugin/iOS/MediaPickerDelegate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,9 @@ private async Task<MediaFile> GetPictureMediaFile(NSDictionary info)
}
else
{
meta = PhotoLibraryAccess.GetPhotoLibraryMetadata(info[UIImagePickerController.ReferenceUrl] as NSUrl);
var url = info[UIImagePickerController.ReferenceUrl] as NSUrl;
if(url != null)
meta = PhotoLibraryAccess.GetPhotoLibraryMetadata(url);
}
}

Expand Down Expand Up @@ -533,24 +535,27 @@ private bool SaveImageWithMetadata(UIImage image, float quality, NSDictionary me

private async Task<MediaFile> GetMovieMediaFile(NSDictionary info)
{
var url = (NSUrl)info[UIImagePickerController.MediaURL];
var url = info[UIImagePickerController.MediaURL] as NSUrl;
if (url == null)
return null;

var path = GetOutputPath(MediaImplementation.TypeMovie,
options.Directory ?? ((IsCaptured) ? string.Empty : "temp"),
options.Name ?? Path.GetFileName(url.Path));
options?.Directory ?? ((IsCaptured) ? string.Empty : "temp"),
options?.Name ?? Path.GetFileName(url.Path));


File.Move(url.Path, path);

string aPath = null;
if (source != UIImagePickerControllerSourceType.Camera)
{
//try to get the album path's url
var url2 = (NSUrl)info[UIImagePickerController.ReferenceUrl];
var url2 = info[UIImagePickerController.ReferenceUrl] as NSUrl;
aPath = url2?.AbsoluteString;
}
else
{
if (options.SaveToAlbum)
if (options?.SaveToAlbum ?? false)
{
try
{
Expand Down
60 changes: 35 additions & 25 deletions src/Media.Plugin/iOS/PhotoLibraryAccess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,37 +20,47 @@ public static NSDictionary GetPhotoLibraryMetadata(NSUrl url)
NSDictionary meta = null;

var image = PHAsset.FetchAssets(new NSUrl[] { url }, new PHFetchOptions()).firstObject as PHAsset;
var imageManager = PHImageManager.DefaultManager;
var requestOptions = new PHImageRequestOptions
{
Synchronous = true,
NetworkAccessAllowed = true,
DeliveryMode = PHImageRequestOptionsDeliveryMode.HighQualityFormat,
};
imageManager.RequestImageData(image, requestOptions, (data, dataUti, orientation, info) =>
if (image == null)
return null;

try
{
try
var imageManager = PHImageManager.DefaultManager;
var requestOptions = new PHImageRequestOptions
{
var fullimage = CIImage.FromData(data);
if (fullimage?.Properties != null)
Synchronous = true,
NetworkAccessAllowed = true,
DeliveryMode = PHImageRequestOptionsDeliveryMode.HighQualityFormat,
};
imageManager.RequestImageData(image, requestOptions, (data, dataUti, orientation, info) =>
{
try
{
meta = new NSMutableDictionary
var fullimage = CIImage.FromData(data);
if (fullimage?.Properties != null)
{
[ImageIO.CGImageProperties.Orientation] = NSNumber.FromNInt ((int)(fullimage.Properties.Orientation ?? CIImageOrientation.TopLeft)),
[ImageIO.CGImageProperties.ExifDictionary] = fullimage.Properties.Exif?.Dictionary ?? new NSDictionary(),
[ImageIO.CGImageProperties.TIFFDictionary] = fullimage.Properties.Tiff?.Dictionary ?? new NSDictionary(),
[ImageIO.CGImageProperties.GPSDictionary] = fullimage.Properties.Gps?.Dictionary ?? new NSDictionary(),
[ImageIO.CGImageProperties.IPTCDictionary] = fullimage.Properties.Iptc?.Dictionary ?? new NSDictionary(),
[ImageIO.CGImageProperties.JFIFDictionary] = fullimage.Properties.Jfif?.Dictionary ?? new NSDictionary()
};
meta = new NSMutableDictionary
{
[ImageIO.CGImageProperties.Orientation] = NSNumber.FromNInt((int)(fullimage.Properties.Orientation ?? CIImageOrientation.TopLeft)),
[ImageIO.CGImageProperties.ExifDictionary] = fullimage.Properties.Exif?.Dictionary ?? new NSDictionary(),
[ImageIO.CGImageProperties.TIFFDictionary] = fullimage.Properties.Tiff?.Dictionary ?? new NSDictionary(),
[ImageIO.CGImageProperties.GPSDictionary] = fullimage.Properties.Gps?.Dictionary ?? new NSDictionary(),
[ImageIO.CGImageProperties.IPTCDictionary] = fullimage.Properties.Iptc?.Dictionary ?? new NSDictionary(),
[ImageIO.CGImageProperties.JFIFDictionary] = fullimage.Properties.Jfif?.Dictionary ?? new NSDictionary()
};
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}

});
});
}
catch
{

}

return meta;
}
Expand Down

0 comments on commit 71d5dac

Please sign in to comment.