简体   繁体   中英

Fix square for image cropping in Android

I am working on an Android application in which I want to fix the ratio of square used for cropping. My code is given below:

public void cropCapturedImage(Uri picUri){
        //call the standard crop action intent 
        Intent cropIntent = new Intent("com.android.camera.action.CROP");
        //indicate image type and Uri of image
        cropIntent.setDataAndType(picUri, "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", 512);
        cropIntent.putExtra("outputY", 512);
        //retrieve data on return
        cropIntent.putExtra("return-data", true);
        //start the activity - we handle returning in onActivityResult
        startActivityForResult(cropIntent, 2);
    }

Android does not have CROP Intent . There is no requirement that any device have an app that honors your undocumented and unsupported Intent action, let alone that particular mix of extras.

As we discussed previously , there are many image cropping libraries available for Android. At least one supports square cropping, and there is at least one fork of that library that offers fixed control over the size of the crop area. Others of the available image cropping libraries may offer similar features, and if you do not like any of those, you are welcome to fork one of those libraries or write your own from scratch.

Also, bear in mind that not all devices have 512 pixels along both axes, and so not users may not be able to crop a 512x512 image very readily.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM