Example usage for android.graphics Matrix setRotate

List of usage examples for android.graphics Matrix setRotate

Introduction

In this page you can find the example usage for android.graphics Matrix setRotate.

Prototype

public void setRotate(float degrees, float px, float py) 

Source Link

Document

Set the matrix to rotate by the specified number of degrees, with a pivot point at (px, py).

Usage

From source file:Main.java

public static Bitmap rotateBitmap(Bitmap b, int degrees) {
    if (degrees != 0 && b != null && !b.isRecycled()) {
        Matrix m = new Matrix();
        m.setRotate(degrees, (float) b.getWidth() / 2, (float) b.getHeight() / 2);
        try {/*from  w ww .j a  v a2  s  .c o m*/
            Bitmap b2 = Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), m, true);
            if (b != b2) {
                b.recycle();
            }
            b = b2;
        } catch (OutOfMemoryError ex) {
        }
    }
    return b;
}

From source file:Main.java

public static Bitmap adjustPhotoRotation(Bitmap bm, final int orientationDegree) {

    Matrix m = new Matrix();
    m.setRotate(orientationDegree, (float) bm.getWidth() / 2, (float) bm.getHeight() / 2);
    float targetX, targetY;
    if (orientationDegree == 90) {
        targetX = bm.getHeight();/* ww w . j  a v a 2  s .c  o m*/
        targetY = 0;
    } else {
        targetX = bm.getHeight();
        targetY = bm.getWidth();
    }

    final float[] values = new float[9];
    m.getValues(values);

    float x1 = values[Matrix.MTRANS_X];
    float y1 = values[Matrix.MTRANS_Y];

    m.postTranslate(targetX - x1, targetY - y1);

    Bitmap temp = Bitmap.createBitmap(bm.getHeight(), bm.getWidth(), Bitmap.Config.ARGB_8888);

    Paint paint = new Paint();
    Canvas canvas = new Canvas(temp);
    canvas.drawBitmap(bm, m, paint);

    return temp;
}

From source file:Main.java

public static Bitmap rotateAndMirror(Bitmap bitmap, int degree, boolean isMirror) throws OutOfMemoryError {

    if ((degree != 0 || isMirror) && bitmap != null) {
        Matrix m = new Matrix();
        m.setRotate(degree, (float) bitmap.getWidth() / 2, (float) bitmap.getHeight() / 2);

        if (isMirror) {
            m.postScale(-1, 1);/*  ww w .  j  ava 2 s .  c  o m*/
            degree = (degree + 360) % 360;
            if (degree == 0 || degree == 180) {
                m.postTranslate((float) bitmap.getWidth(), 0);
            } else if (degree == 90 || degree == 270) {
                m.postTranslate((float) bitmap.getHeight(), 0);
            } else {
                throw new IllegalArgumentException("Invalid degrees=" + degree);
            }
        }

        Bitmap bitmap2 = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), m, true);
        if (bitmap != bitmap2) {
            bitmap.recycle();
            System.gc();
            bitmap = bitmap2;
        }
    }
    return bitmap;
}

From source file:Main.java

public static Bitmap adjustPhotoRotation(Bitmap bm, final int orientationDegree) {
    Matrix m = new Matrix();
    m.setRotate(orientationDegree, (float) bm.getWidth() / 2, (float) bm.getHeight() / 2);
    float targetX, targetY;
    if (orientationDegree == 90) {
        targetX = bm.getHeight();/* w  w  w . j  ava  2 s  .  c o  m*/
        targetY = 0;
    } else {
        targetX = bm.getHeight();
        targetY = bm.getWidth();
    }
    final float[] values = new float[9];
    m.getValues(values);
    float x1 = values[Matrix.MTRANS_X];
    float y1 = values[Matrix.MTRANS_Y];
    m.postTranslate(targetX - x1, targetY - y1);
    Bitmap bm1 = Bitmap.createBitmap(bm.getHeight(), bm.getWidth(), Bitmap.Config.ARGB_8888);
    Paint paint = new Paint();
    Canvas canvas = new Canvas(bm1);
    canvas.drawBitmap(bm, m, paint);
    bm.recycle();
    bm = null;
    return bm1;
}

From source file:Main.java

public static Bitmap getRotatedBitmap(Bitmap bitmap, int degrees) {
    if (degrees != 0 && bitmap != null) {
        Matrix m = new Matrix();
        m.setRotate(degrees, (float) bitmap.getWidth() / 2, (float) bitmap.getHeight() / 2);
        try {//from   w ww .j  av a2  s.c om
            Bitmap b2 = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), m, true);
            if (bitmap != b2) {
                bitmap.recycle();
                bitmap = b2;
            }
        } catch (OutOfMemoryError ex) {
            // TODO Auto-generated catch block
            ex.printStackTrace();
        }
    }
    return bitmap;
}

From source file:Main.java

