Example usage for android.media ExifInterface TAG_ORIENTATION

List of usage examples for android.media ExifInterface TAG_ORIENTATION

Introduction

In this page you can find the example usage for android.media ExifInterface TAG_ORIENTATION.

Prototype

String TAG_ORIENTATION

To view the source code for android.media ExifInterface TAG_ORIENTATION.

Click Source Link

Document

Type is int.

Usage

From source file:Main.java

@TargetApi(Build.VERSION_CODES.ECLAIR)
public static int getImageDegree(String path) {
    int degree = 0;
    try {//from  w ww.  j  ava2 s .  co m
        ExifInterface exifInterface = new ExifInterface(path);
        int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                ExifInterface.ORIENTATION_NORMAL);
        switch (orientation) {
        case ExifInterface.ORIENTATION_ROTATE_90:
            degree = 90;
            break;
        case ExifInterface.ORIENTATION_ROTATE_180:
            degree = 180;
            break;
        case ExifInterface.ORIENTATION_ROTATE_270:
            degree = 270;
            break;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return degree;
}

From source file:Main.java

public static int getExifOrientation(String filepath) {
    int degree = 0;
    ExifInterface exif = null;// w  ww  .j  av  a  2s .c o  m
    try {
        exif = new ExifInterface(filepath);
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    if (exif != null) {
        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, -1);
        if (orientation != -1) {
            // We only recognise a subset of orientation tag values.
            switch (orientation) {
            case ExifInterface.ORIENTATION_ROTATE_90:
                degree = 90;
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                degree = 180;
                break;
            case ExifInterface.ORIENTATION_ROTATE_270:
                degree = 270;
                break;
            }

        }
    }
    return degree;
}

From source file:Main.java

public static Bitmap rotateImage(Bitmap bitmap, String storagePath) {
    Bitmap resultBitmap = bitmap;/* ww  w. j  a  v a  2 s  .co m*/
    try {
        ExifInterface exifInterface = new ExifInterface(storagePath);
        int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);

        Matrix matrix = new Matrix();

        // 1: nothing to do

        // 2
        if (orientation == ExifInterface.ORIENTATION_FLIP_HORIZONTAL) {
            matrix.postScale(-1.0f, 1.0f);
        }
        // 3
        else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
            matrix.postRotate(180);
        }
        // 4
        else if (orientation == ExifInterface.ORIENTATION_FLIP_VERTICAL) {
            matrix.postScale(1.0f, -1.0f);
        }
        // 5
        else if (orientation == ExifInterface.ORIENTATION_TRANSPOSE) {
            matrix.postRotate(-90);
            matrix.postScale(1.0f, -1.0f);
        }
        // 6
        else if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
            matrix.postRotate(90);
        }
        // 7
        else if (orientation == ExifInterface.ORIENTATION_TRANSVERSE) {
            matrix.postRotate(90);
            matrix.postScale(1.0f, -1.0f);
        }
        // 8
        else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
            matrix.postRotate(270);
        }

        // Rotate the bitmap
        resultBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
        if (resultBitmap != bitmap) {
            bitmap.recycle();
        }
    } catch (Exception exception) {
        Log.e("BitmapUtil", "Could not rotate the image: " + storagePath);
    }
    return resultBitmap;
}

From source file:Main.java

public static int getExifDegree(String filepath) {
    int degree = 0;
    ExifInterface exif;//from  ww w .j ava  2s .  co  m
    try {
        exif = new ExifInterface(filepath);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return 0;
    }

    if (exif != null) {
        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, -1);

        if (orientation != -1) {
            switch (orientation) {
            case ExifInterface.ORIENTATION_ROTATE_90:
                degree = 90;
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                degree = 180;
                break;
            case ExifInterface.ORIENTATION_ROTATE_270:
                degree = 270;
                break;
            }
        }
    }
    return degree;
}

From source file:Main.java

public static int getExifOrientation(final ExifInterface exif) {
    int degree = 0;

    if (exif != null) {
        final int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, -1);
        if (orientation != -1) {
            switch (orientation) {
            case ExifInterface.ORIENTATION_ROTATE_90:
                degree = 90;//from w  w  w .j  a v  a  2  s .  c  om
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                degree = 180;
                break;
            case ExifInterface.ORIENTATION_ROTATE_270:
                degree = 270;
                break;
            }
        }
    }
    return degree;
}

From source file:Main.java

