Example usage for android.graphics Matrix postRotate

List of usage examples for android.graphics Matrix postRotate

Introduction

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

Prototype

public boolean postRotate(float degrees) 

Source Link

Document

Postconcats the matrix with the specified rotation.

Usage

From source file:Main.java

@SuppressWarnings("deprecation")
public static int PutImageScale(Canvas canvas, Drawable image, double Angle, int x, int y, double scale) {

    if (scale == 0.0)
        return 0;

    float newWidth = (int) Math.round((float) image.getIntrinsicWidth() * scale);
    float newHeight = (int) Math.round((float) image.getIntrinsicHeight() * scale);

    Bitmap bmp = ((BitmapDrawable) image).getBitmap();
    int width = bmp.getWidth();
    int height = bmp.getHeight();

    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;
    // createa matrix for the manipulation
    Matrix matrix = new Matrix();
    // resize the bit map
    matrix.postScale(scaleWidth, scaleHeight);
    // rotate the Bitmap
    matrix.postRotate((float) Angle);
    // recreate the new Bitmap
    Bitmap resizedBitmap = Bitmap.createBitmap(bmp, 0, 0, width, height, matrix, true);
    // make a Drawable from Bitmap to allow to set the BitMap
    // to the ImageView, ImageButton or what ever
    BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);

    bmd.setBounds(x, y, x + bmd.getIntrinsicWidth(), y + bmd.getIntrinsicHeight());
    bmd.draw(canvas);/*from w  ww.j ava  2 s.  c o m*/

    return bmd.getIntrinsicWidth();

}

From source file:Main.java

private static Bitmap decodeExifBitmap(File file, Bitmap src) {
    try {//from   w ww . j a  va 2  s . c  o  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;
}

From source file:Main.java

public static Matrix getRotateMatrix(Bitmap bitmap, int rotation) {
    Matrix matrix = new Matrix();
    if (bitmap != null && rotation != 0) {
        int cx = bitmap.getWidth() / 2;
        int cy = bitmap.getHeight() / 2;
        matrix.preTranslate(-cx, -cy);//from   w  w  w .j a v a  2 s  .  com
        matrix.postRotate(rotation);
        matrix.postTranslate(cx, cy);
    }
    return matrix;
}

From source file:Main.java

@SuppressWarnings("deprecation")
public static int PutImageTargetHeight(Canvas canvas, Drawable image, double Angle, int x, int y,
        int newHeight) {

    float scale = (float) newHeight / (float) image.getIntrinsicHeight();
    float newWidth = (int) Math.round((float) image.getIntrinsicWidth() * scale);

    Bitmap bmp = ((BitmapDrawable) image).getBitmap();
    int width = bmp.getWidth();
    int height = bmp.getHeight();

    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;
    // createa matrix for the manipulation
    Matrix matrix = new Matrix();
    // resize the bit map
    matrix.postScale(scaleWidth, scaleHeight);
    // rotate the Bitmap
    matrix.postRotate((float) Angle);
    // recreate the new Bitmap
    Bitmap resizedBitmap = Bitmap.createBitmap(bmp, 0, 0, width, height, matrix, true);
    // make a Drawable from Bitmap to allow to set the BitMap
    // to the ImageView, ImageButton or what ever
    BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);

    bmd.setBounds(x, y, x + bmd.getIntrinsicWidth(), y + bmd.getIntrinsicHeight());
    bmd.draw(canvas);/*from   ww  w  .j  a  va2  s  .  c  o m*/

    return bmd.getIntrinsicWidth();

}

From source file:Main.java

