Skip to content
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

IImage - Exporting portrait JPG photos on Android rotates them #6915

Closed
janseris opened this issue May 6, 2022 · 4 comments
Closed

IImage - Exporting portrait JPG photos on Android rotates them #6915

janseris opened this issue May 6, 2022 · 4 comments
Labels
s/needs-verification Indicates that this issue needs initial verification before further triage will happen t/bug Something isn't working

Comments

@janseris
Copy link

janseris commented May 6, 2022

Description

Every photo which is taken on portrait, is exported using Image.AsBytes() as landscape -> rotated counter-clockwise 90 degrees (rotate to the left).
Sample poject: MauiApp2 rotated portrait JPGs.zip


Btw.: I am experiencing the same when exporting using Android.Graphics.Bitmap in Xamarin.

    byte[] XamarinResize(byte[] image, int width, int height)
    {
        var bitmap = Create(image);
        var matrix = new Android.Graphics.Matrix();
        var scaleWidth = ((float)width) / bitmap.Width;
        var scaleHeight = ((float)height) / bitmap.Height;
        matrix.PreScale(scaleWidth, scaleHeight);
        var resizedBitmap = Android.Graphics.Bitmap.CreateBitmap(bitmap, 0, 0, bitmap.Width, bitmap.Height, matrix, true); //size of original image must be entered
        byte[] exported = Export(resizedBitmap, 80);
        return exported;
    }

    //https://stackoverflow.com/questions/25976002/saving-bitmap-to-file-xamarin-monodroid
    byte[] Export(Android.Graphics.Bitmap bitmap, int jpgQuality)
    {
        using (var ms = new MemoryStream())
        {
            bitmap.Compress(Android.Graphics.Bitmap.CompressFormat.Jpeg, jpgQuality, ms);
            return ms.ToArray();
        }
    }

From this thread, it seems like the issue is that when a Bitmap is created, the Exif info is lost and it must be passed around to keep the original rotation.
https://stackoverflow.com/questions/66437183/xamarin-android-cant-read-exif-data-from-the-rotated-image-to-check-whether-the

Edit: Confirmed - all EXIF data is lost when exporting a JPG image using MAUI Graphics or Android.Graphics on Android. No property is preserved (datetime, device info, gps coordinates, rotation, ...). Everything is cut.

Test project for confirmation of this:

MauiApp2 EXIF properties disappearing demonstration.zip


Steps to Reproduce

export IImage as bytes

Version with bug

Release Candidate 2 (current)

Last version that worked well

Unknown/Other

Affected platforms

Android, I was not able test on other platforms

Affected platform versions

Android 11 (API 30)

Did you find any workaround?

No because there is no API to detect image orientation/rotation or to rotate the image

Relevant log output

No response

@janseris janseris added s/needs-verification Indicates that this issue needs initial verification before further triage will happen t/bug Something isn't working labels May 6, 2022
@janseris janseris changed the title Saving a downsized image on Android rotates the image Exporting an image on Android rotates the image May 6, 2022
@janseris janseris changed the title Exporting an image on Android rotates the image Exporting some JPGs on Android rotates the image May 6, 2022
@janseris janseris changed the title Exporting some JPGs on Android rotates the image Exporting portrait JPG photos on Android rotates them May 7, 2022
@XamlTest XamlTest added s/triaged Issue has been reviewed and removed s/triaged Issue has been reviewed labels May 7, 2022
@janseris janseris changed the title Exporting portrait JPG photos on Android rotates them IImage - Exporting portrait JPG photos on Android rotates them May 7, 2022
@drasticactions
Copy link
Contributor

Maui Graphics is its own repo: https://github.com/dotnet/Microsoft.Maui.Graphics

And I think your issue has to do with the platform itself, so https://github.com/xamarin/xamarin-android

@jonathanpeppers Is this something that could be addressed here or should it be moved to the other repos?

@jonathanpeppers
Copy link
Member

I don't see how the above would be a problem in xamarin-android at all. If you call the Android APIs, and they give this result -- the same would happen in Java or Kotlin. Do you get the same result in an Android Studio project?

I don't think there is anything for us to fix here. You could file an issue on Maui.Graphics if one of their APIs is not working as expected.

@janseris
Copy link
Author

janseris commented May 7, 2022

@jonathanpeppers

Do you get the same result in an Android Studio project?

I have never used Android Studio or programmed a mobile app in Java or Kotlin.

I will transfer this to Maui.Graphics then.

@drasticactions
Copy link
Contributor

@janseris His point is that this is something you can replicate with an Android app written in Java using the regular tooling. It's not a bug in the Xamarin.Android binding, nor is it something MAUI introduced. If you called those APIs from Java, you would (most likely) get the same result.

@ghost ghost locked as resolved and limited conversation to collaborators Jun 7, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
s/needs-verification Indicates that this issue needs initial verification before further triage will happen t/bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants