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

public static Bitmap rotateBitmapInNeeded(String path, Bitmap srcBitmap) {
    if (TextUtils.isEmpty(path) || srcBitmap == null) {
        return null;
    }/*from   ww w .ja v a 2 s.  c o  m*/

    ExifInterface localExifInterface;
    try {
        localExifInterface = new ExifInterface(path);
        int rotateInt = localExifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                ExifInterface.ORIENTATION_NORMAL);
        float rotate = getImageRotate(rotateInt);
        if (rotate != 0) {
            Matrix matrix = new Matrix();
            matrix.postRotate(rotate);
            Bitmap dstBitmap = Bitmap.createBitmap(srcBitmap, 0, 0, srcBitmap.getWidth(), srcBitmap.getHeight(),
                    matrix, false);
            if (dstBitmap == null) {
                return srcBitmap;
            } else {
                if (srcBitmap != null && !srcBitmap.isRecycled()) {
                    srcBitmap.recycle();
                }
                return dstBitmap;
            }
        } else {
            return srcBitmap;
        }
    } catch (IOException e) {
        e.printStackTrace();
        return srcBitmap;
    }
}

From source file:Main.java

private static Bitmap createScaledBitmap(File f, int rotation, int scale) throws FileNotFoundException {
    BitmapFactory.Options o2 = new BitmapFactory.Options();
    o2.inSampleSize = scale;// ww w .java  2  s .  c  o m
    Bitmap bitmap = BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
    if (0 < rotation) {
        Matrix matrix = new Matrix();
        matrix.postRotate(rotation);
        Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix,
                true);
        if (rotatedBitmap != bitmap) {
            bitmap.recycle();
            bitmap = null;
        }
        return rotatedBitmap;
    }
    return bitmap;
}

From source file:Main.java

public static Bitmap rotatingImageView(int degree, Bitmap source) {
    Matrix matrix = new Matrix();
    matrix.postRotate((float) degree);
    return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
}

From source file:Main.java

public static boolean rotateBitmap(File inFile, File outFile, int angle)
        throws FileNotFoundException, IOException {
    // Declare/*  w ww  .ja  v  a  2 s . c o m*/
    FileInputStream inStream = null;
    FileOutputStream outStream = null;
    // Create options
    BitmapFactory.Options options = new BitmapFactory.Options();
    // Create transform matrix
    Matrix matrix = new Matrix();
    matrix.postRotate(angle);
    // Increment inSampleSize progressively to reduce image resolution and size. If
    // the program is properly managing memory, and you don't have other large images
    // loaded in memory, this loop will generally not need to go through more than 3
    // iterations. To be safe though, we stop looping after a certain amount of tries
    // to avoid infinite loops
    for (options.inSampleSize = 1; options.inSampleSize <= 32; options.inSampleSize++) {
        try {
            // Load the bitmap from file
            inStream = new FileInputStream(inFile);
            Bitmap originalBitmap = BitmapFactory.decodeStream(inStream, null, options);
            // Rotate the bitmap
            Bitmap rotatedBitmap = Bitmap.createBitmap(originalBitmap, 0, 0, originalBitmap.getWidth(),
                    originalBitmap.getHeight(), matrix, true);
            // Save the rotated bitmap
            outStream = new FileOutputStream(outFile);
            rotatedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
            outStream.close();
            // Recycle the bitmaps to immediately free memory
            originalBitmap.recycle();
            originalBitmap = null;
            rotatedBitmap.recycle();
            rotatedBitmap = null;
            // Return
            return true;
        } catch (OutOfMemoryError e) {
            // If an OutOfMemoryError occurred, we continue with for loop and next inSampleSize value
        } finally {
            // Clean-up if we failed on save
            if (outStream != null) {
                try {
                    outStream.close();
                } catch (IOException e) {
                }
            }
        }
    }
    // Failed
    return false;
}

From source file:Main.java

private static Matrix getMatrix(int orientation) {
    Matrix matrix = new Matrix();
    if (orientation == 90 || orientation == 180 || orientation == 270) {
        matrix.postRotate(orientation);
    }/*  w ww  . j ava2  s  .c om*/
    return matrix;
}

From source file:Main.java

private static Bitmap rotateBitmap(Bitmap bitmap, int angle) {
    // If the rotate angle is 0, then return the original image, else return the rotated image
    if (angle != 0) {
        Matrix matrix = new Matrix();
        matrix.postRotate(angle);
        return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    } else {//w  w w .  j  a va  2  s .c o m
        return bitmap;
    }
}

From source file:Main.java

public static Bitmap rotateWithCanvas(Bitmap bitmap, int degrees) {

    int destWidth, destHeight;

    float centerX = bitmap.getWidth() / 2;
    float centerY = bitmap.getHeight() / 2;

    // We want to do the rotation at origin, but since the bounding
    // rectangle will be changed after rotation, so the delta values
    // are based on old & new width/height respectively.
    Matrix matrix = new Matrix();
    matrix.preTranslate(-centerX, -centerY);
    matrix.postRotate(degrees);
    if (degrees / 90 % 2 == 0) {
        destWidth = bitmap.getWidth();/*w w  w  .ja  va2 s .com*/
        destHeight = bitmap.getHeight();
        matrix.postTranslate(centerX, centerY);
    } else {
        destWidth = bitmap.getHeight();
        destHeight = bitmap.getWidth();
        matrix.postTranslate(centerY, centerX);
    }
    Bitmap cropped = Bitmap.createBitmap(destWidth, destHeight, Bitmap.Config.RGB_565);
    Canvas canvas = new Canvas(cropped);
    canvas.drawBitmap(bitmap, matrix, null);
    return cropped;
}

From source file:Main.java

private static Bitmap rotateBitmap(Bitmap bitmap, int rotate) {
    if (bitmap == null)
        return null;

    int w = bitmap.getWidth();
    int h = bitmap.getHeight();

    // Setting post rotate to 90
    Matrix mtx = new Matrix();
    mtx.postRotate(rotate);
    return Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, true);
}

From source file:Main.java

public static Bitmap rotateBitmap(Bitmap source, float angle) {
    Matrix matrix = new Matrix();
    matrix.postRotate(angle);
    return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
}

From source file:Main.java

public static Bitmap ScaleAndRotateBitmap(Bitmap bitmap, int rotation, int flipHorizontal) {
    Matrix m = new Matrix();
    if (flipHorizontal != 0) {
        m.postScale(-1, 1);/*from  www  .  j ava  2  s .  c o  m*/
    }
    if (rotation != 0) {
        m.postRotate(rotation);
    }
    Bitmap finalBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), m, true);
    if (finalBitmap != bitmap) {
        bitmap.recycle();
    }
    return finalBitmap;
}