public static Bitmap resizeBitmap(Bitmap input, int destWidth, int destHeight, int rotation)
        throws OutOfMemoryError {
    int dstWidth = destWidth;
    int dstHeight = destHeight;
    int srcWidth = input.getWidth();
    int srcHeight = input.getHeight();

    if ((rotation == 90) || (rotation == 270)) {
        dstWidth = destHeight;/*from   w w w . j  a va  2  s . c o m*/
        dstHeight = destWidth;
    }

    boolean needsResize = false;

    if ((srcWidth > dstWidth) || (srcHeight > dstHeight)) {
        needsResize = true;

        float ratio1 = (float) srcWidth / dstWidth;
        float ratio2 = (float) srcHeight / dstHeight;
        Log.v("dsd", "ratio1:" + ratio1 + " ratio2:" + ratio2);

        if (ratio1 > ratio2) {
            float p = (float) dstWidth / srcWidth;
            dstHeight = (int) (srcHeight * p);
        } else {
            float p = (float) dstHeight / srcHeight;
            dstWidth = (int) (srcWidth * p);
        }
    } else {
        dstWidth = srcWidth;
        dstHeight = srcHeight;
    }

    Log.v("dsd", "dstWidth:" + dstWidth + " dstHeight:" + dstHeight + " srcWidth:" + srcWidth + " srcHeight:"
            + srcHeight);
    if ((needsResize) || (rotation != 0)) {
        Bitmap output = null;
        if (rotation == 0) {
            output = Bitmap.createScaledBitmap(input, dstWidth, dstHeight, true);
        } else {
            Matrix matrix = new Matrix();
            matrix.postScale((float) dstWidth / srcWidth, (float) dstHeight / srcHeight);
            matrix.postRotate(rotation);
            output = Bitmap.createBitmap(input, 0, 0, srcWidth, srcHeight, matrix, true);
        }
        return output;
    }
    return input;
}

From source file:Main.java

/**
 * Resize a bitmap object to fit the passed width and height
 * //ww w  .  j a v a2  s .  co m
 * @param input
 *           The bitmap to be resized
 * @param destWidth
 *           Desired maximum width of the result bitmap
 * @param destHeight
 *           Desired maximum height of the result bitmap
 * @return A new resized bitmap
 * @throws OutOfMemoryError
 *            if the operation exceeds the available vm memory
 */
public static Bitmap resizeBitmap(final Bitmap input, int destWidth, int destHeight, int rotation)
        throws OutOfMemoryError, Exception {

    int dstWidth = destWidth;
    int dstHeight = destHeight;
    final int srcWidth = input.getWidth();
    final int srcHeight = input.getHeight();

    if (rotation == 90 || rotation == 270) {
        dstWidth = destHeight;
        dstHeight = destWidth;
    }

    boolean needsResize = false;
    float p;
    if ((srcWidth > dstWidth) || (srcHeight > dstHeight)) {
        needsResize = true;
        if ((srcWidth > srcHeight) && (srcWidth > dstWidth)) {
            p = (float) dstWidth / (float) srcWidth;
            dstHeight = (int) (srcHeight * p);
        } else {
            p = (float) dstHeight / (float) srcHeight;
            dstWidth = (int) (srcWidth * p);
        }
    } else {
        dstWidth = srcWidth;
        dstHeight = srcHeight;
    }

    if (needsResize || rotation != 0) {
        Bitmap output;

        if (rotation == 0) {
            output = Bitmap.createScaledBitmap(input, dstWidth, dstHeight, true);
        } else {
            Matrix matrix = new Matrix();
            matrix.postScale((float) dstWidth / srcWidth, (float) dstHeight / srcHeight);
            matrix.postRotate(rotation);
            output = Bitmap.createBitmap(input, 0, 0, srcWidth, srcHeight, matrix, true);
        }
        return output;
    } else
        return input;
}

From source file:Main.java

/**
 * Resize a bitmap object to fit the passed width and height
 *
 * @param input/*from   w  w  w. java  2 s .  co  m*/
 *           The bitmap to be resized
 * @param destWidth
 *           Desired maximum width of the result bitmap
 * @param destHeight
 *           Desired maximum height of the result bitmap
 * @return A new resized bitmap
 * @throws OutOfMemoryError
 *            if the operation exceeds the available vm memory
 */
