Example usage for android.graphics Matrix postRotate

List of usage examples for android.graphics Matrix postRotate

Introduction

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

Prototype

public boolean postRotate(float degrees) 

Source Link

Document

Postconcats the matrix with the specified rotation.

Usage

From source file:Main.java

public static void prepareJpgMatrix(Matrix matrix, boolean mirror, int displayOrientation, int viewWidth,
        int viewHeight) {
    // Need mirror for front camera.
    matrix.setScale(mirror ? -1 : 1, 1);
    // This is the value for android.hardware.Camera.setDisplayOrientation.
    matrix.postRotate(displayOrientation);
    matrix.postTranslate(viewWidth / 2f, viewHeight / 2f);
}

From source file:Main.java

/** create a coordination convert matrix
 * <br>//from   ww  w  . j  av a  2s  .  c  om
 * See also {@link android.hardware.Camera.Face#rect}
 * */
private static Matrix createConvertMatrix(boolean frontCamera, float displayOrientation, float viewWidth,
        float viewHeight) {
    Matrix matrix = new Matrix();
    // Need mirror for front camera.
    boolean mirror = frontCamera;
    matrix.setScale(mirror ? -1 : 1, 1);
    // This is the value for android.hardware.Camera.setDisplayOrientation.
    matrix.postRotate(displayOrientation);
    // Camera driver coordinates range from (-1000, -1000) to (1000, 1000).
    // UI coordinates range from (0, 0) to (width, height).
    matrix.postScale(viewWidth / 2000f, viewHeight / 2000f);
    matrix.postTranslate(viewWidth / 2f, viewHeight / 2f);
    return matrix;
}

From source file:Main.java

/**
 * Scale a bitmap and correct the dimensions
 *
 * @param bitmap      Bitmap to scale/*  w ww  .  j a v a2s .  c o  m*/
 * @param width       width for scaling
 * @param height      height for scaling
 * @param orientation Current orientation of the Image
 * @return Scaled bitmap
 */
public static Bitmap getScaledBitmap(Bitmap bitmap, int width, int height, int orientation) {
    Matrix m = new Matrix();
    m.setRectToRect(new RectF(0, 0, bitmap.getWidth(), bitmap.getHeight()), new RectF(0, 0, width, height),
            Matrix.ScaleToFit.CENTER);
    if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
        m.postRotate(ORIENTATION_90);
    } else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
        m.postRotate(ORIENTATION_180);
    } else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
        m.postRotate(ORIENTATION_270);
    }
    return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), m, true);
}

From source file:Main.java

public static void prepareMatrix(Matrix matrix, boolean mirror, int displayOrientation, int viewWidth,
        int viewHeight) {
    // Need mirror for front camera.
    matrix.setScale(mirror ? -1 : 1, 1);
    // This is the value for android.hardware.Camera.setDisplayOrientation.
    matrix.postRotate(displayOrientation);
    // Camera driver coordinates range from (-1000, -1000) to (1000, 1000).
    // UI coordinates range from (0, 0) to (width, height).
    matrix.postScale(viewWidth / 2000f, viewHeight / 2000f);
    matrix.postTranslate(viewWidth / 2f, viewHeight / 2f);
}

From source file:Main.java

/**
 * Returns a transformation matrix from one reference frame into another.
 * Handles cropping (if maintaining aspect ratio is desired) and rotation.
 *
 * @param srcWidth Width of source frame.
 * @param srcHeight Height of source frame.
 * @param dstWidth Width of destination frame.
 * @param dstHeight Height of destination frame.
 * @param applyRotation Amount of rotation to apply from one frame to another.
 *  Must be a multiple of 90./*  ww  w  . ja  va2  s  .  c om*/
 * @param maintainAspectRatio If true, will ensure that scaling in x and y remains constant,
 * cropping the image if necessary.
 * @return The transformation fulfilling the desired requirements.
 */
public static Matrix getTransformationMatrix(final int srcWidth, final int srcHeight, final int dstWidth,
        final int dstHeight, final int applyRotation, final boolean maintainAspectRatio) {
    final Matrix matrix = new Matrix();

    if (applyRotation != 0) {
        // Translate so center of image is at origin.
        matrix.postTranslate(-srcWidth / 2.0f, -srcHeight / 2.0f);

        // Rotate around origin.
        matrix.postRotate(applyRotation);
    }

    // Account for the already applied rotation, if any, and then determine how
    // much scaling is needed for each axis.
    final boolean transpose = (Math.abs(applyRotation) + 90) % 180 == 0;

    final int inWidth = transpose ? srcHeight : srcWidth;
    final int inHeight = transpose ? srcWidth : srcHeight;

    // Apply scaling if necessary.
    if (inWidth != dstWidth || inHeight != dstHeight) {
        final float scaleFactorX = dstWidth / (float) inWidth;
        final float scaleFactorY = dstHeight / (float) inHeight;

        if (maintainAspectRatio) {
            // Scale by minimum factor so that dst is filled completely while
            // maintaining the aspect ratio. Some image may fall off the edge.
            final float scaleFactor = Math.max(scaleFactorX, scaleFactorY);
            matrix.postScale(scaleFactor, scaleFactor);
        } else {
            // Scale exactly to fill dst from src.
            matrix.postScale(scaleFactorX, scaleFactorY);
        }
    }

    if (applyRotation != 0) {
        // Translate back from origin centered reference to destination frame.
        matrix.postTranslate(dstWidth / 2.0f, dstHeight / 2.0f);
    }

    return matrix;
}

