compress Rotate Bitmap - Android Graphics

Android examples for Graphics:Bitmap Rotate

Description

compress Rotate Bitmap

Demo Code


//package com.book2s;

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 compressRotateBitmap(String picPath) {
        Bitmap bitmap = null;/* www  . j a  va 2s.  c om*/
        int degree = readPictureDegree(picPath);
        if (degree == 90) {
            bitmap = featBitmapToSuitable(picPath, 500, 1.8f);
            bitmap = rotate(bitmap, 90);
        } else {
            bitmap = featBitmapToSuitable(picPath, 500, 1.8f);
        }
        return bitmap;
    }

    public static int readPictureDegree(String path) {
        int degree = 0;
        try {
            ExifInterface exifInterface = new ExifInterface(path);
            int orientation = exifInterface.getAttributeInt(
                    ExifInterface.TAG_ORIENTATION,
                    ExifInterface.ORIENTATION_NORMAL);
            switch (orientation) {
            case ExifInterface.ORIENTATION_ROTATE_90:
                degree = 90;
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                degree = 180;
                break;
            case ExifInterface.ORIENTATION_ROTATE_270:
                degree = 270;
                break;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return degree;
    }

    public static Bitmap featBitmapToSuitable(String path,
            int suitableSize, float factor) {
        Bitmap bitmap = null;
        try {
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(path, options);
            options.inJustDecodeBounds = false;
            options.inSampleSize = 1;
            options.inPreferredConfig = Bitmap.Config.RGB_565;
            options.inPurgeable = true;
            options.inInputShareable = true;
            int bitmap_w = options.outWidth;
            int bitmap_h = options.outHeight;
            int max_edge = bitmap_w > bitmap_h ? bitmap_w : bitmap_h;
            while (max_edge / (float) suitableSize > factor) {
                options.inSampleSize <<= 1;
                max_edge >>= 1;
            }
            return BitmapFactory.decodeFile(path, options);
        } catch (Exception e) {
        }
        return bitmap;
    }

    public static Bitmap rotate(Bitmap b, int degrees) {
        if (degrees != 0 && b != null) {
            Matrix m = new Matrix();
            m.setRotate(degrees);
            try {
                Bitmap b2 = Bitmap.createBitmap(b, 0, 0, b.getWidth(),
                        b.getHeight(), m, true);
                if (null != b2 && b != b2) {
                    b.recycle();
                    b = b2;
                }
            } catch (OutOfMemoryError ex) {
                // We have no memory to rotate. Return the original bitmap.
            }
        }
        return b;
    }
}

Related Tutorials