Example usage for android.graphics Matrix Matrix

List of usage examples for android.graphics Matrix Matrix

Introduction

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

Prototype

public Matrix() 

Source Link

Document

Create an identity matrix

Usage

From source file:Main.java

/**
 * Rotates a bitmap by a certain angle/*from   ww w  . ja  va 2 s . c  o  m*/
 *
 * @param pSourceBitmap
 *        The bitmap that needs to be rotated
 * @param pAngle
 *        The angle the bitmap should be rotated to
 * @return A rotated bitmap
 */
private static Bitmap rotateImage(Bitmap pSourceBitmap, float pAngle) {
    Bitmap bitmap = null;
    Matrix matrix = new Matrix();
    matrix.postRotate(pAngle);
    try {
        bitmap = Bitmap.createBitmap(pSourceBitmap, 0, 0, pSourceBitmap.getWidth(), pSourceBitmap.getHeight(),
                matrix, true);
    } catch (OutOfMemoryError err) {
        err.printStackTrace();
    }
    return bitmap;
}

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./*from  www. j  ava 2 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:Main.java

/**
 * Crop image bitmap from given bitmap using the given points in the original bitmap and the given rotation.<br>
 * if the rotation is not 0,90,180 or 270 degrees then we must first crop a larger area of the image that
 * contains the requires rectangle, rotate and then crop again a sub rectangle.
 *
 * @param scale how much to scale the cropped image part, use 0.5 to lower the image by half (OOM handling)
 *//*from w w  w. j av a  2 s.co  m*/
private static Bitmap cropBitmapObjectWithScale(Bitmap bitmap, float[] points, int degreesRotated,
        boolean fixAspectRatio, int aspectRatioX, int aspectRatioY, float scale, boolean flipHorizontally,
        boolean flipVertically) {

    // get the rectangle in original image that contains the required cropped area (larger for non rectangular crop)
    Rect rect = getRectFromPoints(points, bitmap.getWidth(), bitmap.getHeight(), fixAspectRatio, aspectRatioX,
            aspectRatioY);

    if (degreesRotated == 90 || degreesRotated == 270) {
        if (flipHorizontally != flipVertically) {
            boolean temp = flipHorizontally;
            flipHorizontally = flipVertically;
            flipVertically = temp;
        }
    }

    // crop and rotate the cropped image in one operation
    float scaleX = flipHorizontally ? -scale : scale;
    float scaleY = flipVertically ? -scale : scale;

    Matrix matrix = new Matrix();
    matrix.setScale(scaleX, scaleY);
    matrix.postRotate(degreesRotated, bitmap.getWidth() / 2, bitmap.getHeight() / 2);
    Bitmap result = Bitmap.createBitmap(bitmap, rect.left, rect.top, rect.width(), rect.height(), matrix, true);

    if (result == bitmap) {
        // corner case when all bitmap is selected, no worth optimizing for it
        result = bitmap.copy(bitmap.getConfig(), false);
    }

    // rotating by 0, 90, 180 or 270 degrees doesn't require extra cropping
    if (degreesRotated % 90 != 0) {

        // extra crop because non rectangular crop cannot be done directly on the image without rotating first
        result = cropForRotatedImage(result, points, rect, degreesRotated, fixAspectRatio, aspectRatioX,
                aspectRatioY);
    }

    return result;
}

From source file:Main.java

public static Bitmap getScaleImage(Bitmap bitmap, float boundBoxInDp) {

    // Get current dimensions
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();

    // Determine how much to scale: the dimension requiring
    // less scaling is.
    // closer to the its side. This way the image always
    // stays inside your.
    // bounding box AND either x/y axis touches it.
    float xScale = boundBoxInDp / width;
    float yScale = boundBoxInDp / height;
    float scale = xScale <= yScale ? xScale : yScale;

    // Create a matrix for the scaling and add the scaling data
    Matrix matrix = new Matrix();
    matrix.postScale(scale, scale);// ww w. java2 s . c om
    // matrix.postRotate(rotate);

    // Create a new bitmap and convert it to a format understood

    // by the
    // ImageView
    Bitmap scaledBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, false);

    // Apply the scaled bitmap
    return scaledBitmap;

}

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 .  ja  va 2 s. c o  m*/
            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  ww  w.  j  a va 2  s . co  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 getScaleImage(final Bitmap bitmap, final float boundBoxInDp) {
    // Get current dimensions
    final int width = bitmap.getWidth();
    final int height = bitmap.getHeight();
    // Determine how much to scale: the dimension requiring
    // less scaling is.
    // closer to the its side. This way the image always
    // stays inside your.
    // bounding box AND either x/y axis touches it.
    final float xScale = boundBoxInDp / width;
    final float yScale = boundBoxInDp / height;
    final float scale = xScale <= yScale ? xScale : yScale;
    // Create a matrix for the scaling and add the scaling data
    final Matrix matrix = new Matrix();
    matrix.postScale(scale, scale);/*  www.  j  a  v  a 2  s  . c  o m*/
    // matrix.postRotate(rotate);
    // Create a new bitmap and convert it to a format understood
    // by the
    // ImageView
    final Bitmap scaledBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, false);
    // Apply the scaled bitmap
    return scaledBitmap;
}

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;/*from   w  ww .  j  a  v  a  2s .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

/**
 * Scales a bitmap to fit required ratio
 *///  w  w  w  .  j  a v  a2  s . c o m
@SuppressWarnings("unused")
private static Bitmap scaleImage(Context mContext, Bitmap bitmap, int reqWidth, int reqHeight) {

    // Get current dimensions AND the desired bounding box
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    int boundingX = dpToPx(mContext, reqWidth);
    int boundingY = dpToPx(mContext, reqHeight);

    // Determine how much to scale: the dimension requiring less scaling is
    // closer to the its side. This way the image always stays inside your
    // bounding box AND either x/y axis touches it.
    float xScale = ((float) boundingX) / width;
    float yScale = ((float) boundingY) / height;
    float scale = (xScale >= yScale) ? xScale : yScale;

    // Create a matrix for the scaling and add the scaling data
    Matrix matrix = new Matrix();
    matrix.postScale(scale, scale);

    // Create a new bitmap and convert it to a format understood by the
    // ImageView
    Bitmap scaledBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);

    return scaledBitmap;
}

From source file:Main.java

public static Bitmap zoomBitmap(Bitmap bitmap, float scale) {
    if (bitmap == null) {
        return null;
    }//from  w  ww  .j  a  v a2s  .  c o m
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    Matrix matrix = new Matrix();
    matrix.postScale(scale, scale);
    Bitmap result = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
    return result;
}