From source file:com.allen.mediautil.ImageTakerHelper.java

/**
 * //from w  ww  .j  ava  2 s. co  m
 *
 * @param angle  angle
 * @param bitmap bitmap
 * @return Bitmap
 */
private static Bitmap rotateImageView(int angle, Bitmap bitmap) {
    Matrix matrix = new Matrix();
    matrix.postRotate(angle);
    return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
}

From source file:Main.java

public static Bitmap rotateImage(Bitmap bitmap, String storagePath) {
    Bitmap resultBitmap = bitmap;//from w  ww .  j  a v a  2 s. c om
    try {
        ExifInterface exifInterface = new ExifInterface(storagePath);
        int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);

        Matrix matrix = new Matrix();

        // 1: nothing to do

        // 2
        if (orientation == ExifInterface.ORIENTATION_FLIP_HORIZONTAL) {
            matrix.postScale(-1.0f, 1.0f);
        }
        // 3
        else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
            matrix.postRotate(180);
        }
        // 4
        else if (orientation == ExifInterface.ORIENTATION_FLIP_VERTICAL) {
            matrix.postScale(1.0f, -1.0f);
        }
        // 5
        else if (orientation == ExifInterface.ORIENTATION_TRANSPOSE) {
            matrix.postRotate(-90);
            matrix.postScale(1.0f, -1.0f);
        }
        // 6
        else if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
            matrix.postRotate(90);
        }
        // 7
        else if (orientation == ExifInterface.ORIENTATION_TRANSVERSE) {
            matrix.postRotate(90);
            matrix.postScale(1.0f, -1.0f);
        }
        // 8
        else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
            matrix.postRotate(270);
        }

        // Rotate the bitmap
        resultBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
        if (resultBitmap != bitmap) {
            bitmap.recycle();
        }
    } catch (Exception exception) {
        Log.e("BitmapUtil", "Could not rotate the image: " + storagePath);
    }
    return resultBitmap;
}

From source file:Main.java

public static Bitmap rotateBitmapTranslate(Bitmap bitmap, float degrees) {
    Bitmap mBitmap = null;// w w w . j ava 2  s .  c o  m
    int width;
    int height;
    try {
        Matrix matrix = new Matrix();
        if ((degrees / 90) % 2 != 0) {
            width = bitmap.getWidth();
            height = bitmap.getHeight();
        } else {
            width = bitmap.getHeight();
            height = bitmap.getWidth();
        }
        int cx = width / 2;
        int cy = height / 2;
        matrix.preTranslate(-cx, -cy);
        matrix.postRotate(degrees);
        matrix.postTranslate(cx, cy);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return mBitmap;
}

From source file:com.example.util.ImageUtils.java

/**
 * /* w w  w  .  j ava  2s.  c  o m*/
 */
public static Bitmap rotateImage(Bitmap bmp) {

    if (bmp == null)
        return bmp;

    int width = bmp.getWidth();
    int height = bmp.getHeight();
    float aspectRatio = ((float) height) / width;

    if (aspectRatio > 1) {
        // no need to rotate the image
        return bmp;
    }

    Matrix matrix = new Matrix();
    matrix.postRotate(90);
    Bitmap rotatedBitmap = null;
    try {
        rotatedBitmap = Bitmap.createBitmap(bmp, 0, 0, width, height, matrix, false);
    } catch (OutOfMemoryError e) {
    }
    return rotatedBitmap;
}

From source file:com.tafayor.selfcamerashot.taflib.helpers.GraphicsHelper.java

public static Bitmap rotateBitmap(Bitmap bmp, int angle) {
    Matrix matrix = new Matrix();
    matrix.postRotate(angle);
    int w = bmp.getWidth();
    int h = bmp.getHeight();
    if (angle % 90 == 0 && angle % 180 != 0) {
        w = bmp.getHeight();/*  w  w w  .  ja v a  2  s  . c o m*/
        h = bmp.getWidth();
    }
    Bitmap rotatedBitmap = Bitmap.createBitmap(bmp, 0, 0, w, h, matrix, false);

    return rotatedBitmap;
}