Example usage for android.graphics Matrix postScale

List of usage examples for android.graphics Matrix postScale

Introduction

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

Prototype

public boolean postScale(float sx, float sy) 

Source Link

Document

Postconcats the matrix with the specified scale.

Usage

From source file:Main.java

public static Bitmap zoomBitmap(Bitmap bitmap, int w, int h) {
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    Matrix matrix = new Matrix();
    float scaleWidht = ((float) w) / width;
    float scaleHeight = ((float) h) / height;
    matrix.postScale(scaleWidht, scaleHeight);
    Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
    return newbmp;
}

From source file:Main.java

/**
 * /*  ww  w.  ja v  a 2  s.  c  o  m*/
 * @param filePath
 * @param targetWidth
 * @param targetHeight
 * @param recycle
 * @return
 */
public static Bitmap resizeAndCropCenter(Bitmap bitmap, int targetWidth, int targetHeight, boolean recycle) {
    int w = bitmap.getWidth();
    int h = bitmap.getHeight();

    int scaleWidth = w / targetWidth;
    int scaleHeight = h / targetHeight;
    int scale = scaleWidth < scaleHeight ? scaleWidth : scaleHeight;
    if (scale < 1) {
        scale = 1;
    }
    //get scalebitmap
    float fScaleWidth = targetWidth / ((float) w);
    float fScaleHeight = targetHeight / ((float) h);
    float fScale = fScaleWidth > fScaleHeight ? fScaleWidth : fScaleHeight;
    if (fScale > 1)
        fScale = 1;
    Matrix matrix = new Matrix();
    matrix.postScale(fScale, fScale);
    Bitmap scaleBitmap = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true);

    //get targetBitmap
    int bitmapX = (scaleBitmap.getWidth() - targetWidth) / 2;
    bitmapX = bitmapX > 0 ? bitmapX : 0;
    int bitmapY = (scaleBitmap.getHeight() - targetHeight) / 2;
    bitmapY = bitmapY > 0 ? bitmapY : 0;
    targetWidth = targetWidth < (scaleBitmap.getWidth()) ? targetWidth : (scaleBitmap.getWidth());
    targetHeight = targetHeight < (scaleBitmap.getHeight()) ? targetHeight : (scaleBitmap.getHeight());
    Bitmap targetBitmap = Bitmap.createBitmap(scaleBitmap, bitmapX, bitmapY, targetWidth, targetHeight);

    if (recycle)
        bitmap.recycle();
    return targetBitmap;
}

From source file:Main.java

/**
 * /*from  ww w.j  a  v  a  2  s .c om*/
 * @param filePath
 * @param targetWidth
 * @param targetHeight
 * @param recycle
 * @return
 */
public static Bitmap resizeAndCropCenter(Bitmap bitmap, int targetWidth, int targetHeight, boolean recycle) {
    int w = bitmap.getWidth();
    int h = bitmap.getHeight();

    int scaleWidth = w / targetWidth;
    int scaleHeight = h / targetHeight;
    int scale = scaleWidth < scaleHeight ? scaleWidth : scaleHeight;
    if (scale < 1) {
        scale = 1;
    }
    //get scalebitmap
    float fScaleWidth = targetWidth / ((float) w);
    float fScaleHeight = targetHeight / ((float) h);
    float fScale = fScaleWidth > fScaleHeight ? fScaleWidth : fScaleHeight;
    if (fScale > 1)
        fScale = 1;
    Matrix matrix = new Matrix();
    matrix.postScale(fScale, fScale);
    Bitmap scaleBitmap = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true);

    //get targetBitmap
    int bitmapX = (scaleBitmap.getWidth() - targetWidth) / 2;
    bitmapX = bitmapX > 0 ? bitmapX : 0;
    int bitmapY = (scaleBitmap.getHeight() - targetHeight) / 2;
    bitmapY = bitmapY > 0 ? bitmapY : 0;
    targetWidth = targetWidth < (scaleBitmap.getWidth()) ? targetWidth : (scaleBitmap.getWidth());
    targetHeight = targetHeight < (scaleBitmap.getHeight()) ? targetHeight : (scaleBitmap.getHeight());
    Bitmap targetBitmap = Bitmap.createBitmap(scaleBitmap, bitmapX, bitmapY, targetWidth, targetHeight);
    //      if (recycle) bitmap.recycle();

    if (targetBitmap != bitmap && recycle) {
        bitmap.recycle();
    }
    return targetBitmap;
}

From source file:Main.java

/**
 * //from www  . j av a2  s  .co  m
 * @param filePath
 * @param targetWidth
 * @param targetHeight
 * @param recycle
 * @return
 */
