Skip to content

Commit

Permalink
Use ExifInterface to parse orientation data on OMR1+ in Glide.
Browse files Browse the repository at this point in the history
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=192690571
  • Loading branch information
sjudd committed Apr 25, 2018
1 parent 9328999 commit a3f8114
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
9 changes: 9 additions & 0 deletions library/src/main/java/com/bumptech/glide/Glide.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;
import android.os.ParcelFileDescriptor;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
Expand Down Expand Up @@ -54,6 +55,7 @@
import com.bumptech.glide.load.resource.bitmap.ByteBufferBitmapDecoder;
import com.bumptech.glide.load.resource.bitmap.DefaultImageHeaderParser;
import com.bumptech.glide.load.resource.bitmap.Downsampler;
import com.bumptech.glide.load.resource.bitmap.ExifInterfaceImageHeaderParser;
import com.bumptech.glide.load.resource.bitmap.ResourceBitmapDecoder;
import com.bumptech.glide.load.resource.bitmap.StreamBitmapDecoder;
import com.bumptech.glide.load.resource.bitmap.UnitBitmapDecoder;
Expand Down Expand Up @@ -331,6 +333,13 @@ private static void throwIncorrectGlideModule(Exception e) {
final Resources resources = context.getResources();

registry = new Registry();
// Right now we're only using this parser for HEIF images, which are only supported on OMR1+.
// If we need this for other file types, we should consider removing this restriction.
// Note that order here matters. We want to check the ExifInterface parser first for orientation
// and then fall back to DefaultImageHeaderParser for other fields.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
registry.register(new ExifInterfaceImageHeaderParser());
}
registry.register(new DefaultImageHeaderParser());

Downsampler downsampler = new Downsampler(registry.getImageHeaderParsers(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.bumptech.glide.load.resource.bitmap;

import android.media.ExifInterface;
import android.support.annotation.NonNull;
import com.bumptech.glide.load.ImageHeaderParser;
import com.bumptech.glide.load.engine.bitmap_recycle.ArrayPool;
import com.bumptech.glide.util.ByteBufferUtil;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;

/**
* Uses {@link ExifInterface} to parse orientation data.
*
* <p>ExifInterface supports the HEIF format on OMR1+. Glide's {@link DefaultImageHeaderParser}
* doesn't currently support HEIF. In the future we should reconcile these two classes, but for
* now this is a simple way to ensure that HEIF files are oriented correctly on platforms where
* they're supported.
*/
public final class ExifInterfaceImageHeaderParser implements ImageHeaderParser {

@NonNull
@Override
public ImageType getType(@NonNull InputStream is) throws IOException {
return ImageType.UNKNOWN;
}

@NonNull
@Override
public ImageType getType(@NonNull ByteBuffer byteBuffer) throws IOException {
return ImageType.UNKNOWN;
}

@Override
public int getOrientation(@NonNull InputStream is, @NonNull ArrayPool byteArrayPool)
throws IOException {
ExifInterface exifInterface = new ExifInterface(is);
return exifInterface.getAttributeInt(
ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
}

@Override
public int getOrientation(@NonNull ByteBuffer byteBuffer, @NonNull ArrayPool byteArrayPool)
throws IOException {
return getOrientation(ByteBufferUtil.toStream(byteBuffer), byteArrayPool);
}
}

0 comments on commit a3f8114

Please sign in to comment.