Crops the requested bitmap to a square. - Android Graphics

Android examples for Graphics:Bitmap Crop

Description

Crops the requested bitmap to a square.

Demo Code


//package com.java2s;

import android.graphics.Bitmap;

import android.media.ThumbnailUtils;

public class Main {
    /**//from   w ww.  j a v  a2 s .  c  o m
     * Crops the requested bitmap to a square. Chops the longer dimension off to match the shorter.
     * @param bitmap the bitmap to be cropped
     * @return the cropped bitmap
     */
    public static Bitmap cropBitmapToSquare(Bitmap bitmap) {
        return ThumbnailUtils.extractThumbnail(bitmap,
                getCropDimensions(bitmap), getCropDimensions(bitmap));
    }

    /**
     * Returns the shorter of the two bitmap dimensions to use for square-cropping.
     * @param bitmap the bitmap to be cropped
     * @return the required dimension
     */
    public static int getCropDimensions(Bitmap bitmap) {
        if (bitmap.getWidth() >= bitmap.getHeight()) {
            return bitmap.getHeight();
        } else {
            return bitmap.getWidth();
        }
    }
}

Related Tutorials