Yalantis / uCrop

Image Cropping Library for Android
https://yalantis.com/blog/introducing-ucrop-our-own-image-cropping-library-for-android/
11.86k stars 2.16k forks source link

How to crop image with known path based on user selection #841

Open Sokamn opened 2 years ago

Sokamn commented 2 years ago

I've a question, how can i crop an image if i know the imagePath?

private val getContent = registerForActivityResult(ActivityResultContracts.GetContent()){ uri ->
    val inputUri = uri
    val outputUri = File(filesDir,"croppedImage.jpg").toUri()
    val listUri = listOf<Uri>(inputUri,outputUri)
    cropImage.launch(listUri)
}
private val uCropContract = object: ActivityResultContract<List<Uri>,Uri>(){
    override fun createIntent(context: Context, input: List<Uri>): Intent {
        val inputUri = input[0]
        val outputUri = input[1]

        val uCrop = UCrop.of(inputUri, outputUri)
            .withAspectRatio(5f,5f)
            .withMaxResultSize(1080,1080)
        return uCrop.getIntent(context)
    }

    override fun parseResult(resultCode: Int, intent: Intent?): Uri {
        return UCrop.getOutput(intent!!)!!
    }
}
private val cropImage = registerForActivityResult(uCropContract){ uri ->
    binding.imvImageAdded.setImageURI(uri)
}