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:fi.tuukka.weather.utils.Utils.java

public static void showImage(Activity activity, View view, Bitmap bmp) {
    final Dialog imageDialog = new Dialog(activity);
    imageDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    imageDialog.setContentView(R.layout.showimage);
    imageDialog.setCancelable(true);/*from   w  w  w .ja  va  2 s . co  m*/

    ImageView imageView = (ImageView) imageDialog.findViewById(R.id.imageView);
    // Getting width & height of the given image.
    DisplayMetrics displayMetrics = activity.getResources().getDisplayMetrics();
    int wn = displayMetrics.widthPixels;
    int hn = displayMetrics.heightPixels;
    int wo = bmp.getWidth();
    int ho = bmp.getHeight();
    Matrix mtx = new Matrix();
    // Setting rotate to 90
    mtx.preRotate(90);
    // Setting resize
    mtx.postScale(((float) 1.3 * wn) / ho, ((float) 1.3 * hn) / wo);
    // Rotating Bitmap
    Bitmap rotatedBMP = Bitmap.createBitmap(bmp, 0, 0, wo, ho, mtx, true);
    BitmapDrawable bmd = new BitmapDrawable(rotatedBMP);

    imageView.setImageDrawable(bmd);

    imageView.setOnClickListener(new View.OnClickListener() {
        public void onClick(View button) {
            imageDialog.dismiss();
        }
    });

    imageDialog.show();
}

From source file:com.tomi.ginatask.MainActivity.java

public static Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
    int width = bm.getWidth();
    int height = bm.getHeight();
    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;
    // CREATE A MATRIX FOR THE MANIPULATION
    Matrix matrix = new Matrix();
    // RESIZE THE BIT MAP
    matrix.postScale(scaleWidth, scaleHeight);
    // RECREATE THE NEW BITMAP
    return Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);

}

From source file:Main.java

/** create a coordination convert matrix
 * <br>/* w w  w  .j a  va  2  s  .com*/
 * 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:com.owncloud.android.utils.BitmapUtils.java

/**
 * Rotate bitmap according to EXIF orientation. 
 * Cf. http://www.daveperrett.com/articles/2012/07/28/exif-orientation-handling-is-a-ghetto/ 
 * @param bitmap Bitmap to be rotated/*w w  w.  ja va2s  .c  om*/
 * @param storagePath Path to source file of bitmap. Needed for EXIF information.
 * @return correctly EXIF-rotated bitmap
 */
public static Bitmap rotateImage(final Bitmap bitmap, final String storagePath) {
    try {
        ExifInterface exifInterface = new ExifInterface(storagePath);
        final int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);

        Matrix matrix = new Matrix();
        // 1: nothing to do

        switch (orientation) {
        case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
            matrix.postScale(-1.0f, 1.0f);
            break;
        case ExifInterface.ORIENTATION_ROTATE_180:
            matrix.postRotate(180);
            break;
        case ExifInterface.ORIENTATION_FLIP_VERTICAL:
            matrix.postScale(1.0f, -1.0f);
            break;
        case ExifInterface.ORIENTATION_TRANSPOSE:
            matrix.postRotate(-90);
            matrix.postScale(1.0f, -1.0f);
            break;
        case ExifInterface.ORIENTATION_ROTATE_90:
            matrix.postRotate(90);
            break;
        case ExifInterface.ORIENTATION_TRANSVERSE:
            matrix.postRotate(90);
            matrix.postScale(1.0f, -1.0f);
            break;
        case ExifInterface.ORIENTATION_ROTATE_270:
            matrix.postRotate(270);
            break;
        }

        // Rotate the bitmap
        final Bitmap resultBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(),
                matrix, true);
        if (resultBitmap != bitmap) {
            bitmap.recycle();
        }
        return resultBitmap;
    } catch (Exception exception) {
        Log_OC.e("BitmapUtil", "Could not rotate the image: " + storagePath);
        return bitmap;
    }
}

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

public static Bitmap rotateBitmap(Bitmap bitmap, int exifOrientation) {
    Matrix matrix = new Matrix();
    switch (exifOrientation) {
    case ExifInterface.ORIENTATION_NORMAL:
    default:/*w  w  w.  j  ava  2s. c om*/
        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

private static Bitmap scale(Bitmap in, int size) {
    int width = in.getWidth();
    int height = in.getHeight();
    float scalew;
    float scaleh;
    Matrix matrix = new Matrix();
    //scale smaller axis to size (if necessary)
    if (width > height) {
        scaleh = (float) size / height;
        scalew = scaleh;//from w ww.ja va2 s  .  co  m
        if ((width * scalew) % 2 != 0) {
            scalew = ((width * scalew) + 1) / (float) width;
        }

    } else {
        scalew = (float) size / width;
        scaleh = scalew;
        if ((height * scaleh) % 2 != 0) {
            scaleh = ((height * scaleh) + 1) / (float) height;
        }
    }
    Log.i("RESIZING", "old width: " + width + "old height: " + height + "new size: " + size);
    matrix.postScale(scalew, scaleh);
    Bitmap scaledPic = Bitmap.createBitmap(in, 0, 0, width, height, matrix, true);
    return scaledPic;
}

From source file:com.daiv.android.twitter.services.SendTweet.java

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

    Log.v("Test_composing_image", "rotation: " + orientation);

    try {/*from  ww w  .  j a va 2 s . c  o  m*/
        Matrix matrix = new Matrix();
        switch (orientation) {
        case ExifInterface.ORIENTATION_NORMAL:
            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;
        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;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return bitmap;
}

From source file:Main.java

/**
 * Gets straighten matrix for the given bounds and degrees.
 *//*from  w ww  . j  a v a  2  s . c  o  m*/
public static void getStraightenMatrix(RectF bounds, float degrees, Matrix matrix) {
    matrix.reset();
    if ((degrees != 0) && !bounds.isEmpty()) {
        float w = bounds.width() / 2;
        float h = bounds.height() / 2;
        float adjustAngle;
        if ((degrees < 0 && w > h) || (degrees > 0 && w <= h)) {
            // The top left point is the boundary.
            adjustAngle = (float) Math.atan(h / -w) + MATH_PI + degrees * DEGREES_TO_RADIAN;
        } else {
            // The top right point is the boundary.
            adjustAngle = (float) Math.atan(h / w) - MATH_PI + degrees * DEGREES_TO_RADIAN;
        }
        float radius = (float) Math.hypot(w, h);
        float scaleX = (float) Math.abs(radius * Math.cos(adjustAngle)) / w;
        float scaleY = (float) Math.abs(radius * Math.sin(adjustAngle)) / h;
        float scale = Math.max(scaleX, scaleY);

        postRotateMatrix(degrees, new RectF(bounds), matrix);
        matrix.postScale(scale, scale);
    }
}