[英]How to directly pass Image into ArthurHub / Android-Image-Cropper for cropping
我正在为我的 UI、Activity Result Contracts 和ArthutHub Android Image Cropper Library使用 Jetpack compose 来裁剪我的应用程序中的图像。 使用以下代码一切正常:
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)
}
因此,使用这种方法,该库不仅负责裁剪图像,还负责通过相机/图库捕获它们。 我想自己传递一个 Bitmap 图像(通过使用内置的 Android API 获取图像来拍照并获取 Gallery Image )并使用这个库进行裁剪,这样我就可以有 2 个专用按钮供用户捕获照片或从图库中选择。
我现在有图像,我希望这个库为我裁剪它,我如何将图像传递给它?
尝试使用最新的库https://github.com/CanHub/Android-Image-Cropper
他们可以像这样传递图像:
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.