fix Bitmap Rotation Exif - Android Graphics

Android examples for Graphics:Bitmap Rotate

Description

fix Bitmap Rotation Exif

Demo Code

/*/*  w w  w.  jav a  2s  . c  o m*/
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
//package com.java2s;
import android.app.Activity;
import android.content.pm.ActivityInfo;

import android.media.ExifInterface;
import android.os.Build;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Surface;
import java.io.IOException;

import java.util.Locale;

public class Main {
    public static void fixBitmapRotationExif(String filePath,
            Activity activityForScreenOrientation) {
        try {
            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");
        }
    }

    public static int getScreenOrientation(Activity activity) {
        int rotation = activity.getWindowManager().getDefaultDisplay()
                .getRotation();
        DisplayMetrics dm = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
        int width = dm.widthPixels;
        int height = dm.heightPixels;
        int orientation;
        // if the device's natural orientation is portrait:
        if ((rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180)
                && height > width
                || (rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270)
                && width > height) {
            switch (rotation) {
            case Surface.ROTATION_0:
                orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
                break;
            case Surface.ROTATION_90:
                orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
                break;
            case Surface.ROTATION_180:
                orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
                break;
            case Surface.ROTATION_270:
                orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
                break;
            default:
                Log.e("getScreenOrientation",
                        "Unknown screen orientation. Defaulting to portrait.");
                orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
                break;
            }
        } else {
            // if the device's natural orientation is landscape or if the device
            // is square:

            switch (rotation) {
            case Surface.ROTATION_0:
                orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
                break;
            case Surface.ROTATION_90:
                orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
                break;
            case Surface.ROTATION_180:
                orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
                break;
            case Surface.ROTATION_270:
                orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
                break;
            default:
                Log.e("getScreenOrientation",
                        "Unknown screen orientation. Defaulting to landscape.");
                orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
                break;
            }
        }

        return orientation;
    }
}

Related Tutorials