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

public static String getImageOrientation(String filepath) {
    if (filepath == null) {
        Log.e(TAG, "GPrintCommon : getImageOrientation() : filepath is null!");
        return unknown;
    }//from   w  w  w  .j  a  va  2  s .com

    int imageWidth, imageHeight;

    ExifInterface exif = null;
    try {
        exif = new ExifInterface(filepath);
    } catch (IOException e) {
        e.printStackTrace();
    }

    if (exif != null && exif.getAttributeInt(ExifInterface.TAG_IMAGE_WIDTH, 0) > 0) {
        imageWidth = exif.getAttributeInt(ExifInterface.TAG_IMAGE_WIDTH, 1);
        imageHeight = exif.getAttributeInt(ExifInterface.TAG_IMAGE_LENGTH, 1);
    } else {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 4;
        Bitmap image = BitmapFactory.decodeFile(filepath, options);
        if (image == null) {
            Log.e(TAG, "GPrintCommon : getImageOrientation() : image is invalid. " + filepath);
            return unknown;
        }

        imageWidth = image.getWidth();
        imageHeight = image.getHeight();
    }

    if (imageWidth > imageHeight) {
        return landscape;
    }

    return portrait;
}

From source file:Main.java

public static int getOrientation(Context context, Uri photoUri, File file) {
    /* it's on the external media. */
    Cursor cursor = context.getContentResolver().query(photoUri,
            new String[] { MediaStore.Images.ImageColumns.ORIENTATION }, null, null, null);
    if (cursor != null && cursor.getCount() != 1) {
        cursor.moveToFirst();/*w  w  w .  j  av  a 2 s .c  o m*/
        return cursor.getInt(0);
    }

    int rotate = 0;
    ExifInterface exif = null;

    try {
        exif = new ExifInterface(file == null ? getPath(context, photoUri) : file.getAbsolutePath());
    } catch (IOException e) {
        e.printStackTrace();
    }
    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;
    case ExifInterface.ORIENTATION_NORMAL:
        rotate = 0;
        break;
    }
    return rotate;
}

From source file:Main.java

