Ajay Jayendran
Ajay Jayendran

Reputation: 1604

Captured Camera Image can't able to Crop

I am trying to get image from phone gallery or capturing image from camera..I have used 'me.villani.lorenzo.android:android-cropimage:1.1.+' for croping the image..It works well for getting image from phone gallery..While Capturing the image from camera,It captured the image but it cannot able to crop..It works fine on Choose from Image from gallery..Here i included my code,please have a look,

public class Details extends AppCompatActivity {
    ImageView i1,i2;
    Bitmap bitmapPic;
    private static int REQUEST_PICTURE = 1;
    private final static int REQUEST_PERMISSION_REQ_CODE = 34;
    private static int REQUEST_CAMERA = 0, SELECT_FILE = 1, REQUEST_CROP_PICTURE = 2;
    private static int CROP_IMAGE_SIZE = 200;
    private static int CROP_IMAGE_HIGHLIGHT_COLOR = 0x6aa746F4;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_details);
        android.support.v7.app.ActionBar ab = getSupportActionBar();
        if (ab != null) {
            ab.hide();
        }
        i1 = (ImageView)findViewById(R.id.prof1);
        i2 = (ImageView)findViewById(R.id.prof2);
        i1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                selectImageOption();
            }
        });
    }
    private void selectImageOption() {
        final CharSequence[] items = { "Capture Photo", "Choose from Gallery", "Cancel" };
        AlertDialog.Builder builder = new AlertDialog.Builder(Details.this);
        builder.setTitle("Add Photo!");
        builder.setItems(items, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int item) {
                if (items[item].equals("Capture Photo")) {
                    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    startActivityForResult(intent, REQUEST_CAMERA);
                } else if (items[item].equals("Choose from Gallery")) {
                    Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
                    intent.setType("image/*");
                    intent.putExtra("return-data", true);
                    startActivityForResult(intent, SELECT_FILE);
                } else if (items[item].equals("Cancel")) {
                    dialog.dismiss();
                }
            }
        });
        builder.show();
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        File croppedImageFile = new File(getFilesDir(), "Pic.jpg");
        Uri croppedImage = Uri.fromFile(croppedImageFile);
        if (requestCode == REQUEST_CROP_PICTURE && resultCode == RESULT_OK) {
            bitmapPic = BitmapFactory.decodeFile(croppedImageFile.getAbsolutePath());
            if (bitmapPic != null) {
                i1.setImageBitmap(bitmapPic);
            } else {
                Toast.makeText(Details.this, "Image Error while Cropping", Toast.LENGTH_LONG).show();
            }
        } else if (resultCode == RESULT_OK && (requestCode == REQUEST_CAMERA || requestCode == SELECT_FILE)) {
            showImageCropView(data, croppedImage);
        }
    }
    private void showImageCropView(Intent data, Uri croppedImage) {
        CropImageIntentBuilder cropImage = new CropImageIntentBuilder(CROP_IMAGE_SIZE, CROP_IMAGE_SIZE, croppedImage);
        cropImage.setOutlineColor(CROP_IMAGE_HIGHLIGHT_COLOR);
        cropImage.setSourceImage(data.getData());
        cropImage.setCircleCrop(true);
        startActivityForResult(cropImage.getIntent(this), REQUEST_CROP_PICTURE);
    }
}

Captured Image from Camera does not able to crop!Please give me better Solution for this.!Thanks in Advance

Upvotes: 0

Views: 1143

Answers (1)

yuzurihaaa93
yuzurihaaa93

Reputation: 465

Bro, check this out. May solve your problem, you can crop the image from camera / gallery. link

  1. set
<activity android:name="com.theartofdev.edmodo.cropper.CropImageActivity"
  android:theme="@style/Base.Theme.AppCompat"/> <!-- optional (needed if default theme has no action bar) --> 

to your manifest

  1. call startCropImageActivity(null); in onclick method

  2. this is the method :

private void startCropImageActivity(Uri imageUri) {
        CropImage.activity(imageUri)
                .setGuidelines(CropImageView.Guidelines.ON)
                .setMultiTouchEnabled(true)
                .setRequestedSize(320, 320, CropImageView.RequestSizeOptions.RESIZE_INSIDE)
                .setMinCropWindowSize(0,0)
                .setAspectRatio(1,1)
                .setCropShape(CropImageView.CropShape.OVAL)
                .start(this);
    }

and in onActivityResult

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
            result = CropImage.getActivityResult(data);
            if (resultCode == RESULT_OK) {
                ((CircleImageView) findViewById(R.id.profileImage)).setImageURI(result.getUri());
            } else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
                Toast.makeText(this, "Cropping failed: " + result.getError(), Toast.LENGTH_LONG).show();
            }
        }
    }

Here I'm using CircleImageView as circle image

Upvotes: 1

Related Questions