Java tutorial
//package com.java2s; /* * COPYRIGHT NOTICE * Copyright (C) 2015, Jhuster <lujun.hust@gmail.com> * https://github.com/Jhuster/Android * * @license under the Apache License, Version 2.0 * * @file BitmapHelper.java * @brief Bitmap processor, include load,save,rotate,crop etc. * * @version 1.0 * @author Jhuster * @date 2015/11/20 */ import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Matrix; public class Main { 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(); 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; } }