The recent use of a week or so of spare time, finally completed an Android image clipping library, the core function is based on their own understanding of the implementation, part of the code reference to the Android source image clipping application. Now open the code on GitHub for everyone to learn and use, Address: Https://github.com/Jhuster/ImageCropper, the effect is as follows:
My general plan is to first introduce the use of this library, and then write a few articles about some of the principles and key technologies, hoping to help the Android developers.
Characteristics
Support for moving and zooming the trim window through gestures
Supports fixed clipping window size, fixed window length-to-width ratio
Supports setting the maximum window length and width
Supports the rotation of cropped pictures
Easy to integrate and use
"How to use"
Modify the Androidmanifest.xml file
<activity android:name="com.ticktick.imagecropper.CropImageActivity"/>
Permission to add write SDcard
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
2. How to start the picture clipping interface
The first method uses the encapsulated cropintent in the library to build the intent object:
Private voidStartcropimage () {//Create a cropintentCropintent Intent =Newcropintent (); //Set The source image Filepath/url and Output Filepath/url (Required)Intent.setimagepath ("/sdcard/source.jpg"); Intent.setoutputpath ("/sdcard/cropped.jpg"); //Set A fixed crop window size (Optional)Intent.setoutputsize (640,480); //Set the max crop window size (Optional)Intent.setmaxoutputsize ( -, -); //Set a fixed crop window ' s width/height aspect (Optional)Intent.setaspect (3,2); //Start Imagecropper activity with certain request code and listen for resultStartactivityforresult (Intent.getintent ( This), request_code_crop_picture);}
The second method, the custom intent object:
Private voidStartcropimage () {//Create Explicit IntentIntent Intent =NewIntent ( This, Cropimageactivity.class); //Set The source image Filepath/url and Output Filepath/url (Required)Intent.setdata (Uri.fromfile (NewFile ("/sdcard/source.jpg"))); Intent.putextra (Mediastore.extra_output,uri.fromfile (NewFile ("/sdcard/cropped.jpg"))); //Set A fixed crop window size (Optional)Intent.putextra ("OUTPUTX",640); Intent.putextra ("outputy",480); //Set the max crop window size (Optional)Intent.putextra ("MAXOUTPUTX", -); Intent.putextra ("maxoutputy", -); //Set a fixed crop window ' s width/height aspect (Optional)Intent.putextra ("Aspectx",3); Intent.putextra ("aspecty",2); //Start Imagecropper activity with certain request code and listen for resultStartactivityforresult (Intent, request_code_crop_picture);}
3. Get clipping results
After clipping, if the user clicks on the "Save" button, you can get the URL of the saved picture via Mediastore.extra_output, and if the user clicks "Cancel", the activity's return value will be set to Result_ CANCEL
protected voidOnactivityresult (intRequestcode,intResultCode, Intent data) { if(ResultCode! =RESULT_OK) { return; } if(Requestcode = =request_code_crop_picture) {Uri Croppeduri=Data.getextras (). getparcelable (Mediastore.extra_output); InputStreaminch=NULL; Try { inch=getcontentresolver (). Openinputstream (Croppeduri); Bitmap b= Bitmapfactory.decodestream (inch); Mimageview.setimagebitmap (b); } Catch(FileNotFoundException e) {e.printstacktrace (); }} super.onactivityresult (Requestcode, ResultCode, data);}
Android Picture Clipping Gallery