crop Bitmap - Android android.graphics

Android examples for android.graphics:Bitmap Operation

Description

crop Bitmap

Demo Code

import android.graphics.Bitmap;
import android.graphics.Matrix;

public class Main{

    public static Bitmap cropBitmap(Bitmap bitmap, float rateF) {
        Bitmap bitmapCropped = null;/*from w  ww.  j  a  v a  2s  .  co  m*/
        float w = bitmap.getWidth();
        float h = bitmap.getHeight();
        float rate_original = w / h;
        if (rateF < rate_original) {
            float w_new = h * rateF;
            float x_offset = (int) ((w - w_new) / 2);
            bitmapCropped = Bitmap.createBitmap(bitmap, (int) x_offset, 0,
                    (int) w_new, (int) h);
        } else if (rateF > rate_original) {
            float h_new = w / rateF;
            float h_offset = (int) ((h - h_new) / 2);
            bitmapCropped = Bitmap.createBitmap(bitmap, 0, (int) h_offset,
                    (int) w, (int) h_new);
        } else
            bitmapCropped = bitmap;
        return bitmapCropped;
    }

}

Related Tutorials