选择一个图像后裁剪图像的最佳方法

时间:2019-07-04 19:42:08

标签: java android

需要裁剪图像的最佳方法

尝试了一些方法,但是它不能为我提供适当裁剪的选项,如果可以的话,请帮我提供示例和最佳裁剪图像的方法

private void selectFile() {
    Intent img = new Intent(Intent.ACTION_OPEN_DOCUMENT);
    img.setType("image/*");
    startActivityForResult(i, 3000);
}

---穆罕默德- 裁剪后如何获取Uri

                Uri uri = data.getData();
                getContentResolver().takePersistableUriPermission(Objects.requireNonNull(uri), Intent.FLAG_GRANT_READ_URI_PERMISSION);
                stickerPack.addSticker(uri, this);
            }
            finish();
            startActivity(getIntent());
        }

1 个答案:

答案 0 :(得分:1)

您可以使用默认的Android Crop功能

int PICK_IMAGE_REQUEST = 100;
int PIC_CROP = 1;

void chooseImageFromGallery() {
    if (isPermissionsGranted(Manifest.permission.READ_EXTERNAL_STORAGE)) {
        Intent intent = new Intent(Intent.ACTION_PICK,
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
    } else {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE),
                    PICK_IMAGE_REQUEST);
        }
    }
}

 @Override void onRequestPermissionsResult(int requestCode,
        String permissions[], int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
               if (requestCode == PICK_IMAGE_REQUEST && grantResults.length > 0  && grantResults[0] == PackageManager.PERMISSION_GRANTED ) {
                chooseImageFromGallery()
               }
    }


@Override void onActivityResult(Int requestCode, Int resultCode, Intent data) {
    if (resultCode == Activity.RESULT_OK && requestCode == PICK_IMAGE_REQUEST) {
        Uri imageUri = data?.data
        performCrop(imageUri)
    }else if(resultCode == Activity.RESULT_OK && requestCode == PIC_CROP){
     // get the returned data
    Bundle extras = data.getExtras();
    // get the cropped bitmap
    Bitmap selectedBitmap = extras.getParcelable("data");

    imgView.setImageBitmap(selectedBitmap);
    }
}

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

或者您可以使用任何库来拾取和裁剪图像,请检查这些库

https://github.com/ArthurHub/Android-Image-Cropper

https://github.com/Yalantis/uCrop