如何修复Android中的图像裁剪

时间:2019-05-27 05:39:07

标签: android image crop

我正在尝试从相机和图库中裁剪图像,但无法正常工作。我正在使用此代码来裁剪图像。该图像是从相机中捕获的,并且是从图库中选择的,但我无法对其进行裁剪。

我的Android版本是牛轧糖。

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();
    }
}

@Override
protected void onActivityResult ( int requestCode, int resultCode, Intent data){
    super.onActivityResult(requestCode, resultCode, data);
    /*ImageCropFunction();*/
    if (resultCode == RESULT_CANCELED) return;

    if (requestCode == CAMERA_REQUEST) {
        Bitmap bitmap = (Bitmap) data.getExtras().get("data");
        imagephotoButton.setImageBitmap(bitmap);
        /*  ImageCropFunction();*/
    } else if (requestCode == GALLERY_REQUEST) {
        imageUri = data.getData();
        imagephotoButton.setImageURI(imageUri);
        /* ImageCropFunction();*/
    }

    if (requestCode == PIC_CROP) {
        if (data != null) {
            // get the returned data
            Bundle extras = data.getExtras();
            // get the cropped bitmap
            Bitmap selectedBitmap = extras.getParcelable("data");

            imagephotoButton.setImageBitmap(selectedBitmap);
        }
    }
}

0 个答案:

没有答案