public static Bitmap resizeBitmap(final Bitmap input, int destWidth, int destHeight, int rotation)
        throws OutOfMemoryError {

    int dstWidth = destWidth;
    int dstHeight = destHeight;
    final int srcWidth = input.getWidth();
    final int srcHeight = input.getHeight();

    if (rotation == 90 || rotation == 270) {
        dstWidth = destHeight;
        dstHeight = destWidth;
    }

    boolean needsResize = false;
    float p;
    if ((srcWidth > dstWidth) || (srcHeight > dstHeight)) {
        needsResize = true;
        if ((srcWidth > srcHeight) && (srcWidth > dstWidth)) {
            p = (float) dstWidth / (float) srcWidth;
            dstHeight = (int) (srcHeight * p);
        } else {
            p = (float) dstHeight / (float) srcHeight;
            dstWidth = (int) (srcWidth * p);
        }
    } else {
        dstWidth = srcWidth;
        dstHeight = srcHeight;
    }

    if (needsResize || rotation != 0) {
        Bitmap output;

        if (rotation == 0) {
            output = Bitmap.createScaledBitmap(input, dstWidth, dstHeight, true);
        } else {
            Matrix matrix = new Matrix();
            matrix.postScale((float) dstWidth / srcWidth, (float) dstHeight / srcHeight);
            matrix.postRotate(rotation);
            output = Bitmap.createBitmap(input, 0, 0, srcWidth, srcHeight, matrix, true);
        }
        return output;
    } else
        return input;
}

From source file:com.wishlist.Utility.java

public static byte[] scaleImage(Context context, Uri photoUri) throws IOException {
    InputStream is = context.getContentResolver().openInputStream(photoUri);
    BitmapFactory.Options dbo = new BitmapFactory.Options();
    dbo.inJustDecodeBounds = true;/*  w ww .j  a  va 2s  . c  om*/
    BitmapFactory.decodeStream(is, null, dbo);
    is.close();

    int rotatedWidth, rotatedHeight;
    int orientation = getOrientation(context, photoUri);

    if (orientation == 90 || orientation == 270) {
        rotatedWidth = dbo.outHeight;
        rotatedHeight = dbo.outWidth;
    } else {
        rotatedWidth = dbo.outWidth;
        rotatedHeight = dbo.outHeight;
    }

    Bitmap srcBitmap;
    is = context.getContentResolver().openInputStream(photoUri);
    if (rotatedWidth > MAX_IMAGE_DIMENSION || rotatedHeight > MAX_IMAGE_DIMENSION) {
        float widthRatio = ((float) rotatedWidth) / ((float) MAX_IMAGE_DIMENSION);
        float heightRatio = ((float) rotatedHeight) / ((float) MAX_IMAGE_DIMENSION);
        float maxRatio = Math.max(widthRatio, heightRatio);

        // Create the bitmap from file
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = (int) maxRatio;
        srcBitmap = BitmapFactory.decodeStream(is, null, options);
    } else {
        srcBitmap = BitmapFactory.decodeStream(is);
    }
    is.close();

    /* if the orientation is not 0 (or -1, which means we don't know), we
     * have to do a rotation. */
    if (orientation > 0) {
        Matrix matrix = new Matrix();
        matrix.postRotate(orientation);

        srcBitmap = Bitmap.createBitmap(srcBitmap, 0, 0, srcBitmap.getWidth(), srcBitmap.getHeight(), matrix,
                true);
    }

    String type = context.getContentResolver().getType(photoUri);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    if (type.equals("image/png")) {
        srcBitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
    } else if (type.equals("image/jpg") || type.equals("image/jpeg")) {
        srcBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    }
    byte[] bMapArray = baos.toByteArray();
    baos.close();
    return bMapArray;
}

From source file:com.example.facebook_photo.Utility.java

