1/* 2 * Copyright (C) 2009 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.camera; 18 19import android.graphics.Bitmap; 20import android.graphics.Matrix; 21 22public class RotateBitmap { 23 public static final String TAG = "RotateBitmap"; 24 private Bitmap mBitmap; 25 private int mRotation; 26 27 public RotateBitmap(Bitmap bitmap) { 28 mBitmap = bitmap; 29 mRotation = 0; 30 } 31 32 public RotateBitmap(Bitmap bitmap, int rotation) { 33 mBitmap = bitmap; 34 mRotation = rotation % 360; 35 } 36 37 public void setRotation(int rotation) { 38 mRotation = rotation; 39 } 40 41 public int getRotation() { 42 return mRotation; 43 } 44 45 public Bitmap getBitmap() { 46 return mBitmap; 47 } 48 49 public void setBitmap(Bitmap bitmap) { 50 mBitmap = bitmap; 51 } 52 53 public Matrix getRotateMatrix() { 54 // By default this is an identity matrix. 55 Matrix matrix = new Matrix(); 56 if (mRotation != 0) { 57 // We want to do the rotation at origin, but since the bounding 58 // rectangle will be changed after rotation, so the delta values 59 // are based on old & new width/height respectively. 60 int cx = mBitmap.getWidth() / 2; 61 int cy = mBitmap.getHeight() / 2; 62 matrix.preTranslate(-cx, -cy); 63 matrix.postRotate(mRotation); 64 matrix.postTranslate(getWidth() / 2, getHeight() / 2); 65 } 66 return matrix; 67 } 68 69 public boolean isOrientationChanged() { 70 return (mRotation / 90) % 2 != 0; 71 } 72 73 public int getHeight() { 74 if (isOrientationChanged()) { 75 return mBitmap.getWidth(); 76 } else { 77 return mBitmap.getHeight(); 78 } 79 } 80 81 public int getWidth() { 82 if (isOrientationChanged()) { 83 return mBitmap.getHeight(); 84 } else { 85 return mBitmap.getWidth(); 86 } 87 } 88 89 public void recycle() { 90 if (mBitmap != null) { 91 mBitmap.recycle(); 92 mBitmap = null; 93 } 94 } 95} 96 97