Easy-to-use open-source Library (II)-uCrop image cropping and open-source ucrop

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.