Easiest Android Image Cropper Tool

Hello !

Welcome Back Guys 

Today I am Going To Tell You About an Amazing Image Crop Tool For Your Amazing Android App
So Lets Start…
So Guys this is very useful tool u can use it to crop Your Image in any aspect or rotate your image either picking from gallery or by clicking through camera
In this Tutorial we are gonna crop image in square Ratio

create a new Project in Android Studio 
Add this dependency to your app level gradle :
 compile 'com.theartofdev.edmodo:android-image-cropper:2.3.0'  

now add storage permissions in android manifest

 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>  

Now lets code Our layout

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.cursedcompany.androidclassroomimagecrop.MainActivity">
<ImageView
android:layout_centerHorizontal="true"
android:layout_width="250dp"
android:layout_height="250dp"
android:src="@mipmap/ic_launcher"
android:id="@+id/image_view"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:id="@+id/button"
android:layout_margin="10dp"
android:text="Click To select Image"/>
</RelativeLayout>

Now lets code our MainActivity and use Android Image Cropper

 public class MainActivity extends AppCompatActivity {  
private int REQ_CODE = 1;
private ImageView mImageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mImageView = (ImageView) findViewById(R.id.image_view);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, REQ_CODE);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQ_CODE && resultCode == RESULT_OK) {
Uri tempUri = data.getData();
CropImage.activity(tempUri)
.setGuidelines(CropImageView.Guidelines.ON)
.setAspectRatio(1, 1)
.start(this);
Log.d("TAG", "MUR: result worked");
}
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE){
CropImage.ActivityResult result = CropImage.getActivityResult(data);
       //Uri Of Cropped Image:
Uri imageUri = result.getUri();
       mImageView.setImageURI(imageUri);  
     }  
}
}

So what we did is we get the URI of image through intent on onActivityResult we gave the Uri To Android Cropper by Calling Method:

CropImage.activity(Uri imageUri)  

To add Guidelines we call the Method :

.setGuidelines(CropImageView.Guidelines.ON) 

 To Crop the Image In Square Ratio we Call the Method and gave the ratio 1 : 1:

.setAspectRatio(int i, int j) 

To Execute the Cropping Process We Call the Method:

  .start(this);  

Now The Cropping Is done. Its Time To get the result:

To get The Cropped image URI we created the Instance of CropImage.ActivityResult  :

  CropImage.ActivityResult result = CropImage.getActivityResult(Intent data);  

here it requires an Intent data which we got on OnActivityResult

Now to get the Result In Uri We can simply get from Our CropImage.ActivityResult  instance :-

Uri imageUri = result.getUri();

And Its Over now we can use this URI to set Our Views or upload in server…

For More Information about Android Image Cropper Check Out their Website:
https://arthurhub.github.io/Android-Image-Cropper

Thank You Guys For Reading My Blog I will Be Uploading More Cool Android Programming Posts Keep Checking…..

SCREEN 1
SCREEN 2
SCREEN 3

4 thoughts on “Easiest Android Image Cropper Tool”

Leave a Reply

Your email address will not be published. Required fields are marked *