Example usage for android.graphics Matrix postTranslate

List of usage examples for android.graphics Matrix postTranslate

Introduction

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

Prototype

public boolean postTranslate(float dx, float dy) 

Source Link

Document

Postconcats the matrix with the specified translation.

Usage

From source file:Main.java

public static Bitmap rotateWithCanvas(Bitmap bitmap, int degrees) {

    int destWidth, destHeight;

    float centerX = bitmap.getWidth() / 2;
    float centerY = bitmap.getHeight() / 2;

    // We want to do the rotation at origin, but since the bounding
    // rectangle will be changed after rotation, so the delta values
    // are based on old & new width/height respectively.
    Matrix matrix = new Matrix();
    matrix.preTranslate(-centerX, -centerY);
    matrix.postRotate(degrees);//from  w  ww  . ja  v a 2s  .  c  om
    if (degrees / 90 % 2 == 0) {
        destWidth = bitmap.getWidth();
        destHeight = bitmap.getHeight();
        matrix.postTranslate(centerX, centerY);
    } else {
        destWidth = bitmap.getHeight();
        destHeight = bitmap.getWidth();
        matrix.postTranslate(centerY, centerX);
    }
    Bitmap cropped = Bitmap.createBitmap(destWidth, destHeight, Bitmap.Config.RGB_565);
    Canvas canvas = new Canvas(cropped);
    canvas.drawBitmap(bitmap, matrix, null);
    return cropped;
}

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

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);//from  w  w  w.j  av  a  2  s .co 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 rotateAndMirror(Bitmap b, int degrees, boolean mirror) {
    if ((degrees != 0 || mirror) && b != null) {
        Matrix m = new Matrix();
        // Mirror first.
        // horizontal flip + rotation = -rotation + horizontal flip
        if (mirror) {
            m.postScale(-1, 1);/*from  w  w w.j a  va 2s.  c o m*/
            degrees = (degrees + 360) % 360;
            if (degrees == 0 || degrees == 180) {
                m.postTranslate((float) b.getWidth(), 0);
            } else if (degrees == 90 || degrees == 270) {
                m.postTranslate((float) b.getHeight(), 0);
            } else {
                throw new IllegalArgumentException("Invalid degrees=" + degrees);
            }
        }
        if (degrees != 0) {
            // clockwise
            m.postRotate(degrees, (float) b.getWidth() / 2, (float) b.getHeight() / 2);
        }

        try {
            Bitmap b2 = Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), m, true);
            if (b != b2) {
                b.recycle();
                b = b2;
            }
        } catch (OutOfMemoryError ex) {
            // We have no memory to rotate. Return the original bitmap.
        }
    }
    return b;
}

From source file:Main.java

public static Drawable rotateBitmap(Bitmap inputBitmap) {
    Matrix matrix = new Matrix();
    matrix.setRotate(90, (float) inputBitmap.getWidth() / 2, (float) inputBitmap.getHeight() / 2);

    float outputX = inputBitmap.getHeight();
    float outputY = 0;

    final float[] values = new float[9];
    matrix.getValues(values);/*www. j  av a2 s.  c  o m*/
    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 new BitmapDrawable(null, outputBitmap);
}

From source file:Main.java

/**
 * A potentially expensive operation to crop the given Bitmap so that it fills the given dimensions. This operation
 * is significantly less expensive in terms of memory if a mutable Bitmap with the given dimensions is passed in
 * as well./*w w w.j  a v  a 2s.com*/
 *
 * @param recycled A mutable Bitmap with dimensions width and height that we can load the cropped portion of toCrop
 *                 into
 * @param toCrop The Bitmap to resize
 * @param width The width of the final Bitmap
 * @param height The height of the final Bitmap
 * @return The resized Bitmap (will be recycled if recycled is not null)
 */
public static Bitmap centerCrop(Bitmap recycled, Bitmap toCrop, int width, int height) {
    if (toCrop == null) {
        return null;
    } else if (toCrop.getWidth() == width && toCrop.getHeight() == height) {
        return toCrop;
    }
    //from ImageView/Bitmap.createScaledBitmap
    //https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/widget/ImageView.java
    //https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/graphics/java/android/graphics/Bitmap.java
    final float scale;
    float dx = 0, dy = 0;
    Matrix m = new Matrix();
    if (toCrop.getWidth() * height > width * toCrop.getHeight()) {
        scale = (float) height / (float) toCrop.getHeight();
        dx = (width - toCrop.getWidth() * scale) * 0.5f;
    } else {
        scale = (float) width / (float) toCrop.getWidth();
        dy = (height - toCrop.getHeight() * scale) * 0.5f;
    }

    m.setScale(scale, scale);
    m.postTranslate((int) dx + 0.5f, (int) dy + 0.5f);
    final Bitmap result;
    if (recycled != null) {
        result = recycled;
    } else {
        result = Bitmap.createBitmap(width, height,
                toCrop.getConfig() == null ? Bitmap.Config.ARGB_8888 : toCrop.getConfig());
    }
    Canvas canvas = new Canvas(result);
    Paint paint = new Paint(PAINT_FLAGS);
    canvas.drawBitmap(toCrop, m, paint);
    return result;
}

From source file:Main.java

public static Bitmap rotateBitmapTranslate(Bitmap bitmap, float degrees) {
    Bitmap mBitmap = null;//  ww  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:Main.java

/** create a coordination convert matrix
 * <br>/*from  w  ww  .j  a v  a 2 s  .  co m*/
 * 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

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

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 ww.j av 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 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);

    return bm1;
}