Android Image Crop: Output x and Output y

Tags: android crop
Question!

I have implemented image crop intent with a specific output size. But changes done in the sizes on code doesn't effect in any way to the image and the output for each change remains the same.

 private void performCrop(String picUri) {
    try {
        //Start Crop Activity

        Intent cropIntent = new Intent("com.android.camera.action.CROP");
        // indicate image type and Uri
        File f = new File(picUri);
        Uri contentUri = Uri.fromFile(f);

        cropIntent.setDataAndType(contentUri, "image/*");
        // set crop properties
        cropIntent.putExtra("crop", "true");
        // indicate aspect of desired crop
        cropIntent.putExtra("aspectX", 1);
        cropIntent.putExtra("aspectY", 1);
        // indicate output X and Y
        cropIntent.putExtra("outputX", 400);
        cropIntent.putExtra("outputY", 400);

        // retrieve data on return
        cropIntent.putExtra("return-data", true);
        // start the activity - we handle returning in onActivityResult
        startActivityForResult(cropIntent, RESULT_CROP);
    }
    // respond to users whose devices do not support the crop action
    catch (ActivityNotFoundException anfe) {
        // display an error message
        String errorMessage = "your device doesn't support the crop action!";
        Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
        toast.show();
    }
}

Changes in the output x and y value doesn't effect the image size being cropped. Image size always remains 160*160. Jut when I change the aspect ratio the image size changes a bit. But i need to provide a specific size for x and y

And if there be any library regarding image crop and also supported on all device then it would awesome



Answers

Set the aspectX and aspectY in ratio of outputX and outputY.

Suppose you want the result image as 200 * 300 pixels. Then, simply set aspectX as 2 and aspectY as 3. You'll get the desired image dimension. Here is the updated code.

 private void performCrop(String picUri) {
    try {
        //Start Crop Activity

        Intent cropIntent = new Intent("com.android.camera.action.CROP");
        // indicate image type and Uri
        File f = new File(picUri);
        Uri contentUri = Uri.fromFile(f);

        cropIntent.setDataAndType(contentUri, "image/*");
        // set crop properties
        cropIntent.putExtra("crop", "true");
        // indicate aspect of desired crop
        cropIntent.putExtra("aspectX", 2);
        cropIntent.putExtra("aspectY", 3);
        // indicate output X and Y
        cropIntent.putExtra("outputX", 200);
        cropIntent.putExtra("outputY", 300);

        // retrieve data on return
        cropIntent.putExtra("return-data", true);
        // start the activity - we handle returning in onActivityResult
        startActivityForResult(cropIntent, RESULT_CROP);
    }
    // respond to users whose devices do not support the crop action
    catch (ActivityNotFoundException anfe) {
        // display an error message
        String errorMessage = "your device doesn't support the crop action!";
        Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
        toast.show();
    }
    }


This video can help you solving your question :)
By: admin