Skip to content

Instantly share code, notes, and snippets.

@instantiator
Created August 22, 2017 23:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save instantiator/6a5a41266a66afd58aaefbeede3d07bc to your computer and use it in GitHub Desktop.
Save instantiator/6a5a41266a66afd58aaefbeede3d07bc to your computer and use it in GitHub Desktop.
package com.flt.bothie2.utility;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.util.Log;
import android.util.Pair;
import com.drew.imaging.ImageMetadataReader;
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.drew.metadata.MetadataException;
import com.drew.metadata.exif.ExifIFD0Directory;
import com.drew.metadata.exif.ExifImageDirectory;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
/**
* This class provides helper methods to manage the inspection of images that support the EXIF
* format, and the rotation of a Bitmap to orient it correctly according to this information.
* The supporting EXIF library is: https://github.com/drewnoakes/metadata-extractor
*/
public class ImageOrientationHelper {
private static final String TAG = "IOH";
public static Bitmap rotateImage(Bitmap bmp, float rotation) {
Matrix matrix = new Matrix();
matrix.postRotate(rotation);
return applyMatrix(bmp, matrix);
}
public static Bitmap applyMatrix(Bitmap bmp, Matrix matrix) {
return Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);
}
public static Integer getOrientationFromExifDataIn(InputStream stream) throws Exception {
Metadata metadata = ImageMetadataReader.readMetadata(stream);
List<Pair<Directory, Integer>> pairs = new ArrayList<>();
pairs.add(new Pair<Directory, Integer>(metadata.getFirstDirectoryOfType(ExifIFD0Directory.class), ExifIFD0Directory.TAG_ORIENTATION));
pairs.add(new Pair<Directory, Integer>(metadata.getFirstDirectoryOfType(ExifImageDirectory.class), ExifImageDirectory.TAG_ORIENTATION));
Integer orientation = null;
try {
for (Pair<Directory,Integer> pair : pairs) {
Directory directory = pair.first; Integer tag = pair.second;
if (directory != null && directory.containsTag(tag)) {
orientation = directory.getInt(tag);
} else {
Log.w(TAG, "TAG_ORIENTATION was not found in " + directory.getName());
}
}
} catch (MetadataException me) {
Log.w(TAG, "Could not extract orientation metadata.");
}
return orientation;
}
public static Matrix matrixForOrientation(int orientation) {
Matrix matrix = new Matrix();
switch (orientation) {
case ExifInterface.ORIENTATION_NORMAL:
// NOP
break;
case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
matrix.setScale(-1, 1);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
matrix.setRotate(180);
break;
case ExifInterface.ORIENTATION_FLIP_VERTICAL:
matrix.setRotate(180);
matrix.postScale(-1, 1);
break;
case ExifInterface.ORIENTATION_TRANSPOSE:
matrix.setRotate(90);
matrix.postScale(-1, 1);
break;
case ExifInterface.ORIENTATION_ROTATE_90:
matrix.setRotate(90);
break;
case ExifInterface.ORIENTATION_TRANSVERSE:
matrix.setRotate(-90);
matrix.postScale(-1, 1);
break;
case ExifInterface.ORIENTATION_ROTATE_270:
matrix.setRotate(-90);
break;
default:
Log.w(TAG, "Unexpected exif orientation value:" + orientation);
break;
}
return matrix;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment