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

public static int getExifRotation(File imageFile) {
    if (imageFile == null)
        return 0;
    try {/*from  ww  w .ja v  a 2 s. c o  m*/
        ExifInterface exif = new ExifInterface(imageFile.getAbsolutePath());
        // We only recognize a subset of orientation tag values
        switch (exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED)) {
        case ExifInterface.ORIENTATION_ROTATE_90:
            return 90;
        case ExifInterface.ORIENTATION_ROTATE_180:
            return 180;
        case ExifInterface.ORIENTATION_ROTATE_270:
            return 270;
        default:
            return ExifInterface.ORIENTATION_UNDEFINED;
        }
    } catch (IOException e) {
        return 0;
    }
}

From source file:Main.java

public static Bitmap processToteImage(String image_path, Bitmap bm) {
    Bitmap bitmap = null;//  w ww.  j  a  v a2  s. co m
    try {
        ExifInterface exif = new ExifInterface(image_path);
        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
        Log.d("EXIF", "Exif: " + orientation);
        Matrix matrix = new Matrix();
        if (orientation == 6) {
            matrix.postRotate(90);
        } else if (orientation == 3) {
            matrix.postRotate(180);
        } else if (orientation == 8) {
            matrix.postRotate(270);
        }
        bitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true); // rotating
        // bitmap
    } catch (Exception e) {

    }
    return bitmap;
}

From source file:Main.java

public static int getOrientation(final Uri photoUri) {
    ExifInterface exifInterface = null;//from   w w w  .  j a  v  a  2s .  c  o m
    try {
        exifInterface = new ExifInterface(photoUri.toString());
    } catch (IOException e) {
        e.printStackTrace();
    }
    return exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, -1);
}

From source file:Main.java

public static boolean copyExifRotation(File sourceFile, File destFile) {
    if (sourceFile == null || destFile == null)
        return false;
    try {/*from  w  w w.  ja v  a 2s .  c  o  m*/
        ExifInterface exifSource = new ExifInterface(sourceFile.getAbsolutePath());
        ExifInterface exifDest = new ExifInterface(destFile.getAbsolutePath());
        exifDest.setAttribute(ExifInterface.TAG_ORIENTATION,
                exifSource.getAttribute(ExifInterface.TAG_ORIENTATION));
        exifDest.saveAttributes();
        return true;
    } catch (IOException e) {
        return false;
    }
}

From source file:Main.java

/**
 * Get Image Orientation from file//from  w ww.  java  2 s .  c om
 *
 * @param imageFile File for which orientation is changed
 * @return Current Orientation
 */
public static int getBitmapOrientation(File imageFile) {
    try {
        ExifInterface exif = new ExifInterface(imageFile.getAbsolutePath());
        return exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
    } catch (IOException e) {
    }
    return 0;
}

From source file:Main.java

public synchronized static int getPhotoOrientationDegree(String filepath) {
    int degree = 0;
    ExifInterface exif = null;// ww  w .  j  a  v  a 2 s .co m

    try {
        exif = new ExifInterface(filepath);
    } catch (IOException e) {
        // Log.d(PhotoUtil.class.getSimpleName(), "Error: "+e.getMessage());
    }

    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;
            }

        }
    }
    // Log.d(PhotoUtil.class.getSimpleName(), "Photo Degree: "+degree);
    return degree;
}

From source file:Main.java

/**
 * Extracts the EXIF rotation tag of an image file.
 *
 * @param filePath Path to the image file
 * @return Rotation in degrees/*from w  w w .j  a v a2 s .  c om*/
 * @throws IOException
 */
public static int getExifRotation(String filePath) throws IOException {
    ExifInterface exif = new ExifInterface(filePath);

    int exifOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

    switch (exifOrientation) {
    case ExifInterface.ORIENTATION_ROTATE_90:
        return 90;
    case ExifInterface.ORIENTATION_ROTATE_180:
        return 180;
    case ExifInterface.ORIENTATION_ROTATE_270:
        return 270;
    default:
        return 0;
    }
}

From source file:Main.java

public static int getCameraPhotoOrientation(Context context, Uri imageUri, String imagePath) {
    int rotate = 0;
    try {/*from  www .  j a va2  s. c  o m*/
        context.getContentResolver().notifyChange(imageUri, null);
        File imageFile = new File(imagePath);

        ExifInterface exif = new ExifInterface(imageFile.getAbsolutePath());
        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

        switch (orientation) {
        case ExifInterface.ORIENTATION_ROTATE_270:
            rotate = 270;
            break;
        case ExifInterface.ORIENTATION_ROTATE_180:
            rotate = 180;
            break;
        case ExifInterface.ORIENTATION_ROTATE_90:
            rotate = 90;
            break;
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
    return rotate;
}

From source file:Main.java

public static int getRotationAngle(String photoPath) {
    ExifInterface ei = null;//from   w w w . j  ava  2 s  . co  m
    try {
        ei = new ExifInterface(photoPath);
    } catch (IOException e) {
        Log.e(TAG, "Unexpected error occurred when getting rotation angle.");
    }
    int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);

    switch (orientation) {
    case ExifInterface.ORIENTATION_ROTATE_90:
        Log.d(TAG, "90 Degrees rotation needed");
        return 90;
    case ExifInterface.ORIENTATION_ROTATE_180:
        Log.d(TAG, "180 Degrees rotation needed");
        return 180;
    case ExifInterface.ORIENTATION_ROTATE_270:
        Log.d(TAG, "270 Degrees rotation needed");
        return 270;
    case ExifInterface.ORIENTATION_NORMAL:
    default:
        Log.d(TAG, "0 Degrees rotation needed");
        return 0;
    }
}

From source file:Main.java

/**
 * <pre>//from  w  ww .  j a  va  2 s . co m
 * Get rotation angle of an image.
 * This information is stored in image file. Therefore, this method needs path to file, not a {@link Bitmap} object or byte array.
 * </pre>
 * @param imagePath absolute path to image file
 * @return one of 0, 90, 180, 270
 */
public static int getImageRotateAngle(String imagePath) throws IOException {
    ExifInterface exif = new ExifInterface(imagePath);
    int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
    int angle = 0;
    if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
        angle = 90;
    } else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
        angle = 180;
    } else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
        angle = 270;
    }

    return angle;
}