public static Bitmap getRotatedBitmap(Bitmap bitmap, int degrees) {
    if (degrees != 0 && bitmap != null) {
        Matrix m = new Matrix();
        m.setRotate(degrees, (float) bitmap.getWidth() / 2, (float) bitmap.getHeight() / 2);
        try {//from   w w  w  . j a v a  2 s  .c  o m
            Bitmap b2 = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), m, true);
            if (bitmap.equals(b2)) {
                bitmap.recycle();
                bitmap = b2;
            }
        } catch (OutOfMemoryError ex) {
            // TODO Auto-generated catch block
            ex.printStackTrace();
        }
    }
    return bitmap;
}

From source file:Main.java

public static Bitmap rotateBitmapFromExif(String filePath, Bitmap bitmap) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;/* w w w . j  a va2s.c o  m*/
    BitmapFactory.decodeFile(filePath, options);

    String orientString;
    try {
        ExifInterface exif = new ExifInterface(filePath);
        orientString = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
    } catch (IOException e) {
        e.printStackTrace();
        return bitmap;
    }

    int orientation = (orientString != null) ? Integer.parseInt(orientString)
            : ExifInterface.ORIENTATION_NORMAL;
    int rotationAngle = 0, outHeight = 0, outWidth = 0;
    switch (orientation) {
    case ExifInterface.ORIENTATION_ROTATE_90:
        rotationAngle = 90;
        outHeight = bitmap.getWidth();
        outWidth = bitmap.getHeight();
        break;
    case ExifInterface.ORIENTATION_ROTATE_180:
        rotationAngle = 180;
        outHeight = bitmap.getHeight();
        outWidth = bitmap.getWidth();
        break;
    case ExifInterface.ORIENTATION_ROTATE_270:
        rotationAngle = 270;
        outHeight = bitmap.getWidth();
        outWidth = bitmap.getHeight();
        break;
    }

    if (rotationAngle == 0) {
        return bitmap;
    }

    Matrix matrix = new Matrix();
    matrix.setRotate(rotationAngle, (float) bitmap.getWidth() / 2, (float) bitmap.getHeight() / 2);
    Bitmap rotateBitmap = Bitmap.createBitmap(bitmap, 0, 0, outWidth, outHeight, matrix, true);
    if (bitmap != rotateBitmap) {
        bitmap.recycle();
    }

    return rotateBitmap;
}

From source file:Main.java

public static boolean setImageToScreenMatrix(Matrix dst, RectF image, RectF screen, int rotation) {
    RectF rotatedImage = new RectF();
    dst.setRotate(rotation, image.centerX(), image.centerY());
    if (!dst.mapRect(rotatedImage, image)) {
        return false; // fails for rotations that are not multiples of 90
                      // degrees
    }/* w w w .  j a  v a2  s  . co  m*/
    boolean rToR = dst.setRectToRect(rotatedImage, screen, Matrix.ScaleToFit.CENTER);
    boolean rot = dst.preRotate(rotation, image.centerX(), image.centerY());
    return rToR && rot;
}

From source file:Main.java

public static Bitmap createScaledRotatedBitmapFromFile(String filePath, int reqWidth, int reqHeight,
        int orientation) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;/* w w w  . ja v a2  s  .  c om*/
    BitmapFactory.decodeFile(filePath, options);

    Bitmap bitmap;
    int rotationAngle = 0, outHeight = 0, outWidth = 0;
    if (reqWidth >= reqHeight) {
        if (options.outWidth >= options.outHeight) {
            if (orientation != 1) {
                rotationAngle = 180;
            } else {

            }
            bitmap = decodeSampledBitmapFromFile(filePath, reqWidth, reqHeight);
        } else {
            bitmap = decodeSampledBitmapFromFile(filePath, reqHeight, reqWidth);
        }
        outHeight = bitmap.getWidth();
        outWidth = bitmap.getHeight();
    } else {
        if (options.outWidth > options.outHeight) {
            if (orientation != 1) {
                rotationAngle = 90;
            } else {
                rotationAngle = 270;
            }
            bitmap = decodeSampledBitmapFromFile(filePath, reqHeight, reqWidth);
        } else {
            bitmap = decodeSampledBitmapFromFile(filePath, reqWidth, reqHeight);
        }
        outHeight = bitmap.getWidth();
        outWidth = bitmap.getHeight();
    }

    if (rotationAngle == 0) {
        return bitmap;
    }

    Matrix matrix = new Matrix();
    matrix.setRotate(rotationAngle, (float) bitmap.getWidth() / 2, (float) bitmap.getHeight() / 2);
    Bitmap rotateBitmap = Bitmap.createBitmap(bitmap, 0, 0, outHeight, outWidth, matrix, true);
    if (bitmap != rotateBitmap) {
        bitmap.recycle();
    }

    return rotateBitmap;
}

From source file:Main.java

public static Bitmap rotate(Bitmap source, float degrees) {
    if (source == null || source.getWidth() == 0 || source.getHeight() == 0)
        return null;
    int width = source.getWidth();
    int height = source.getHeight();
    Matrix m = new Matrix();
    float w = width;
    float h = height;
    float px = w / 2;
    float py = h / 2;
    m.setRotate(degrees, px, py);
    return Bitmap.createBitmap(source, 0, 0, width, height, m, true);
}