Code example for Matrix

Methods: mapRect, postScale, postTranslate, setRotate

0
     * @param pool A pool that may or may not contain an image of the necessary dimensions 
     * @param exifOrientation the exif orientation [1-8] 
     * @return The rotated and/or flipped image or toOrient if no rotation or flip was necessary 
     */ 
    public static Bitmap rotateImageExif(Bitmap toOrient, BitmapPool pool, int exifOrientation) {
        final Matrix matrix = new Matrix();
        boolean swapWidthHeight = false;
        switch (exifOrientation) {
            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:
                swapWidthHeight = true;
                matrix.setRotate(90);
                matrix.postScale(-1, 1);
                break; 
            case ExifInterface.ORIENTATION_ROTATE_90:
                swapWidthHeight = true;
                matrix.setRotate(90);
                //matrix.postTranslate(toOrient.getHeight(), 0); 
                break; 
            case ExifInterface.ORIENTATION_TRANSVERSE:
                swapWidthHeight = true;
                matrix.setRotate(-90);
                matrix.postScale(-1, 1);
                break; 
            case ExifInterface.ORIENTATION_ROTATE_270:
                swapWidthHeight = true;
                matrix.setRotate(-90);
                break; 
            default: //case ExifInterface.ORIENTATION_NORMAL 
                return toOrient;
        } 
 
        //from Bitmap.createBitmap 
        final RectF newRect = new RectF(0, 0, toOrient.getWidth(), toOrient.getHeight());
        matrix.mapRect(newRect);
 
        final int newWidth = Math.round(newRect.width());
        final int newHeight = Math.round(newRect.height());
 
        Bitmap result = pool.get(newWidth, newHeight);
        if (result == null) {
            result = Bitmap.createBitmap(newWidth, newHeight, toOrient.getConfig());
        } 
 
        matrix.postTranslate(-newRect.left, -newRect.top);
 
        final Canvas canvas = new Canvas(result);
        final Paint paint = new Paint();
        paint.setFilterBitmap(true);
        paint.setAntiAlias(true);