Applies a direct orientation to a bitmap. - Android Graphics

Android examples for Graphics:Bitmap Orientation

Description

Applies a direct orientation to a bitmap.

Demo Code


//package com.java2s;

import android.graphics.Bitmap;

import android.graphics.Matrix;
import android.media.ExifInterface;

public class Main {
    /**//from   w w  w. j a v a2  s.co  m
     * Applies a direct orientation to a bitmap.
     * @param bitmap the bitmap to be re-oriented
     * @param orientation the desired orientation
     * @return the re-oriented bitmap
     */
    public static Bitmap applyOrientation(Bitmap bitmap, int orientation) {
        int rotate = 0;
        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;
        default:
            return bitmap;
        }
        int w = bitmap.getWidth();
        int h = bitmap.getHeight();
        Matrix m = new Matrix();
        m.postRotate(rotate);
        return Bitmap.createBitmap(bitmap, 0, 0, w, h, m, true);
    }
}

Related Tutorials