Example usage for android.media ExifInterface getAttributeInt

List of usage examples for android.media ExifInterface getAttributeInt

Introduction

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

Prototype

public int getAttributeInt(String tag, int defaultValue) 

Source Link

Document

Returns the integer value of the specified tag.

Usage

From source file:Main.java

/**
 * Returns a matrix with rotation set based on Exif orientation tag.
 * If the orientation is undefined or 0 null is returned.
 *
 * @param pathToOriginal Path to original image file that may have exif data.
 * @return  A rotation in degrees based on exif orientation
 *///w w  w  .ja  v  a2s.com
public static int getOrientation(String pathToOriginal) {
    int degreesToRotate = 0;
    try {
        ExifInterface exif = new ExifInterface(pathToOriginal);
        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                ExifInterface.ORIENTATION_UNDEFINED);
        if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
            degreesToRotate = 90;
        } else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
            degreesToRotate = 180;
        } else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
            degreesToRotate = 270;
        }
    } catch (Exception e) {
        if (Log.isLoggable(TAG, Log.ERROR)) {
            Log.e(TAG, "Unable to get orientation for image with path=" + pathToOriginal, e);
        }
    }
    return degreesToRotate;
}

From source file:Main.java

public static int getRotation(String jpegFilePath) {
    int degrees = 0;
    try {//w w w  .  j  a v  a 2  s  . c o  m
        ExifInterface exifInterface = new ExifInterface(jpegFilePath);
        int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                ExifInterface.ORIENTATION_NORMAL);
        switch (orientation) {
        case ExifInterface.ORIENTATION_ROTATE_90:
            degrees = 90;
            break;
        case ExifInterface.ORIENTATION_ROTATE_180:
            degrees = 180;
            break;
        case ExifInterface.ORIENTATION_ROTATE_270:
            degrees = 270;
            break;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return degrees;
}

From source file:Main.java

public static Matrix rotateImage(Context context, Uri imageUri, File f) {
    Matrix matrix = new Matrix();
    try {/*from   w w  w  . j  a v a  2  s  . co m*/
        if (imageUri != null) {
            context.getContentResolver().notifyChange(imageUri, null);
        }
        ExifInterface exif = new ExifInterface(f.getAbsolutePath());
        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

        switch (orientation) {
        case ExifInterface.ORIENTATION_ROTATE_270:
            matrix.postRotate(270);
            break;
        case ExifInterface.ORIENTATION_ROTATE_180:
            matrix.postRotate(180);
            break;
        case ExifInterface.ORIENTATION_ROTATE_90:
            matrix.postRotate(90);
            break;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return matrix;
}

From source file:Main.java

@TargetApi(Build.VERSION_CODES.ECLAIR)
public static int getImageDegree(String path) {
    int degree = 0;
    try {// ww  w  . j ava 2  s . c o 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

/**
 * This method resets bitmap orientation to 0 if not.
 * /*from  w w w. j av a  2 s.co  m*/
 * @param path
 * @param bitmap
 * @return bitmap with 0 degree orientation
 */
public static Bitmap resetBitmapOrientation(String path, Bitmap bitmap) {
    // TODO Auto-generated method stub
    int rotate = 0;
    try {
        ExifInterface ei = new ExifInterface(path);
        int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
        switch (orientation) {
        case ExifInterface.ORIENTATION_ROTATE_90:
            rotate = 90;
            break;
        case ExifInterface.ORIENTATION_ROTATE_180:
            rotate = 180;
            break;
        case ExifInterface.ORIENTATION_ROTATE_270:
            rotate = 270;
            break;
        default:
            // do nothing...
            break;
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    if (rotate == 0) {
        return bitmap;
    } else {
        Matrix m = new Matrix();
        m.postRotate(rotate);
        return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), m, true);
    }
}

From source file:Main.java

public static int getPictureDegree(String imagePath) {
    int i = 0;//from w  w  w.  j  ava 2s  . c  om
    try {
        ExifInterface localExifInterface = new ExifInterface(imagePath);
        int j = localExifInterface.getAttributeInt("Orientation", 1);
        switch (j) {
        case 6:
            i = 90;
            break;
        case 3:
            i = 180;
            break;
        case 8:
            i = 270;
        case 4:
        case 5:
        case 7:
        default:
            break;
        }
    } catch (IOException localIOException) {
        localIOException.printStackTrace();
    }
    return i;
}

From source file:Main.java

public static int getOrientationFromExif(String imagePath) {
    int orientation = 0;
    try {//from w  w  w. ja v  a 2s. c om
        ExifInterface exif = new ExifInterface(imagePath);
        int exifOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                ExifInterface.ORIENTATION_NORMAL);

        switch (exifOrientation) {
        case ExifInterface.ORIENTATION_ROTATE_270:
            orientation = 270;

            break;
        case ExifInterface.ORIENTATION_ROTATE_180:
            orientation = 180;

            break;
        case ExifInterface.ORIENTATION_ROTATE_90:
            orientation = 90;

            break;

        case ExifInterface.ORIENTATION_NORMAL:
            orientation = 0;

            break;
        default:
            break;
        }
    } catch (Exception e) {

    }

    return orientation;
}

From source file:Main.java

/**
 * Get the orientation rotate degree of an image file.
 * /*from  w ww  .j  a  v a 2  s.c  o  m*/
 * @param soureFilePath
 *            The file path of the image.
 * @return The orientation rotate degree of an image file(90 or 180 or 270). <br>
 *         0, if the image file has no orientation rotate degree or the
 *         source file is not a valid image file.
 */
public static int getOrientationRotateDegree(String soureFilePath) {
    if (null == soureFilePath) {
        return 0;
    }

    try {
        ExifInterface exif = new ExifInterface(soureFilePath);
        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
        switch (orientation) {
        case ExifInterface.ORIENTATION_ROTATE_90: {
            return 90;
        }
        case ExifInterface.ORIENTATION_ROTATE_180: {
            return 180;
        }
        case ExifInterface.ORIENTATION_ROTATE_270: {
            return 270;
        }
        default: {
            return 0;
        }
        }

    } catch (IOException e) {
        e.printStackTrace();
        return 0;
    }
}

From source file:Main.java

public static int getCameraPhotoOrientation(Context context, Uri imageUri, String imagePath) {
    int rotate = 0;
    try {//from w  ww  . j  a  v a  2  s .  com
        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 getDegress(String path) {
    int degree = 0;
    try {/*www .j  a  va 2s.  c o 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;
}