public static Bitmap resizeAndCropCenter(Bitmap bitmap, int targetWidth, int targetHeight, boolean recycle) {
    int w = bitmap.getWidth();
    int h = bitmap.getHeight();

    int scaleWidth = w / targetWidth;
    int scaleHeight = h / targetHeight;
    int scale = scaleWidth < scaleHeight ? scaleWidth : scaleHeight;
    if (scale < 1) {
        scale = 1;
    }
    //get scalebitmap
    float fScaleWidth = targetWidth / ((float) w);
    float fScaleHeight = targetHeight / ((float) h);
    float fScale = fScaleWidth > fScaleHeight ? fScaleWidth : fScaleHeight;
    if (fScale > 1)
        fScale = 1;
    Matrix matrix = new Matrix();
    matrix.postScale(fScale, fScale);
    Bitmap scaleBitmap = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true);

    //get targetBitmap
    int bitmapX = (scaleBitmap.getWidth() - targetWidth) / 2;
    bitmapX = bitmapX > 0 ? bitmapX : 0;
    int bitmapY = (scaleBitmap.getHeight() - targetHeight) / 2;
    bitmapY = bitmapY > 0 ? bitmapY : 0;
    targetWidth = targetWidth < (scaleBitmap.getWidth()) ? targetWidth : (scaleBitmap.getWidth());
    targetHeight = targetHeight < (scaleBitmap.getHeight()) ? targetHeight : (scaleBitmap.getHeight());

    if (bitmapX == 0 && bitmapY == 0 && targetWidth == w && targetHeight == h) {
        return bitmap;
    }
    Bitmap targetBitmap = Bitmap.createBitmap(scaleBitmap, bitmapX, bitmapY, targetWidth, targetHeight);
    if (recycle)
        bitmap.recycle();
    return targetBitmap;
}

From source file:Main.java

/**
 * //  w ww.j  av a2 s  . co m
 * @param filePath
 * @param targetWidth
 * @param targetHeight
 * @param recycle
 * @return
 */
public static Bitmap resizeAndCropCenter(String filePath, int targetWidth, int targetHeight, boolean recycle) {
    //get sampleBitmap
    Options opts = new Options();
    opts.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(filePath, opts);

    int scaleWidth = opts.outWidth / targetWidth;
    int scaleHeight = opts.outHeight / targetHeight;
    int scale = scaleWidth < scaleHeight ? scaleWidth : scaleHeight;
    if (scale < 1) {
        scale = 1;
    }
    opts.inJustDecodeBounds = false;
    opts.inSampleSize = scale;
    Bitmap sampleBitmap = BitmapFactory.decodeFile(filePath, opts);

    if (opts.outWidth == -1 || opts.outHeight == -1) {
        throw new IllegalArgumentException();
    }
    //get scalebitmap
    float fScaleWidth = targetWidth / ((float) opts.outWidth);
    float fScaleHeight = targetHeight / ((float) opts.outHeight);
    float fScale = fScaleWidth > fScaleHeight ? fScaleWidth : fScaleHeight;
    if (fScale > 1)
        fScale = 1;
    Matrix matrix = new Matrix();
    matrix.postScale(fScale, fScale);
    Bitmap scaleBitmap = Bitmap.createBitmap(sampleBitmap, 0, 0, opts.outWidth, opts.outHeight, matrix, true);

    //get targetBitmap
    int bitmapX = (scaleBitmap.getWidth() - targetWidth) / 2;
    bitmapX = bitmapX > 0 ? bitmapX : 0;
    int bitmapY = (scaleBitmap.getHeight() - targetHeight) / 2;
    bitmapY = bitmapY > 0 ? bitmapY : 0;
    targetWidth = targetWidth < (scaleBitmap.getWidth()) ? targetWidth : (scaleBitmap.getWidth());
    targetHeight = targetHeight < (scaleBitmap.getHeight()) ? targetHeight : (scaleBitmap.getHeight());
    Bitmap targetBitmap = Bitmap.createBitmap(scaleBitmap, bitmapX, bitmapY, targetWidth, targetHeight);

    if (recycle)
        sampleBitmap.recycle();
    return targetBitmap;
}

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);
            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);
            }/*w w  w. j ava  2 s.c o  m*/
        }

        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 void prepareMatrix(Matrix matrix, boolean mirror, int displayOrientation, int viewWidth,
        int viewHeight) {
    matrix.setScale((float) (mirror ? -1 : 1), 1.0F);
    matrix.postRotate((float) displayOrientation);
    matrix.postScale((float) viewWidth / 2000.0F, (float) viewHeight / 2000.0F);
    matrix.postTranslate((float) viewWidth / 2.0F, (float) viewHeight / 2.0F);
}

From source file:Main.java

public static Bitmap zoomBitmap(Bitmap bitmap, int w, int h) {
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    Matrix matrix = new Matrix();
    float scaleWidth = (float) (w / width);
    float scaleHeight = (float) (h / height);
    matrix.postScale(scaleWidth, scaleHeight);
    Bitmap result = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
    return result;
}

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);
            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);
            }/*from  w ww  .j a  va2 s  .co  m*/
        }
        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 Bitmap rotateImage(Bitmap bitmap, String storagePath) {
    Bitmap resultBitmap = bitmap;//from  w  w  w. jav  a2  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;
}