How to get JPEG's Exif rotation info in Unity? (Android); UnityでExifの回転情報を取得する方法

f:id:peroon:20181123121758p:plain

Use the asset.

assetstore.unity.com

Then

// assume that you already have image path

// Exif
private NativeGallery.ImageOrientation lastLoadedOrientation;
var info = NativeGallery.GetImageProperties(an_media.Path);
lastLoadedOrientation = info.orientation;

...

        // Exif roatated image may be used...
        if(lastLoadedOrientation == NativeGallery.ImageOrientation.Rotate270)
        {
            image = RotateTexture(image, false);
        }

// Util
    Texture2D RotateTexture(Texture2D originalTexture, bool clockwise)
    {
        Color32[] original = originalTexture.GetPixels32();
        Color32[] rotated = new Color32[original.Length];
        int w = originalTexture.width;
        int h = originalTexture.height;

        int iRotated, iOriginal;

        for (int j = 0; j < h; ++j)
        {
            for (int i = 0; i < w; ++i)
            {
                iRotated = (i + 1) * h - j - 1;
                iOriginal = clockwise ? original.Length - 1 - (j * w + i) : j * w + i;
                rotated[iRotated] = original[iOriginal];
            }
        }

        Texture2D rotatedTexture = new Texture2D(h, w);
        rotatedTexture.SetPixels32(rotated);
        rotatedTexture.Apply();
        return rotatedTexture;
    }