Example usage for android.graphics Matrix setScale

List of usage examples for android.graphics Matrix setScale

Introduction

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

Prototype

public void setScale(float sx, float sy) 

Source Link

Document

Set the matrix to scale by sx and sy.

Usage

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   ww  w  .  j  a v a2  s  .  c  o 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 rotateBitmap(String src, Bitmap bitmap) {
    try {/*from   w  w w .jav a  2  s .  co m*/
        int orientation = getExifOrientation(src);

        if (orientation == 1) {
            return bitmap;
        }

        Matrix matrix = new Matrix();
        switch (orientation) {
        case 2:
            matrix.setScale(-1, 1);
            break;
        case 3:
            matrix.setRotate(180);
            break;
        case 4:
            matrix.setRotate(180);
            matrix.postScale(-1, 1);
            break;
        case 5:
            matrix.setRotate(90);
            matrix.postScale(-1, 1);
            break;
        case 6:
            matrix.setRotate(90);
            break;
        case 7:
            matrix.setRotate(-90);
            matrix.postScale(-1, 1);
            break;
        case 8:
            matrix.setRotate(-90);
            break;
        default:
            return bitmap;
        }

        try {
            Bitmap oriented = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix,
                    true);
            bitmap.recycle();
            return oriented;
        } catch (OutOfMemoryError e) {
            e.printStackTrace();
            return bitmap;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    return bitmap;
}

From source file:Main.java

public static Bitmap normalizeExifRotateBitmap(Bitmap bitmap, int orientation) {

    Matrix matrix = new Matrix();
    switch (orientation) {
    case ExifInterface.ORIENTATION_NORMAL:
        return bitmap;
    case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
        matrix.setScale(-1, 1);
        break;//from   ww w  . ja v  a 2s  .  c  om
    case ExifInterface.ORIENTATION_ROTATE_180:
        matrix.setRotate(180);
        break;
    case ExifInterface.ORIENTATION_FLIP_VERTICAL:
        matrix.setRotate(180);
        matrix.postScale(-1, 1);
        break;
    case ExifInterface.ORIENTATION_TRANSPOSE:
        matrix.setRotate(90);
        matrix.postScale(-1, 1);
        break;
    case ExifInterface.ORIENTATION_ROTATE_90:
        matrix.setRotate(90);
        break;
    case ExifInterface.ORIENTATION_TRANSVERSE:
        matrix.setRotate(-90);
        matrix.postScale(-1, 1);
        break;
    case ExifInterface.ORIENTATION_ROTATE_270:
        matrix.setRotate(-90);
        break;
    default:
        return bitmap;
    }
    try {
        Bitmap bmRotated = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix,
                true);
        bitmap.recycle();
        return bmRotated;
    } catch (OutOfMemoryError e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

/**
 * Creates a centered bitmap of the desired size.
 *
 * @param source original bitmap source/*from   w  w  w  . j a  v a 2  s. c  o  m*/
 * @param width targeted width
 * @param height targeted height
 * @param options options used during thumbnail extraction
 */
public static Bitmap extractThumbnail(Bitmap source, int width, int height, int options) {
    if (source == null) {
        return null;
    }

    float scale;
    if (source.getWidth() < source.getHeight()) {
        scale = width / (float) source.getWidth();
    } else {
        scale = height / (float) source.getHeight();
    }
    Matrix matrix = new Matrix();
    matrix.setScale(scale, scale);
    Bitmap thumbnail = transform(matrix, source, width, height, OPTIONS_SCALE_UP | options);
    return thumbnail;
}

From source file:Main.java

public static Bitmap rotateBitmap(Bitmap bitmap, int exifOrientation) {
    Matrix matrix = new Matrix();
    switch (exifOrientation) {
    case ExifInterface.ORIENTATION_NORMAL:
    default://from w ww .  j a v  a2s . c o m
        return bitmap;
    case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
        matrix.setScale(-1, 1);
        break;
    case ExifInterface.ORIENTATION_ROTATE_180:
        matrix.setRotate(180);
        break;
    case ExifInterface.ORIENTATION_FLIP_VERTICAL:
        matrix.setRotate(180);
        matrix.postScale(-1, 1);
        break;
    case ExifInterface.ORIENTATION_TRANSPOSE:
        matrix.setRotate(90);
        matrix.postScale(-1, 1);
        break;
    case ExifInterface.ORIENTATION_ROTATE_90:
        matrix.setRotate(90);
        break;
    case ExifInterface.ORIENTATION_TRANSVERSE:
        matrix.setRotate(-90);
        matrix.postScale(-1, 1);
        break;
    case ExifInterface.ORIENTATION_ROTATE_270:
        matrix.setRotate(-90);
        break;
    }

    try {
        return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);

    } catch (OutOfMemoryError e) {
        e.printStackTrace();
        return null;
    }
}

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 .jav  a 2 s .c o m*/
 *
 * @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 createCroppedScaleBitmap(Bitmap src, int reqWidth, int reqHeight) {
    final int bWidth = src.getWidth();
    final int bHeight = src.getHeight();
    Matrix matrix = new Matrix();
    int maxSize = Math.max(reqHeight, reqWidth);
    float scaleX;
    if (bWidth * bHeight < reqWidth * reqHeight)
        scaleX = 0;// w  w  w.ja v  a  2  s. c  om
    else {
        if (bWidth > bHeight) {
            scaleX = (float) maxSize / bWidth;
        } else
            scaleX = (float) maxSize / bHeight;
    }
    Bitmap sourceBitmap;
    if (scaleX > 0 && scaleX != 1) {
        matrix.setScale(scaleX, scaleX);
        sourceBitmap = Bitmap.createBitmap(src, 0, 0, bWidth, bHeight, matrix, true);
        if (sourceBitmap != src && !src.isRecycled())
            src.recycle();
    } else
        sourceBitmap = src;
    return sourceBitmap;
}

From source file:Main.java

private static Bitmap createScaledBitmap(Bitmap src, int dstWidth, int dstHeight, float offset,
        boolean clipShadow, Paint paint) {

    Matrix m;
    synchronized (Bitmap.class) {
        m = sScaleMatrix;/*from  www .  j  av  a 2 s. co m*/
        sScaleMatrix = null;
    }

    if (m == null) {
        m = new Matrix();
    }

    final int width = src.getWidth();
    final int height = src.getHeight();
    final float sx = dstWidth / (float) width;
    final float sy = dstHeight / (float) height;
    m.setScale(sx, sy);

    Bitmap b = createBitmap(src, 0, 0, width, height, m, offset, clipShadow, paint);

    synchronized (Bitmap.class) {
        sScaleMatrix = m;
    }

    return b;
}

From source file:Main.java

/**
 * Transform source Bitmap to targeted width and height.
 *///  w  ww  .  j a v  a  2 s  .c o m
private static Bitmap transform(Matrix scaler, Bitmap source, int targetWidth, int targetHeight,
        boolean recycle) {

    float bitmapWidthF = source.getWidth();
    float bitmapHeightF = source.getHeight();

    float bitmapAspect = bitmapWidthF / bitmapHeightF;
    float viewAspect = (float) targetWidth / targetHeight;

    if (bitmapAspect > viewAspect) {
        float scale = targetHeight / bitmapHeightF;
        if (scale < .9F || scale > 1F) {
            scaler.setScale(scale, scale);
        } else {
            scaler = null;
        }
    } else {
        float scale = targetWidth / bitmapWidthF;
        if (scale < .9F || scale > 1F) {
            scaler.setScale(scale, scale);
        } else {
            scaler = null;
        }
    }

    Bitmap b1;
    if (scaler != null) {
        // this is used for minithumb and crop, so we want to filter here.
        b1 = Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), scaler, true);
    } else {
        b1 = source;
    }

    if (recycle && b1 != source) {
        source.recycle();
    }

    int dx1 = Math.max(0, b1.getWidth() - targetWidth);
    int dy1 = Math.max(0, b1.getHeight() - targetHeight);

    Bitmap b2 = Bitmap.createBitmap(b1, dx1 / 2, dy1 / 2, targetWidth, targetHeight, null, true);

    if (b2 != b1) {
        if (recycle || b1 != source) {
            b1.recycle();
        }
    }

    return b2;
}

From source file:Main.java

private static Bitmap decodeExifBitmap(File file, Bitmap src) {
    try {/*from  w w  w  .  j  a v a  2 s.co m*/
        // Try to load the bitmap as a bitmap file
        ExifInterface exif = new ExifInterface(file.getAbsolutePath());
        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
        if (orientation == ExifInterface.ORIENTATION_UNDEFINED
                || orientation == ExifInterface.ORIENTATION_NORMAL) {
            return src;
        }
        Matrix matrix = new Matrix();
        if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
            matrix.postRotate(90);
        } else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
            matrix.postRotate(180);
        } else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
            matrix.postRotate(270);
        } else if (orientation == ExifInterface.ORIENTATION_FLIP_HORIZONTAL) {
            matrix.setScale(-1, 1);
            matrix.postTranslate(src.getWidth(), 0);
        } else if (orientation == ExifInterface.ORIENTATION_FLIP_VERTICAL) {
            matrix.setScale(1, -1);
            matrix.postTranslate(0, src.getHeight());
        }
        // Rotate the bitmap
        return Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), matrix, true);
    } catch (IOException e) {
        // Ignore
    }
    return src;
}