Android-Image-Cropper icon indicating copy to clipboard operation
Android-Image-Cropper copied to clipboard

README update required

Open ArtRoman opened this issue 2 years ago • 4 comments
trafficstars

Current documentation is misleading, this code in README.md will be reported with an "Unresolved reference" errors:

  cropImage.launch(
      options(uri = imageUri) {
        setGuidelines(Guidelines.ON)
        setOutputCompressFormat(CompressFormat.PNG)
      }
    )
  }

It requires new launch and options objects in 4.3.3 and later:

        cropImage.launch(
            CropImageContractOptions(
                uri = imageUri,
                CropImageOptions(
                    outputCompressFormat = Bitmap.CompressFormat.PNG,
                    // other params
                )
            )
        ) 

ArtRoman avatar Aug 10 '23 16:08 ArtRoman

Can you please create a PR?

vanniktech avatar Aug 10 '23 16:08 vanniktech

The cropImage.launch() method was changed in version 4.3.3 of the Android Image Cropper library. The old method used a options() block to set the crop options, but this block is no longer supported. In the new method, you need to pass a CropImageContractOptions object to the launch() method. This object contains the crop options, including the output compression format.

However, in newer versions, you will need to use the new launch() method.

Here is an example of how to use the new launch() method:


val cropImageOptions = CropImageOptions.Builder()
    .setOutputCompressFormat(Bitmap.CompressFormat.PNG)
    .build()

val cropImageContractOptions = CropImageContractOptions(
    uri = imageUri,
     cropImageOptions = cropImageOptions
)

cropImage.launch(cropImageContractOptions)

harshsingh-io avatar Aug 17 '23 16:08 harshsingh-io

Here's the modified version of the code:

class MainActivity : AppCompatActivity() {

    private val cropImage = registerForActivityResult(CropImageContract()) { result ->
        if (result.isSuccessful) {
            // Use the cropped image URI.
            val croppedImageUri = result.uriContent
            val croppedImageFilePath = result.getUriFilePath(this) // optional usage
            // Process the cropped image URI as needed.
        } else {
            // An error occurred.
            val exception = result.error
            // Handle the error.
        }
    }

    private fun startCrop() {
        // Start cropping activity with guidelines.
        cropImage.launch(
            CropImageContractOptions(
                cropImageOptions = CropImageOptions(
                    guidelines = Guidelines.ON
                )
            )
        )

        // Start cropping activity with gallery picker only.
        cropImage.launch(
            CropImageContractOptions(
                pickImageContractOptions = PickImageContractOptions(
                    includeGallery = true,
                    includeCamera = false
                )
            )
        )

        // Start cropping activity for a pre-acquired image with custom settings.
        cropImage.launch(
            CropImageContractOptions(
                uri = imageUri,
                cropImageOptions = CropImageOptions(
                    guidelines = Guidelines.ON,
                    outputCompressFormat = Bitmap.CompressFormat.PNG
                )
            )
        )
    }

    // Call the startCrop function when needed.
}

In this code, I've made the following changes:

  • Added the necessary import for AppCompatActivity.
  • Adjusted the variable name croppedImageUri to be more descriptive.
  • Added this as the context parameter for getUriFilePath to ensure proper context reference.
  • Updated the way cropImage.launch is called by providing CropImageContractOptions objects that contain the necessary options for each scenario

harshsingh-io avatar Aug 17 '23 17:08 harshsingh-io

Here's the modified version of the code:

Thank you, I haven't got any problems with modifying code. I use most of available options, so my changelog is quite larger:

cropImage.launch(
    options(uri) {
        setGuidelines(CropImageView.Guidelines.ON_TOUCH)
        setActivityTitle(getString(R.string.profile_select_image))
        setCropShape(CropImageView.CropShape.RECTANGLE)
        setFixAspectRatio(false)
        setInitialCropWindowPaddingRatio(0f)
        setCropMenuCropButtonTitle(getString(R.string.send))
        setRequestedSize(Constants.MAX_PHOTO_SIZE, Constants.MAX_PHOTO_SIZE, CropImageView.RequestSizeOptions.RESIZE_INSIDE)
        setOutputCompressFormat(Bitmap.CompressFormat.JPEG)
    }
)

to

cropImage.launch(
    CropImageContractOptions(
        uri, CropImageOptions(
            guidelines = CropImageView.Guidelines.ON_TOUCH,
            activityTitle = getString(R.string.profile_select_image),
            cropShape = CropImageView.CropShape.RECTANGLE,
            fixAspectRatio = false,
            initialCropWindowPaddingRatio = 0f,
            cropMenuCropButtonTitle = getString(R.string.send),
            outputRequestSizeOptions = CropImageView.RequestSizeOptions.RESIZE_INSIDE,
            outputRequestWidth = Constants.MAX_PHOTO_SIZE,
            outputRequestHeight = Constants.MAX_PHOTO_SIZE,
            outputCompressFormat = Bitmap.CompressFormat.JPEG,
        )
    )
)

The issue is about misleading documentation in the project's welcome page of this repository, which may disappoint new library users.

ArtRoman avatar Aug 21 '23 16:08 ArtRoman

@harshsingh-io thanks for the corrections. I've created a PR :)

vanniktech avatar Aug 05 '24 09:08 vanniktech

@vanniktech I hope we can work together. I was not free at that moment. That's why I was not able to create PR of it.

harshsingh-io avatar Aug 05 '24 20:08 harshsingh-io