Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Crop image in android

I want to do cropping of image i found some pretty useful ones but somehow is like lacking of the darken the unselected areas so I wondering do anyone know how? or lead me to the right direction? The online tutorial i found shows that is will darken the selected area but when I use it, it won't. Please help me thanks alot and sorry for my bad command of english.

Links to the tutorial I use.

Crop image tutorial 1

Crop Image tutorial 2

I want it to be something like this.

I want it be something like this

editButton.setOnClickListener(new Button.OnClickListener(){          @Override         public void onClick(View v) {             // TODO Auto-generated method stub             Intent goEdit;             goEdit = new Intent(PreviewActivity.this, CropImage.class);             goEdit.putExtra("image-path", path);             goEdit.putExtra("scale", true);             goEdit.putExtra("fileName", nameFromPath);             //finish();             checkEdit = true;             startActivityForResult(goEdit,0);          } }); 

EDIT I use this button listener to call into the cropImage file by calling to the class CropImage activity. This is a custom intent not the crop feature inside android but I think is the copy of it so that make it support for all versions but when I call into it the selected area isnt brighten and I donno where is the problem can anyone guide me? Thanks This is the library I'm using drioid4you crop image

like image 393
I Yeu C Avatar asked Mar 05 '13 16:03

I Yeu C


2 Answers

Can you use default android Crop functionality?

Here is my code

private void performCrop(Uri picUri) {     try {         Intent cropIntent = new Intent("com.android.camera.action.CROP");         // indicate image type and Uri         cropIntent.setDataAndType(picUri, "image/*");         // set crop properties here         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", 128);         cropIntent.putExtra("outputY", 128);         // retrieve data on return         cropIntent.putExtra("return-data", true);         // start the activity - we handle returning in onActivityResult         startActivityForResult(cropIntent, PIC_CROP);     }     // respond to users whose devices do not support the crop action     catch (ActivityNotFoundException anfe) {         // display an error message         String errorMessage = "Whoops - your device doesn't support the crop action!";         Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);         toast.show();     } } 

declare:

final int PIC_CROP = 1; 

at top.

In onActivity result method, writ following code:

@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {     super.onActivityResult(requestCode, resultCode, data);      if (requestCode == PIC_CROP) {         if (data != null) {             // get the returned data             Bundle extras = data.getExtras();             // get the cropped bitmap             Bitmap selectedBitmap = extras.getParcelable("data");              imgView.setImageBitmap(selectedBitmap);         }     } } 

It is pretty easy for me to implement and also shows darken areas.

like image 78
Akbari Dipali Avatar answered Oct 04 '22 16:10

Akbari Dipali


This library: Android-Image-Cropper is very powerful to CropImages. It has 3,731 stars on github at this time.

You will crop your images with a few lines of code.

1 - Add the dependecies into buid.gradle (Module: app)

compile 'com.theartofdev.edmodo:android-image-cropper:2.7.+' 

2 - Add the permissions into AndroidManifest.xml

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

3 - Add CropImageActivity into AndroidManifest.xml

<activity android:name="com.theartofdev.edmodo.cropper.CropImageActivity"  android:theme="@style/Base.Theme.AppCompat"/> 

4 - Start the activity with one of the cases below, depending on your requirements.

// start picker to get image for cropping and then use the image in cropping activity CropImage.activity() .setGuidelines(CropImageView.Guidelines.ON) .start(this);  // start cropping activity for pre-acquired image saved on the device CropImage.activity(imageUri) .start(this);  // for fragment (DO NOT use `getActivity()`) CropImage.activity() .start(getContext(), this); 

5 - Get the result in onActivityResult

@Override public void onActivityResult(int requestCode, int resultCode, Intent data) {   if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {     CropImage.ActivityResult result = CropImage.getActivityResult(data);     if (resultCode == RESULT_OK) {       Uri resultUri = result.getUri();     } else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {       Exception error = result.getError();     }   } } 

You can do several customizations, as set the Aspect Ratio or the shape to RECTANGLE, OVAL and a lot more.

like image 28
Soon Santos Avatar answered Oct 04 '22 17:10

Soon Santos