Easy-to-use open-source Library (II)-uCrop image cropping and open-source ucrop
Recently, I found this uCrop on Github to implement the image cropping function. Many people in star decided to get into the trap. As a result, I spent an hour reading materials and exploring, finally, the image cropping function is implemented in the project. Today we will introduce how to use uCrop.
UCrop
Directory: configuration 1. Add dependency
maven { url "https://jitpack.io" }
compile 'com.github.yalantis:ucrop:2.2.1'
2. Configure the Androidmanifest File
<activity android:name="com.yalantis.ucrop.UCropActivity" android:screenOrientation="portrait"/>
ScreenOrientation is the screen direction. Because it is used on the mobile phone, it is set to the vertical direction.
3. Declaration of Read and Write Permissions
PS: here the Write permission actually includes the read and Write permissions, so we only need to declare one. It is no problem to declare more Write permissions.
Brief process introduction:
Before using this function, let's take a look at the image cutting process.
First, the Ucrop constructor has two parameters: the Uri of the image to be cropped and the cropped Uri. The cropped Uri needs to be created, and then the Ucrop is set, such as the width and height of the cropping box, setting the number of vertical lines of the cropping box or the format of the output image, etc.
After the setting is complete, you can enter the Activity to be cropped to perform the cropping operation. After the cropping is completed, a return code is displayed. we can intercept the Activity in the onActivityResult method to obtain the cropped Uri, then we can use this Uri to obtain the cropped image.
Use: 1. Create an output Uri and mDestinationUri2. create a Ucrop class.
The parameter specifies the Uri of the image to be cropped.
3. Capture the output Uri of the cropped image in onActivityResult.
4. process images
Here, we use the toString method of Uri to convert Uri to String.
5. display images
Here I/O Stream is used. For details, refer to the Code for understanding. (actually, you can directly use the Uri YOU JUST OBTAINED. You do not need to convert it to the String type)
Possible problems: the application is stopped:
Cause: the actionbar is used for the current Activity that uses cropping. In the Androidmanifest file, change the theme of the activity to Noactionbar.