crop Image via Intent - Android Graphics

Android examples for Graphics:Image Intent

Description

crop Image via Intent

Demo Code


import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.provider.MediaStore;

public class Main{
    /*from  ww w .  j a va  2s .  c o m*/
    public static final int REQUEST_CODE_CROP = REQUEST_CODE_TAKE_PHOTO + 5;
    public static void cropImage(Uri uri, int width, int height,
            Activity activity) {
        Intent intent = cropImageIntent(uri, width, height);
        activity.startActivityForResult(intent, REQUEST_CODE_CROP);
    }
    public static void cropImage(Uri uri, int width, int height,
            android.support.v4.app.Fragment fragment) {
        Intent intent = cropImageIntent(uri, width, height);
        fragment.startActivityForResult(intent, REQUEST_CODE_CROP);
    }
    private static Intent cropImageIntent(Uri uri, int width, int height) {
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "image/*");
        intent.putExtra("crop", "true");

        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);

        intent.putExtra("outputX", width);
        intent.putExtra("outputY", height);
        intent.putExtra("return-data", true);
        intent.putExtra("outputFormat",
                Bitmap.CompressFormat.JPEG.toString());

        intent.putExtra("noFaceDetection", true);
        return intent;
    }
    private static Intent cropImageIntent(Uri uri, Uri outputUri,
            int width, int height) {
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "image/*");
        intent.putExtra(MediaStore.EXTRA_OUTPUT, outputUri);
        intent.putExtra("crop", "true");

        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);

        intent.putExtra("outputX", width);
        intent.putExtra("outputY", height);
        intent.putExtra("return-data", false);
        intent.putExtra("outputFormat",
                Bitmap.CompressFormat.JPEG.toString());
        intent.putExtra("noFaceDetection", true);
        return intent;
    }
}

Related Tutorials