简体   繁体   English

如何将图像直接传递到 ArthurHub / Android-Image-Cropper 进行裁剪

[英]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.

相关问题 Android-Image-Cropper 转圈 - Android-Image-Cropper to circle 使用Android-Image-Cropper将图像发送到服务器时,使用getCroppedImage()返回NULL - Sending image to server with Android-Image-Cropper returns NULL with getCroppedImage() Android-Image-Cropper菜单项Actionlistener无法正常工作 - Android-Image-Cropper Menu item Actionlistener is not working 如何解决“gradle failed resolve com.theartofdev.edmodo:android-image-cropper:+” - how to solve "gradle failed resolve com.theartofdev.edmodo:android-image-cropper:+" Image-Crop-Activity不会将Image-Uri发送到接收活动(Android-Image-Cropper) - Image-Crop-Activity doesn't send Image-Uri to receiving Activity (Android-Image-Cropper) 当我尝试使用 android-image-cropper:2.8.+ 时卡住了 - I'm stuck when i try to use android-image-cropper:2.8.+ 找不到与com.theartofdev.edmodo:android-image-cropper:+相匹配的版本 - Could not find any version that matches com.theartofdev.edmodo:android-image-cropper:+ 找不到 com.theartofdev.edmodo:android-image-cropper:2.8.0 - Could not find com.theartofdev.edmodo:android-image-cropper:2.8.0 我在使用编译 'com.theartofdev.edmodo:android-image-cropper:2.7.+' 这个库裁剪图像时遇到问题 - I facing issue while using compile 'com.theartofdev.edmodo:android-image-cropper:2.7.+' this library to crop the image ArthurHub 图像裁剪器在 Fragment 中无法正常工作,要求手动定义 RESULT_OKAY - ArthurHub Image cropper not working properly in Fragment, asking for RESULT_OKAY to be defined manually
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM