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

private static int getOrientationFromExif(Uri imageUri, Context context) {
    int orientation = -1;
    Log.i("Photo Editor", "imageUri = " + imageUri);
    // File imageFile = new File(getRealPathFromUri(imageUri, context));
    File imageFile = new File(imageUri.getPath());
    try {/* www. j  a  v  a  2  s. co m*/
        ExifInterface exif;
        Log.i("Photo Editor", "imageFile.getAbsolutePath() = " + imageFile.getAbsolutePath());
        exif = new ExifInterface(imageFile.getAbsolutePath());
        orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return orientation;
}

From source file:Main.java

public static Bitmap rotateByExifInfo(Bitmap source, String path) {
    try {/* w w  w  . j  a v  a2s . c  o m*/
        ExifInterface exifInterface = new ExifInterface(path);
        String tagName = ExifInterface.TAG_ORIENTATION;
        int defaultValue = ExifInterface.ORIENTATION_NORMAL;
        int orientation = exifInterface.getAttributeInt(tagName, defaultValue);
        switch (orientation) {
        case ExifInterface.ORIENTATION_NORMAL:
            return source;
        case ExifInterface.ORIENTATION_ROTATE_90:
            return rotate(source, 90);
        case ExifInterface.ORIENTATION_ROTATE_180:
            return rotate(source, 180);
        case ExifInterface.ORIENTATION_ROTATE_270:
            return rotate(source, 270f);
        default:
            return source;
        }
    } catch (Exception e) {
        return source;
    }
}

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 va 2s .  c o  m*/
                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 downSampleBitmap(Uri uri, Activity act, Boolean needRotate) {
    DisplayMetrics displaymetrics = new DisplayMetrics();
    act.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
    Resources r = act.getResources();
    int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, r.getDisplayMetrics()); // 50: magic num
    int targetWidth = displaymetrics.heightPixels;
    int targetHeight = displaymetrics.widthPixels - px;

    Bitmap resizedBitmap = decodeSampledBitmap(uri, targetWidth, targetHeight, act);
    Bitmap returnBitmap = null;/*w  w  w  .  ja v  a 2  s  . c o m*/
    ExifInterface exif;
    try {
        float degree = 0;
        exif = new ExifInterface(uri.toString());
        int orient = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
        if (resizedBitmap != null && needRotate) {
            degree = getDegree(orient);
            if (degree != 0) {
                returnBitmap = createRotatedBitmap(resizedBitmap, degree);
            }
            returnBitmap = returnBitmap == null ? resizedBitmap : returnBitmap;
        }
    } catch (IOException e) {
        Log.v(TAG, "not found file at downsample");
        e.printStackTrace();
    }
    return returnBitmap;
}

From source file:Main.java

