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 getDesirableBitmap(String imgPath, int desiredSize) {
    try {//w w w  . j a  va  2  s  .c om
        int scale = getDesirableBitmapSampleSize(imgPath, desiredSize);
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = scale;
        Bitmap originalBitmap = BitmapFactory.decodeFile(imgPath, options);

        int rotation = getExifOrientation(imgPath);
        if (rotation != 0) {
            Matrix matrix = new Matrix();
            matrix.postRotate(rotation);
            Bitmap exifBitmap = Bitmap.createBitmap(originalBitmap, 0, 0, originalBitmap.getWidth(),
                    originalBitmap.getHeight(), matrix, true);
            originalBitmap.recycle();
            return exifBitmap;
        }
        return originalBitmap;
    } catch (Exception ex) {
        ex.printStackTrace();
        //TODO: Handle
    } catch (OutOfMemoryError e) {
        e.printStackTrace();
        //Hmmm...what to do...
    }
    return null;
}

From source file:Main.java

public static Bitmap getResizedBitmap(Bitmap src, int targetWidth, int targetHeight, float degrees) {
    int srcWidth = src.getWidth();
    int srcHeight = src.getHeight();

    float scale = getFitScale(targetWidth, targetHeight, srcWidth, srcHeight);

    Matrix matrix = new Matrix();
    matrix.postScale(scale, scale);//www.  ja  v a  2s.c  o  m
    matrix.postRotate(degrees);

    return Bitmap.createBitmap(src, 0, 0, srcWidth, srcHeight, matrix, true);
}

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 Matrix getRotationMatrixForImage(Context ctx, Uri contentUri) {

    String pathToImage = getAbsolutePathFromUri(ctx, contentUri);
    ExifInterface exif;/* www .  j a v  a 2  s. c  o m*/
    try {
        exif = new ExifInterface(pathToImage);
        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
        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);
        }
        return matrix;
    } catch (IOException e) {
        return new Matrix();
    }
}

From source file:Main.java

/**
 * To avoid problems with rotated videos retrieved from camera
 * @param bitmap/*from   ww w  . ja v a2s  .com*/
 * @param filePath
 * @return
 */
public static Bitmap rotateImage(Bitmap bitmap, String filePath) {
    Bitmap resultBitmap = bitmap;

    try {
        ExifInterface exifInterface = new ExifInterface(filePath);
        int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);

        Matrix matrix = new Matrix();

        if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
            matrix.postRotate(ExifInterface.ORIENTATION_ROTATE_90);
        } else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
            matrix.postRotate(ExifInterface.ORIENTATION_ROTATE_180);
        } else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
            matrix.postRotate(ExifInterface.ORIENTATION_ROTATE_270);
        }

        // Rotate the bitmap
        resultBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    } catch (Exception exception) {
        Log.d("AndroidTouchGallery", "Could not rotate the image");
    }
    return resultBitmap;
}

From source file:Main.java

public static Bitmap rotateBitmap(Bitmap src, int degree) {
    Bitmap ret = null;//from   w  w  w  .j a  v a  2s .com
    Matrix matrix = new Matrix();
    matrix.postRotate(degree);
    try {
        ret = Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), matrix, true);
    } catch (OutOfMemoryError ignore) {
    }
    if (ret == null) {
        ret = src;
    }
    if (src != ret) {
        src.recycle();
    }
    return ret;
}

From source file:Main.java

/**
 * Shrinks and rotates (if necessary) a passed Bitmap.
 *
 * @param bm//from w ww. j a  v  a 2 s  .  c o m
 * @param maxLengthOfEdge
 * @param rotateXDegree
 * @return Bitmap
 */
public static Bitmap shrinkBitmap(Bitmap bm, int maxLengthOfEdge, int rotateXDegree) {
    if (maxLengthOfEdge > bm.getWidth() && maxLengthOfEdge > bm.getHeight()) {
        return bm;
    } else {
        // shrink image
        float scale = (float) 1.0;
        if (bm.getHeight() > bm.getWidth()) {
            scale = ((float) maxLengthOfEdge) / bm.getHeight();
        } else {
            scale = ((float) maxLengthOfEdge) / bm.getWidth();
        }
        // CREATE CommonAsync MATRIX FOR THE MANIPULATION
        Matrix matrix = new Matrix();
        // RESIZE THE BIT MAP
        matrix.postScale(scale, scale);
        matrix.postRotate(rotateXDegree);

        // RECREATE THE NEW BITMAP
        bm = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, false);

        matrix = null;
        System.gc();

        return bm;
    }
}

