[英]How to directly pass Image into ArthurHub / Android-Image-Cropper for cropping
I am using Jetpack compose for my UI, Activity Result Contracts and ArthutHub Android Image Cropper Library to crop images in my App.我正在为我的 UI、Activity Result Contracts 和ArthutHub Android Image Cropper Library使用 Jetpack compose 来裁剪我的应用程序中的图像。 Everything works fine as expected with the following code:使用以下代码一切正常:
val (result, launcher) = setUpContentPhotoCropImageIntent()//this activity result
//contracts in launched on a button press
result.value?.let {
//code here to save the image and show it in the UI, upload to cloud storage etc.
}
@Composable
fun setUpContentPhotoCropImageIntent(): Pair<MutableState<Uri?>,
ManagedActivityResultLauncher<Any?, Uri?>> {
val cropActivityResultContract = object : ActivityResultContract<Any?, Uri?>() {
override fun createIntent(context: Context, input: Any?): Intent {
CropImage.isExplicitCameraPermissionRequired(context)
CropImage.getPickImageChooserIntent(context)
return CropImage
.activity()
.setActivityTitle("Choose Photo")
.setCropMenuCropButtonTitle("Select")
.setAllowRotation(true)
.setGuidelines(CropImageView.Guidelines.ON_TOUCH)
.setCropShape(CropImageView.CropShape.RECTANGLE)
.setAllowFlipping(true)
.setOutputCompressQuality(100)
.setFixAspectRatio(true)
.setMaxCropResultSize(
2000,
2000
)
.getIntent(context)
}
override fun parseResult(resultCode: Int, intent: Intent?): Uri? {
return CropImage.getActivityResult(intent)?.uri
}
}
val result = remember { mutableStateOf<Uri?>(null) }
val launcher = rememberLauncherForActivityResult(cropActivityResultContract) {
result.value = it
}
return Pair(result, launcher)
}
So with this approach, the library takes care of not only cropping images but also capturing them via camera/gallery.因此,使用这种方法,该库不仅负责裁剪图像,还负责通过相机/图库捕获它们。 I would like to pass a Bitmap image myself(by getting images using the built-in Android APIs to Take Picture and get Gallery Image ) and use this library for just cropping, so that I can have 2 dedicated buttons for the user to either capture photo or choose from Gallery.我想自己传递一个 Bitmap 图像(通过使用内置的 Android API 获取图像来拍照并获取 Gallery Image )并使用这个库进行裁剪,这样我就可以有 2 个专用按钮供用户捕获照片或从图库中选择。
I have the image now, I want this library to just crop it for me, How do i Pass an Image into it?我现在有图像,我希望这个库为我裁剪它,我如何将图像传递给它?
Try to use the latest library https://github.com/CanHub/Android-Image-Cropper尝试使用最新的库https://github.com/CanHub/Android-Image-Cropper
and them you can pass the image like this:他们可以像这样传递图像:
class MainActivity {
private val cropImage = registerForActivityResult(CropImageContract()) { result ->
if (result.isSuccessful) {
// use the returned uri
val uriContent = result.uriContent
val uriFilePath = result.getUriFilePath(context) // optional usage
} else {
// an error occurred
val exception = result.error
}
}
private fun startCrop() {
// start cropping activity for pre-acquired image saved on the device and customize settings
cropImage.launch(
options(uri = imageUri) {
setGuidelines(Guidelines.ON)
setOutputCompressFormat(CompressFormat.PNG)
}
)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.