crop Image by Rect - Android Graphics

Android examples for Graphics:Image Crop

Description

crop Image by Rect

Demo Code


//package com.java2s;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapRegionDecoder;

import android.graphics.Point;
import android.graphics.Rect;

public class Main {
    public static void cropImage(File original, File output,
            Rect imageRect, Rect cropRect) throws FileNotFoundException,
            IOException {//from   ww w .jav a 2  s .  c om
        Point dimen = getImageDimensions(original);
        float scaledW = imageRect.right - imageRect.left;
        float scaledH = imageRect.bottom - imageRect.top;
        float scale = Math.min((float) dimen.x / scaledW, (float) dimen.y
                / scaledH);

        Rect cropRegion = getIntrinsicCropRegion(dimen, cropRect, scale);
        BitmapRegionDecoder decoder = BitmapRegionDecoder.newInstance(
                original.getPath(), false);
        Bitmap bitmap = decoder.decodeRegion(cropRegion, null);
        saveBitmap(output, bitmap);
    }

    public static Point getImageDimensions(File f)
            throws FileNotFoundException {
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(new FileInputStream(f), null, o);
        return new Point(o.outWidth, o.outHeight);
    }

    private static Rect getIntrinsicCropRegion(Point imageDimen,
            Rect cropRect, float scale) {
        Rect cropRegion = new Rect();
        cropRegion.left = (int) (cropRect.left * scale);
        cropRegion.top = (int) (cropRect.top * scale);
        cropRegion.right = (int) (cropRect.right * scale + cropRegion.left);
        cropRegion.bottom = (int) (cropRect.bottom * scale + cropRegion.top);
        checkBoundaryCondition(imageDimen, cropRegion);
        return cropRegion;
    }

    public static void saveBitmap(File output, Bitmap bitmap)
            throws FileNotFoundException, IOException {
        FileOutputStream fos = new FileOutputStream(output);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 80, fos);
        fos.flush();
        fos.close();
        bitmap.recycle();
        bitmap = null;
    }

    private static void checkBoundaryCondition(Point imageDimen,
            Rect cropRegion) {
        checkVerticalBoundary(imageDimen, cropRegion);
        checkHorizontalBoundary(imageDimen, cropRegion);
    }

    private static void checkVerticalBoundary(Point imageDimen,
            Rect cropRegion) {
        if (cropRegion.top < 0) {
            cropRegion.bottom -= cropRegion.top;
            cropRegion.top = 0;
        } else if (imageDimen.y < cropRegion.bottom) {
            int diff = cropRegion.bottom - imageDimen.y;
            cropRegion.top -= diff;
            cropRegion.bottom -= diff;
        }
    }

    private static void checkHorizontalBoundary(Point imageDimen,
            Rect cropRegion) {
        if (cropRegion.left < 0) {
            cropRegion.right -= cropRegion.left;
            cropRegion.left = 0;
        } else if (imageDimen.x < cropRegion.right) {
            int diff = cropRegion.right - imageDimen.x;
            cropRegion.left -= diff;
            cropRegion.right -= diff;
        }
    }
}

Related Tutorials