public static byte[] scaleImage(Context context, Uri photoUri) throws IOException {
    InputStream is = context.getContentResolver().openInputStream(photoUri);
    BitmapFactory.Options dbo = new BitmapFactory.Options();
    dbo.inJustDecodeBounds = true;//from w ww . jav a 2 s. c  o m
    BitmapFactory.decodeStream(is, null, dbo);
    is.close();

    int rotatedWidth, rotatedHeight;
    int orientation = getOrientation(context, photoUri);

    if (orientation == 90 || orientation == 270) {
        rotatedWidth = dbo.outHeight;
        rotatedHeight = dbo.outWidth;
    } else {
        rotatedWidth = dbo.outWidth;
        rotatedHeight = dbo.outHeight;
    }

    Bitmap srcBitmap;
    is = context.getContentResolver().openInputStream(photoUri);
    if (rotatedWidth > MAX_IMAGE_DIMENSION || rotatedHeight > MAX_IMAGE_DIMENSION) {
        float widthRatio = ((float) rotatedWidth) / ((float) MAX_IMAGE_DIMENSION);
        float heightRatio = ((float) rotatedHeight) / ((float) MAX_IMAGE_DIMENSION);
        float maxRatio = Math.max(widthRatio, heightRatio);

        // Create the bitmap from file
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = (int) maxRatio;
        srcBitmap = BitmapFactory.decodeStream(is, null, options);
    } else {
        srcBitmap = BitmapFactory.decodeStream(is);
    }
    is.close();

    /*
     * if the orientation is not 0 (or -1, which means we don't know), we
     * have to do a rotation.
     */
    if (orientation > 0) {
        Matrix matrix = new Matrix();
        matrix.postRotate(orientation);

        srcBitmap = Bitmap.createBitmap(srcBitmap, 0, 0, srcBitmap.getWidth(), srcBitmap.getHeight(), matrix,
                true);
    }

    String type = context.getContentResolver().getType(photoUri);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    if (type.equals("image/png")) {
        srcBitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
    } else if (type.equals("image/jpg") || type.equals("image/jpeg")) {
        srcBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    }
    byte[] bMapArray = baos.toByteArray();
    baos.close();
    return bMapArray;
}

From source file:Main.java

public static Bitmap getBitmapByFixingRotationForFile(String filePath, Bitmap sourceBitmap,
        Activity activityForScreenOrientation, boolean freeSourceBitmap) {
    try {/*  w  w  w  . jav a2  s  .c  o  m*/
        ExifInterface exif = new ExifInterface(filePath);
        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

        if (orientation == ExifInterface.ORIENTATION_UNDEFINED
                && Build.MANUFACTURER.toLowerCase(Locale.ENGLISH).contains("htc"))
            return null;

        boolean flippedHorizontally = false, flippedVertically = false;

        int angle = 0;

        if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
            angle += 90;
        } else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
            angle += 180;
        } else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
            angle += 270;
        } else if (orientation == ExifInterface.ORIENTATION_FLIP_HORIZONTAL) {
            flippedHorizontally = true;
        } else if (orientation == ExifInterface.ORIENTATION_FLIP_VERTICAL) {
            flippedVertically = true;
        } else if (orientation == ExifInterface.ORIENTATION_TRANSPOSE) {
            angle += 90;
            flippedVertically = true;
        } else if (orientation == ExifInterface.ORIENTATION_TRANSVERSE) {
            angle -= 90;
            flippedVertically = true;
        }

        if (activityForScreenOrientation != null) {
            orientation = getScreenOrientation(activityForScreenOrientation);
            if (orientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
                angle += 90;
            } else if (orientation == ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE) {
                angle += 180;
            } else if (orientation == ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT) {
                angle += 270;
            }
        }

        Bitmap bmp = sourceBitmap;
        if (bmp == null) {
            bmp = BitmapFactory.decodeFile(filePath, null);
        }
        if (angle != 0) {
            Matrix mat = new Matrix();
            mat.postRotate(angle);

            if (flippedHorizontally) {
                mat.postScale(-1.f, 1.f);
            }
            if (flippedVertically) {
                mat.postScale(1.f, -1.f);
            }

            Bitmap rotated = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), mat, true);
            if (freeSourceBitmap || bmp != sourceBitmap) {
                bmp.recycle();
            }
            bmp = rotated;
        }

        return bmp;

    } catch (IOException e) {
        Log.w("TAG", "-- Error in setting image");
    } catch (OutOfMemoryError oom) {
        Log.w("TAG", "-- OOM Error in setting image");
    }

    return null;
}