public static int getExifOrientation(String filepath) {
    int degree = 0;
    ExifInterface exif = null;
    try {/*from w w  w.j  a  v a 2s .  com*/
        exif = new ExifInterface(filepath);
    } catch (IOException ex) {
        Log.e(TAG, "cannot read exif", ex);
    }
    if (exif != null) {
        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, -1);
        if (orientation != -1) {
            // We only recognize 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 int getExifDegree(String filepath) {
    int degree = 0;
    ExifInterface exif;
    try {/*from  ww  w  .  jav  a  2  s.c o m*/
        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:com.owncloud.android.utils.BitmapUtils.java

/**
 * Rotate bitmap according to EXIF orientation. 
 * Cf. http://www.daveperrett.com/articles/2012/07/28/exif-orientation-handling-is-a-ghetto/ 
 * @param bitmap Bitmap to be rotated/*  www .  j a  v a2s  . com*/
 * @param storagePath Path to source file of bitmap. Needed for EXIF information.
 * @return correctly EXIF-rotated bitmap
 */
public static Bitmap rotateImage(final Bitmap bitmap, final String storagePath) {
    try {
        ExifInterface exifInterface = new ExifInterface(storagePath);
        final int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);

        Matrix matrix = new Matrix();
        // 1: nothing to do

        switch (orientation) {
        case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
            matrix.postScale(-1.0f, 1.0f);
            break;
        case ExifInterface.ORIENTATION_ROTATE_180:
            matrix.postRotate(180);
            break;
        case ExifInterface.ORIENTATION_FLIP_VERTICAL:
            matrix.postScale(1.0f, -1.0f);
            break;
        case ExifInterface.ORIENTATION_TRANSPOSE:
            matrix.postRotate(-90);
            matrix.postScale(1.0f, -1.0f);
            break;
        case ExifInterface.ORIENTATION_ROTATE_90:
            matrix.postRotate(90);
            break;
        case ExifInterface.ORIENTATION_TRANSVERSE:
            matrix.postRotate(90);
            matrix.postScale(1.0f, -1.0f);
            break;
        case ExifInterface.ORIENTATION_ROTATE_270:
            matrix.postRotate(270);
            break;
        }

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

From source file:com.yanzhenjie.album.task.LocalImageLoader.java

/**
 * Read the rotation angle of the picture file.
 *
 * @param path image path.//from  w  ww .  ja  v a 2 s .c  o  m
 * @return one of 0, 90, 180, 270.
 */
public static int readDegree(String path) {
    try {
        ExifInterface exifInterface = new ExifInterface(path);
        int orientation = exifInterface.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 (Exception e) {
        return 0;
    }
}

From source file:com.allen.mediautil.ImageTakerHelper.java

/**
 * ?/*w w  w  . ja  v a2 s. c  om*/
 *
 * @param path ?
 * @return degree
 */
private static int readPictureDegree(String path) {
    int degree = 0;
    try {
        ExifInterface exifInterface = new ExifInterface(path);
        int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                ExifInterface.ORIENTATION_NORMAL);
        switch (orientation) {
        case ExifInterface.ORIENTATION_ROTATE_90:
            degree = 90;// SUPPRESS CHECKSTYLE
            break;
        case ExifInterface.ORIENTATION_ROTATE_180:
            degree = 180;// SUPPRESS CHECKSTYLE
            break;
        case ExifInterface.ORIENTATION_ROTATE_270:
            degree = 270;// SUPPRESS CHECKSTYLE
            break;
        default:
            break;
        }
    } catch (IOException e) {
        Log.e("readPictureDegree", "readPictureDegree failed", e);
    }
    return degree;
}

From source file:Main.java

/**
 * get image orientation rotate degree/*from www.  j  a  v a2  s . c o  m*/
 *
 * @param filepath
 * @return
 */
public static int getExifOrientation(String filepath) {
    int degree = 0;
    ExifInterface exif = null;
    try {
        exif = new ExifInterface(filepath);
    } catch (IOException ex) {
        Log.d(TAG, "cannot read exif" + ex);
    }
    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:mobisocial.noteshere.util.UriImage.java

public static float rotationForImage(Context context, Uri uri) {
    if (uri.getScheme().equals("content")) {
        String[] projection = { Images.ImageColumns.ORIENTATION };
        Cursor c = context.getContentResolver().query(uri, projection, null, null, null);
        try {/* ww  w. ja v a  2s  . c  o  m*/
            if (c.moveToFirst()) {
                return c.getInt(0);
            }
        } finally {
            c.close();
        }
    } else if (uri.getScheme().equals("file")) {
        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:com.spoiledmilk.ibikecph.util.Util.java

public static Bitmap bmpDecodeFile(File f, int width_limit, int height_limit, long max_size,
        boolean max_dimensions) {
    if (f == null) {
        return null;
    }// ww  w .j  ava2s . c o m

    LOG.d("bmpDecodeFile(" + f.getAbsolutePath() + "," + width_limit + "," + height_limit + "," + max_size + ","
            + max_dimensions + ")");

    Bitmap bmp = null;
    boolean shouldReturn = false;

    FileInputStream fin = null;
    int orientation = ExifInterface.ORIENTATION_NORMAL;
    try {
        // Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;

        fin = new FileInputStream(f);
        BitmapFactory.decodeStream(fin, null, o);
        try {
            fin.close();
            fin = null;
        } catch (IOException e) {
        }

        // Find the correct scale value. It should be the power of 2.
        int scale = 1;
        if (width_limit != -1 && height_limit != -1) {
            if (max_dimensions) {
                while (o.outWidth / scale > width_limit || o.outHeight / scale > height_limit)
                    scale *= 2;
            } else {
                while (o.outWidth / scale / 2 >= width_limit && o.outHeight / scale / 2 >= height_limit)
                    scale *= 2;
            }
        } else if (max_size != -1)
            while ((o.outWidth * o.outHeight) / (scale * scale) > max_size)
                scale *= 2;

        // Decode with inSampleSize
        o = null;
        if (scale > 1) {
            o = new BitmapFactory.Options();
            o.inSampleSize = scale;
        }
        fin = new FileInputStream(f);
        try {
            bmp = BitmapFactory.decodeStream(fin, null, o);
        } catch (OutOfMemoryError e) {
            // Try to recover from out of memory error - but keep in mind
            // that behavior after this error is
            // undefined,
            // for example more out of memory errors might appear in catch
            // block.
            if (bmp != null)
                bmp.recycle();
            bmp = null;
            System.gc();
            LOG.e("Util.bmpDecodeFile() OutOfMemoryError in decodeStream()! Trying to recover...");
        }

        if (bmp != null) {
            LOG.d("resulting bitmap width : " + bmp.getWidth() + " height : " + bmp.getHeight() + " size : "
                    + (bmp.getRowBytes() * bmp.getHeight()));

            ExifInterface exif = new ExifInterface(f.getPath());
            orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
        }
    } catch (FileNotFoundException e) {
        shouldReturn = true;
    } catch (IOException e) {
        shouldReturn = true; // bitmap is still valid here, just can't be
        // rotated
    } finally {
        if (fin != null)
            try {
                fin.close();
            } catch (IOException e) {
            }
    }

    if (shouldReturn || bmp == null)
        return bmp;

    float rotate = 0;
    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;
    }
    if (rotate > 0) {
        Matrix matrix = new Matrix();
        matrix.postRotate(rotate);
        Bitmap bmpRot = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);
        matrix = null;
        bmp.recycle();
        bmp = null;
        // System.gc();
        return bmpRot;
    }

    return bmp;
}