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

/**
 * Method to flip horizontally a Bitmap.
 *
 * @param source The original Bitmap./*w  w w .  j  a va  2s.c o  m*/
 * @return The flipped Bitmap.
 */
public static Bitmap flipHorizonally(Bitmap source) {
    Matrix m = new Matrix();
    m.setScale(-1, 1);
    return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), m, false);
}

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();/*from  w  ww .  jav  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 temp = Bitmap.createBitmap(bm.getHeight(), bm.getWidth(), Bitmap.Config.ARGB_8888);

    Paint paint = new Paint();
    Canvas canvas = new Canvas(temp);
    canvas.drawBitmap(bm, m, paint);

    return temp;
}

From source file:Main.java

public static Bitmap setScale(Bitmap source, int width, int height) {
    if (source == null || width < 1 || height < 1) {
        return null;
    }/*from w  w w  .  ja  va2  s .c  om*/
    int w = source.getWidth();
    int h = source.getHeight();
    Matrix matrix = new Matrix();
    float scaleWidth = ((float) width / w);
    float scaleHeight = ((float) height / h);
    matrix.postScale(scaleWidth, scaleHeight);
    Bitmap bitmap = Bitmap.createBitmap(source, 0, 0, w, h, matrix, true);
    return bitmap;
}

From source file:Main.java

public static Bitmap bitmapZoomByScale(Bitmap srcBitmap, float scaleWidth, float scaleHeight) {
    int srcWidth = srcBitmap.getWidth();
    int srcHeight = srcBitmap.getHeight();
    Matrix matrix = new Matrix();

    matrix.postScale(scaleWidth, scaleHeight);
    Bitmap resizedBitmap = Bitmap.createBitmap(srcBitmap, 0, 0, srcWidth, srcHeight, matrix, true);
    if (resizedBitmap != null) {
        srcBitmap = null;/*from   w  ww .ja  va2s .com*/
        return resizedBitmap;
    } else {
        return srcBitmap;
    }
}

From source file:Main.java

public static Matrix rotateImage(Context context, Uri imageUri, File f) {
    Matrix matrix = new Matrix();
    try {//from www.  j ava  2  s .  c  o  m
        if (imageUri != null) {
            context.getContentResolver().notifyChange(imageUri, null);
        }
        ExifInterface exif = new ExifInterface(f.getAbsolutePath());
        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

        switch (orientation) {
        case ExifInterface.ORIENTATION_ROTATE_270:
            matrix.postRotate(270);
            break;
        case ExifInterface.ORIENTATION_ROTATE_180:
            matrix.postRotate(180);
            break;
        case ExifInterface.ORIENTATION_ROTATE_90:
            matrix.postRotate(90);
            break;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return matrix;
}

From source file:Main.java

public static Bitmap overlay(Bitmap bmp1, Bitmap bmp2) {
    Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(), bmp1.getConfig());
    Canvas canvas = new Canvas(bmOverlay);
    canvas.drawBitmap(bmp1, new Matrix(), null);
    canvas.drawBitmap(bmp2, 0, 0, null);
    return bmOverlay;
}

From source file:Main.java

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

From source file:Main.java

public static Bitmap getSolideSizeBitmap(Bitmap bm, int width, int height) {
    float widthScale = (width * 1f) / (bm.getWidth() * 1f);
    float heightScale = (height * 1f) / (bm.getHeight() * 1f);
    Matrix matrix = new Matrix();
    matrix.setScale(widthScale, heightScale);
    return Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, false);
}

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 va2s.c  o  m*/
    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

public static Bitmap zoomBitmap(Bitmap bitmap, float scale) {
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    Log.e("ss", "width=" + width);
    Log.e("ss", "height=" + height);
    Matrix matrix = new Matrix();
    //float scaleWidht = ((float)w / width);
    //float scaleHeight = ((float)h / height);
    matrix.postScale(scale, scale);/*from   w  w w  .j a v  a2s  .  c  o m*/
    Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
    return newbmp;
}