Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 * 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.
 */

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;

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 Bitmap getBitmapByFixingRotationForFile(String filePath, Bitmap sourceBitmap,
            Activity activityForScreenOrientation, boolean freeSourceBitmap) {
        try {
            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;
    }

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