public static int getRotationAngle(String photoPath) {
    ExifInterface ei = null;
    try {//  w w  w.ja v a 2 s.c  om
        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

public static int getExifOrientation(String filepath) {
    int degree = 0;
    ExifInterface exif = null;
    try {//  w  ww . j av  a 2  s  .  co m
        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 getBitmapByFixingRotationForFile(String filePath, Bitmap sourceBitmap,
        Activity activityForScreenOrientation, boolean freeSourceBitmap) {
    try {//from w  w w. j  av  a 2 s  . c  om
        ExifInterface exif = new ExifInterface(filePath);
        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

        if (orientation == ExifInterface.ORIENTATION_UNDEFINED
                && Build.MANUFACTURER.toLowerCase(Locale.ENGLISH).contains("htc"))
            return null;

        boolean flippedHorizontally = false, flippedVertically = false;

        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;
        } else if (orientation == ExifInterface.ORIENTATION_FLIP_HORIZONTAL) {
            flippedHorizontally = true;
        } else if (orientation == ExifInterface.ORIENTATION_FLIP_VERTICAL) {
            flippedVertically = true;
        } else if (orientation == ExifInterface.ORIENTATION_TRANSPOSE) {
            angle += 90;
            flippedVertically = true;
        } else if (orientation == ExifInterface.ORIENTATION_TRANSVERSE) {
            angle -= 90;
            flippedVertically = true;
        }

        if (activityForScreenOrientation != null) {
            orientation = getScreenOrientation(activityForScreenOrientation);
            if (orientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
                angle += 90;
            } else if (orientation == ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE) {
                angle += 180;
            } else if (orientation == ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT) {
                angle += 270;
            }
        }

        Bitmap bmp = sourceBitmap;
        if (bmp == null) {
            bmp = BitmapFactory.decodeFile(filePath, null);
        }
        if (angle != 0) {
            Matrix mat = new Matrix();
            mat.postRotate(angle);

            if (flippedHorizontally) {
                mat.postScale(-1.f, 1.f);
            }
            if (flippedVertically) {
                mat.postScale(1.f, -1.f);
            }

            Bitmap rotated = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), mat, true);
            if (freeSourceBitmap || bmp != sourceBitmap) {
                bmp.recycle();
            }
            bmp = rotated;
        }

        return bmp;

    } catch (IOException e) {
        Log.w("TAG", "-- Error in setting image");
    } catch (OutOfMemoryError oom) {
        Log.w("TAG", "-- OOM Error in setting image");
    }

    return null;
}

From source file:Main.java

public synchronized static int getPhotoOrientationDegree(String filepath) {
    int degree = 0;
    ExifInterface exif = null;

    try {//  w w  w  . j a  va  2  s .  c om
        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

public static void fixBitmapRotationExif(String filePath, Activity activityForScreenOrientation) {
    try {//from   w w  w .  ja va  2  s  .c om
        ExifInterface exif = new ExifInterface(filePath);

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

        if (exifOrientation == ExifInterface.ORIENTATION_UNDEFINED
                && Build.MANUFACTURER.toLowerCase(Locale.ENGLISH).contains("htc"))
            return;

        boolean flippedHorizontally = false, flippedVertically = false;

        int angle = 0;

        if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_90) {
            angle += 90;
        } else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_180) {
            angle += 180;
        } else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_270) {
            angle += 270;
        } else if (exifOrientation == ExifInterface.ORIENTATION_FLIP_HORIZONTAL) {
            flippedHorizontally = true;
        } else if (exifOrientation == ExifInterface.ORIENTATION_FLIP_VERTICAL) {
            flippedVertically = true;
        } else if (exifOrientation == ExifInterface.ORIENTATION_TRANSPOSE) {
            angle += 90;
            flippedVertically = true;
        } else if (exifOrientation == ExifInterface.ORIENTATION_TRANSVERSE) {
            angle -= 90;
            flippedVertically = true;
        }

        int orientation;

        if (activityForScreenOrientation != null) {
            orientation = getScreenOrientation(activityForScreenOrientation);
            if (orientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
                angle += 90;
            } else if (orientation == ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE) {
                angle += 180;
            } else if (orientation == ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT) {
                angle += 270;
            }
        }

        orientation = 0;
        angle = angle % 360;

        if (angle == -90 && flippedVertically && !flippedHorizontally) {
            orientation = ExifInterface.ORIENTATION_TRANSVERSE;
        } else if (angle == -270 && flippedVertically && !flippedHorizontally) {
            orientation = ExifInterface.ORIENTATION_TRANSPOSE;
        } else if (angle == -90 && !flippedVertically && flippedHorizontally) {
            orientation = ExifInterface.ORIENTATION_TRANSPOSE;
        } else if (angle == -270 && !flippedVertically && flippedHorizontally) {
            orientation = ExifInterface.ORIENTATION_TRANSVERSE;
        } else {
            while (angle < 0) {
                angle += 360;
            }
            switch (angle) {
            case 0:
                if (flippedHorizontally) {
                    orientation = ExifInterface.ORIENTATION_FLIP_HORIZONTAL;
                } else if (flippedVertically) {
                    orientation = ExifInterface.ORIENTATION_FLIP_VERTICAL;
                }
                break;
            case 90:
                orientation = ExifInterface.ORIENTATION_ROTATE_90;
                break;
            case 180:
                orientation = ExifInterface.ORIENTATION_ROTATE_180;
                break;
            case 270:
                orientation = ExifInterface.ORIENTATION_ROTATE_270;
                break;
            }
        }

        if (orientation != exifOrientation) {
            exif.setAttribute(ExifInterface.TAG_ORIENTATION, ((Integer) orientation).toString());
            exif.saveAttributes();
        }
    } catch (IOException e) {
        Log.w("TAG", "-- Error in setting image");
    }
}

From source file:Main.java

private static Bitmap turnPic(final String path, Bitmap bitmap) {

    ExifInterface exif;
    final int ninetyDegrees = 90;
    int rotationAngle = 0;
    Matrix matrix = new Matrix();

    if (path != null) {
        try {/*from ww  w  .  ja v a  2 s .co  m*/
            exif = new ExifInterface(path);
            int exifOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                    ExifInterface.ORIENTATION_NORMAL);
            switch (exifOrientation) {
            case ExifInterface.ORIENTATION_ROTATE_90:
                rotationAngle = ninetyDegrees;
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                rotationAngle = ninetyDegrees * 2;
                break;
            case ExifInterface.ORIENTATION_ROTATE_270:
                rotationAngle = ninetyDegrees * 3;
                break;
            case ExifInterface.ORIENTATION_NORMAL:
            default:
                break;
            }
        } catch (IOException e) {
            e.printStackTrace();
            return bitmap;
        }
        if (rotationAngle != 0) {
            matrix.postRotate(rotationAngle);
        }
        bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    }
    return bitmap;
}