rotate And Scale Bitmap Image - Android Graphics

Android examples for Graphics:Bitmap Scale

Description

rotate And Scale Bitmap Image

Demo Code


import android.app.Activity;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;
import java.io.*;

public class Main{
    public static Bitmap rotateAndScale(Bitmap b, int degrees,
            float maxSideLen) {

        return rotateAndScale(b, degrees, maxSideLen, true);
    }//from   w  w w  .java2s .co  m
    public static Bitmap rotateAndScale(Bitmap b, int degrees,
            float maxSideLen, boolean recycle) {
        if (null == b || degrees == 0 && b.getWidth() <= maxSideLen + 10
                && b.getHeight() <= maxSideLen + 10) {
            return b;
        }

        Matrix m = new Matrix();
        if (degrees != 0) {
            m.setRotate(degrees);
        }

        float scale = Math.min(maxSideLen / b.getWidth(),
                maxSideLen / b.getHeight());
        if (scale < 1) {
            m.postScale(scale, scale);
        }
        Debug.debug("degrees: " + degrees + ", scale: " + scale);

        try {
            Bitmap b2 = Bitmap.createBitmap(b, 0, 0, b.getWidth(),
                    b.getHeight(), m, true);
            if (null != b2 && b != b2) {
                if (recycle) {
                    b.recycle();
                }
                b = b2;
            }
        } catch (OutOfMemoryError e) {
        }

        return b;
    }
}

Related Tutorials