crop Bitmap Image To Frame - Android Graphics

Android examples for Graphics:Bitmap Crop

Description

crop Bitmap Image To Frame

Demo Code


//package com.java2s;

import android.graphics.Bitmap;

import android.graphics.Canvas;

import android.graphics.Matrix;
import android.graphics.Paint;

import android.graphics.Bitmap.Config;

import android.graphics.drawable.BitmapDrawable;

public class Main {
    public static Bitmap cropToFrame(final Bitmap src,
            final int frameWidth, final int frameHeight) {
        final int imageWidth = src.getWidth();
        final int imageHeight = src.getHeight();

        float tempHeight = (float) imageWidth / frameWidth * frameHeight;
        Bitmap frameBitmap = null;//from  w  w  w  .j a  v  a  2 s.c  o  m
        // TH1
        if (tempHeight > imageHeight) {
            float scale = ((float) frameHeight) / imageHeight;
            Bitmap scaledBitmap = scaleAndRotate(src, scale, 0).getBitmap();
            frameBitmap = crop(scaledBitmap, frameWidth, frameHeight);
            scaledBitmap.recycle();
        } else if (tempHeight < imageHeight) {
            float scale = ((float) frameWidth) / imageWidth;
            Bitmap scaledBitmap = scaleAndRotate(src, scale, 0).getBitmap();
            frameBitmap = crop(scaledBitmap, frameWidth, frameHeight);
            scaledBitmap.recycle();
        } else {
            float scale = ((float) frameWidth) / imageWidth;
            frameBitmap = scaleAndRotate(src, scale, 0).getBitmap();
        }
        return frameBitmap;
    }

    public static BitmapDrawable scaleAndRotate(Bitmap bitmap,
            int newWidth, int newHeight, float degrees) {
        // load the origial BitMap (500 x 500 px)
        Bitmap bitmapOrg = bitmap;

        int width = bitmapOrg.getWidth();
        int height = bitmapOrg.getHeight();
        // int newWidth = 200;
        // int newHeight = 200;

        // calculate the scale - in this case = 0.4f
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;

        // createa matrix for the manipulation
        Matrix matrix = new Matrix();
        // resize the bit map
        matrix.postScale(scaleWidth, scaleHeight);
        // rotate the Bitmap
        matrix.postRotate(degrees);

        // recreate the new Bitmap
        Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, width,
                height, matrix, true);

        BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);
        return bmd;
    }

    public static BitmapDrawable scaleAndRotate(Bitmap bitmap, float scale,
            float degrees) {
        // load the origial BitMap (500 x 500 px)
        Bitmap bitmapOrg = bitmap;
        int width = bitmapOrg.getWidth();
        int height = bitmapOrg.getHeight();

        float scaleWidth = ((float) scale) * width;
        float scaleHeight = ((float) scale) * height;
        // createa matrix for the manipulation
        Matrix matrix = new Matrix();
        // resize the bit map
        matrix.postScale(scale, scale);
        // rotate the Bitmap
        matrix.postRotate(degrees);

        // recreate the new Bitmap
        Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, width,
                height, matrix, true);

        BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);
        return bmd;
    }

    public static Bitmap crop(Bitmap source, int left, int top, int width,
            int height) {
        Bitmap cropBitmap = Bitmap.createBitmap(width, height,
                Config.ARGB_4444);
        Canvas canvas = new Canvas(cropBitmap);
        canvas.drawBitmap(source, -left, -top, new Paint());
        return cropBitmap;
    }

    public static Bitmap crop(Bitmap src, int width, int height) {
        int left = (int) ((src.getWidth() - width) / 2f);
        int top = (int) ((src.getHeight() - height) / 2f);
        return crop(src, left, top, width, height);
    }
}

Related Tutorials