From source file:Main.java

public static boolean storeImage(Context context, Bitmap bmp, boolean isRotate) {

    // use the current data&time for image file name
    String takenTime_YYMMDD_HHMMSS = new SimpleDateFormat(DATA_FORMAT).format(new Date());

    // saved bitmap: full path
    String path = PIC_ROOT_PATH + takenTime_YYMMDD_HHMMSS;
    File f = new File(path);

    if (f != null && !f.getParentFile().exists()) {
        f.getParentFile().mkdirs();// w w w  .  j  av a  2 s . co m
    }

    if (isRotate) {
        Matrix matrix = new Matrix();
        matrix.reset();
        matrix.postRotate(90);
        bmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);
    }

    try {
        FileOutputStream out = new FileOutputStream(f);
        bmp.compress(Bitmap.CompressFormat.JPEG, 100, out);
        out.flush();
        out.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        return false;
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }

    return updateGallery(context, bmp, takenTime_YYMMDD_HHMMSS);
}

From source file:Main.java

private static Bitmap setProperOrientation(int orientation, Bitmap srcBitmap) {
    if (orientation > 0) {
        Matrix matrix = new Matrix();
        matrix.postRotate(orientation);

        srcBitmap = Bitmap.createBitmap(srcBitmap, 0, 0, srcBitmap.getWidth(), srcBitmap.getHeight(), matrix,
                true);/*from  www  .  j a v a  2s.c  o m*/
    }
    return srcBitmap;
}

From source file:Main.java

public static Bitmap rotateBitmap(String path, int orientation, int screenWidth, int screenHeight) {
    Bitmap bitmap = null;/*from  w  ww.j  a  v a  2  s.c om*/
    final int maxWidth = screenWidth / 2;
    final int maxHeight = screenHeight / 2;
    try {
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path, options);
        int sourceWidth, sourceHeight;
        if (orientation == 90 || orientation == 270) {
            sourceWidth = options.outHeight;
            sourceHeight = options.outWidth;
        } else {
            sourceWidth = options.outWidth;
            sourceHeight = options.outHeight;
        }
        boolean compress = false;
        if (sourceWidth > maxWidth || sourceHeight > maxHeight) {
            float widthRatio = (float) sourceWidth / (float) maxWidth;
            float heightRatio = (float) sourceHeight / (float) maxHeight;

            options.inJustDecodeBounds = false;
            if (new File(path).length() > 512000) {
                float maxRatio = Math.max(widthRatio, heightRatio);
                options.inSampleSize = (int) maxRatio;
                compress = true;
            }
            bitmap = BitmapFactory.decodeFile(path, options);
        } else {
            bitmap = BitmapFactory.decodeFile(path);
        }
        if (orientation > 0) {
            Matrix matrix = new Matrix();
            //matrix.postScale(sourceWidth, sourceHeight);
            matrix.postRotate(orientation);
            bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
        }
        sourceWidth = bitmap.getWidth();
        sourceHeight = bitmap.getHeight();
        if ((sourceWidth > maxWidth || sourceHeight > maxHeight) && compress) {
            float widthRatio = (float) sourceWidth / (float) maxWidth;
            float heightRatio = (float) sourceHeight / (float) maxHeight;
            float maxRatio = Math.max(widthRatio, heightRatio);
            sourceWidth = (int) ((float) sourceWidth / maxRatio);
            sourceHeight = (int) ((float) sourceHeight / maxRatio);
            Bitmap bm = Bitmap.createScaledBitmap(bitmap, sourceWidth, sourceHeight, true);
            bitmap.recycle();
            return bm;
        }
    } catch (Exception e) {
    }
    return bitmap;
}