public static int defineExifOrientation(Uri imageUri, String mimeType) {
    int rotation = 0;
    if ("image/jpeg".equalsIgnoreCase(mimeType) && "file".equals(imageUri.getScheme())) {
        try {/*from   w ww.  j a  v a  2s .  c o m*/
            ExifInterface exif = new ExifInterface(imageUri.getPath());
            int exifOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                    ExifInterface.ORIENTATION_NORMAL);
            Log.e("dsd", "exifOrientation:" + exifOrientation);
            switch (exifOrientation) {
            case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
            case ExifInterface.ORIENTATION_NORMAL:
                rotation = 0;
                break;
            case ExifInterface.ORIENTATION_TRANSVERSE:
            case ExifInterface.ORIENTATION_ROTATE_90:
                rotation = 90;
                break;
            case ExifInterface.ORIENTATION_FLIP_VERTICAL:
            case ExifInterface.ORIENTATION_ROTATE_180:
                rotation = 180;
                break;
            case ExifInterface.ORIENTATION_TRANSPOSE:
            case ExifInterface.ORIENTATION_ROTATE_270:
                rotation = 270;
                break;
            }
        } catch (IOException e) {
            Log.e("dsd", "Can't read EXIF tags from file [%s]" + imageUri);
        }
    }
    return rotation;
}

From source file:Main.java

public static Matrix getRotationMatrixForImage(Context ctx, Uri contentUri) {

    String pathToImage = getAbsolutePathFromUri(ctx, contentUri);
    ExifInterface exif;//from  w  ww  . jav a2s.c  om
    try {
        exif = new ExifInterface(pathToImage);
        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
        Matrix matrix = new Matrix();
        if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
            matrix.postRotate(90);
        } else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
            matrix.postRotate(180);
        } else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
            matrix.postRotate(270);
        }
        return matrix;
    } catch (IOException e) {
        return new Matrix();
    }
}

From source file:Main.java

/**
 * Calculates the amount of rotation needed for the image to look upright.
 * //www.  ja v  a2 s .c o  m
 * @param context
 *            the application's context
 * @param uri
 *            the Uri pointing to the file
 * @return the needed rotation as a <code>float</code>
 */
private static float rotationForImage(Context context, Uri uri) {
    if ("content".equals(uri.getScheme())) {
        String[] projection = { Images.ImageColumns.ORIENTATION };
        Cursor c = context.getContentResolver().query(uri, projection, null, null, null);
        if (c.moveToFirst()) {
            return c.getInt(0);
        }
    } else if ("file".equals(uri.getScheme())) {
        try {
            ExifInterface exif = new ExifInterface(uri.getPath());
            int rotation = (int) exifOrientationToDegrees(
                    exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL));
            return rotation;
        } catch (IOException e) {
            Log.e(TAG, "Error checking exif", e);
        }
    }
    return 0f;
}

From source file:Main.java

public static int getOrientationFromExif(File imgFile) throws IOException {
    ExifInterface exif = new ExifInterface(imgFile.getCanonicalPath());
    return exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);
}

From source file:Main.java

public static Bitmap createCorrectlyRotatedBitmapIfNeeded(Bitmap bitmap, String jpegPath, float scale) {
    // Rotate the image if necessary; all images are shot in LANDSCAPE mode
    try {/*from ww w . j  a  v  a 2  s.  c o m*/
        ExifInterface exif = new ExifInterface(jpegPath);

        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
        Log.d("CashLensUtils", "image has orientation " + Integer.toString(orientation) + ", width "
                + Integer.toString(bitmap.getWidth()) + ", height " + Integer.toString(bitmap.getHeight()));

        Matrix matrix = new Matrix();

        if (scale != 1.0f)
            matrix.preScale(scale, scale);

        // From http://sylvana.net/jpegcrop/exif_orientation.html
        // For convenience, here is what the letter F would look like if it were tagged correctly 
        // and displayed by a program that ignores the orientation tag (thus showing the stored image):
        //   (1)       2      (3)      4         5          (6)          7         (8)
        //
        //   888888  888888      88  88      8888888888  88                  88  8888888888
        //   88          88      88  88      88  88      88  88          88  88      88  88
        //   8888      8888    8888  8888    88          8888888888  8888888888          88
        //   88          88      88  88
        //   88          88  888888  888888

        if (orientation == 3)
            matrix.postRotate(180);
        else if (orientation == 6)
            matrix.postRotate(90);
        else if (orientation == 8)
            matrix.postRotate(-90);

        if (orientation != 1 || scale != 1.0f) {
            // Create a new image with the correct (maybe rotated) width/height
            Bitmap newImage = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix,
                    true);

            Log.d("CashLensUtils", "created a new image with width " + Integer.toString(newImage.getWidth())
                    + ", height " + Integer.toString(newImage.getHeight()));

            // Replace original image
            return newImage;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    return bitmap;
}