Skip to content

Setting size and quality options when picking #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Brosten opened this issue May 4, 2021 · 14 comments
Open

Setting size and quality options when picking #25

Brosten opened this issue May 4, 2021 · 14 comments
Labels
enhancement New feature or request

Comments

@Brosten
Copy link

Brosten commented May 4, 2021

Summary

When using the legacy CrossMedia plugin, you have the ability to define image max width/height and compressionQuality. Are there any plans to implement this kind of functionality?

API Changes

Perhaps looking at CrossMedia.Current.PickPhotoAsync could give some inspiration

@Brosten Brosten added the enhancement New feature or request label May 4, 2021
@dimonovdd
Copy link
Owner

dimonovdd commented May 4, 2021

It's definitely not in my plans. But if someone does I won't mind. Unfortunately I don't have much time. And I have to get my priorities right
I plan to make previews of images and video files

@Brosten
Copy link
Author

Brosten commented May 5, 2021

While we are at it, getting it rotated to the origin set in the exif info wouldn't hurt.
Currently I borrowed some code from here
https://stackoverflow.com/questions/44181914/iphone-image-orientation-wrong-when-resizing-with-skiasharp
to do that stuff.

@dimonovdd
Copy link
Owner

Do you have problems with origin image rotation in this plugin?

@Brosten
Copy link
Author

Brosten commented May 6, 2021

Well, it's not a huge problem. But if I just take the photo bytes and show it on screen, it might show with the wrong rotation.
So... I'm rotating it myself, before I show it.

Perhaps some basic image manipulation of byte[] images could be something for the CommunityToolkit.
Something like
ResizeAndRotate(int MaxWidthOrHeight, int quality)

@dimonovdd
Copy link
Owner

I had no problem with the rotated image. If you can provide a description of how to reproduce this. I'll try to fix it

@Brosten
Copy link
Author

Brosten commented May 6, 2021

I think it's camera app dependent. I'm using Piel 4 (Android) with default camera.
Don't have time today to fix repo, but I'll try to fix one soon...

@Brosten
Copy link
Author

Brosten commented May 7, 2021

On my android (Pixel 4) the following code draws the image upside down.
<StackLayout HeightRequest="500" WidthRequest="300"> <Button Text="Pick photo" Clicked="Button_Clicked" /> <Image x:Name="theImage" HeightRequest="200" WidthRequest="200" /> </StackLayout>

private async void Button_Clicked(object sender, EventArgs e) { var image = await NativeMedia.MediaGallery.PickAsync(types: NativeMedia.MediaFileType.Image); var file = image.Files.FirstOrDefault(); var stream = await file.OpenReadAsync(); theImage.Source = ImageSource.FromStream(() => stream); }

on iOS it doesn't rotate at all. And if trying to rotate with SkiaSharp, the codec will end up being null. This is not the case if using PickPhotoAsync from Xamarin.Essentials.

         using (var inputStream = new SKManagedStream(stream));
         using (var codec = SKCodec.Create(inputStream));
// Here codec is null for iOS, but not for android

@Brosten
Copy link
Author

Brosten commented Jun 2, 2021

@dimonovdd : Were you able to reproduce this?

@Brosten
Copy link
Author

Brosten commented Aug 31, 2021

Regarding what I wrote about codec being null for iOS, it seems to have been fixed in some way. At least it works fine for me in iOS as well now.

Width, Height, Quality and orientation are still things i'd like to see! =)

@dimonovdd
Copy link
Owner

Width, Height, Quality and orientation are still things i'd like to see! =)

Do you have any ideas? how to implement it for all platforms? Have you tried using MetadataExtractor?

@Adam-Langley
Copy link

Hi guys,
I recently came across your library and have started using it - great work thanks.
I also had the issue, that a photo taken in landscape orientation would display on other operating systems in incorrect orientation.
My scenario is that the image is picked and uploaded to a server, then the server uses the image in a DevExpress report, and hence renders into a final PDF with incorrect orientation.
I also required max dimension, and quality level support, so I just applied that after the image was picked (caveat - it's iOS specific).
The act of using the iOS APIs to re-encode an image, has the side effect of baking the orientation into the resultant image - hence it implicitly solves the orientation issue.

  public static async Task<Stream> OpenReadAsync(IMediaFile mediaFile)
  {
      using (var imageData = NSData.FromStream(await mediaFile.OpenReadAsync()))
      using (var image = UIImage.LoadFromData(imageData))
      {
          // load image
          using (var resized = image.ResizeImage(800, float.MaxValue))
          {
              return resized.AsJPEG(0.25f).AsStream();
          }
      }
  }

@dimonovdd
Copy link
Owner

@Adam-Langley Hi. You can create a PR. But keep in mind that using UIImage, we lose metadata.

@cdavidyoung
Copy link

I could sure use the compression of CrossMedia.Current.PickPhotoAsync that I used with Xamarin. I am using MediaGallery in Maui but the photos on iOS are about 10x larger than they need to be and cause my data files to be huge.

If the CrossMedia code were available on github I would be tempted to try and port the compression feature to the MediaGallery repo. I don't see it though. Can someone point it out?

@dimonovdd
Copy link
Owner

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants