adjust Photo Rotation - Android Camera

Android examples for Camera:Photo

Description

adjust Photo Rotation

Demo Code


//package com.java2s;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;

public class Main {
    public static Bitmap adjustPhotoRotation(Bitmap inputBitmap,
            int orientationDegree) {
        if (inputBitmap == null) {
            return null;
        }/*from w  ww  . j  a  va2s  .  c o  m*/

        Matrix matrix = new Matrix();
        matrix.setRotate(orientationDegree,
                (float) inputBitmap.getWidth() / 2,
                (float) inputBitmap.getHeight() / 2);
        float outputX, outputY;
        if (orientationDegree == 90) {
            outputX = inputBitmap.getHeight();
            outputY = 0;
        } else {
            outputX = inputBitmap.getHeight();
            outputY = inputBitmap.getWidth();
        }

        final float[] values = new float[9];
        matrix.getValues(values);
        float x1 = values[Matrix.MTRANS_X];
        float y1 = values[Matrix.MTRANS_Y];
        matrix.postTranslate(outputX - x1, outputY - y1);
        Bitmap outputBitmap = Bitmap.createBitmap(inputBitmap.getHeight(),
                inputBitmap.getWidth(), Bitmap.Config.ARGB_8888);
        Paint paint = new Paint();
        Canvas canvas = new Canvas(outputBitmap);
        canvas.drawBitmap(inputBitmap, matrix, paint);
        return outputBitmap;
    }
}

Related Tutorials