# Android-Image-Cropper2 **Repository Path**: zhao_dong_liang/android-image-cropper2 ## Basic Information - **Project Name**: Android-Image-Cropper2 - **Description**: No description available - **Primary Language**: Unknown - **License**: Apache-2.0 - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-02-13 - **Last Updated**: 2025-02-13 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README [![CanHub](.documentation/art/cover.png?raw=true)](https://github.com/canhub) Android Image Cropper ===================== - **Powerful** (Zoom, Rotation, Multi-Source) - **Customizable** (Shape, Limits, Style) - **Optimized** (Async, Sampling, Matrix) - **Simple** image cropping library for Android ![Crop demo](.documentation/art/showcase-1.gif?raw=true) ## Add to your project ```groovy dependencies { implementation("com.vanniktech:android-image-cropper:4.6.0") } ``` ## Using the Library There are 3 ways of using the library. Check out the sample app for all details. ### [1. Calling crop directly](./sample/src/main/kotlin/com/canhub/cropper/sample/SampleCrop.kt) **Note:** This way is deprecated and will be removed in future versions. The path forward is to write your own Activity, handle all the `Uri` stuff yourself and use `CropImageView`. ```kotlin 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. } ``` ### [2. Using CropView](./sample/src/main/kotlin/com/canhub/cropper/sample/SampleUsingImageView.kt) **Note:** This is the only way forward, add `CropImageView` into your own activity and do whatever you wish. Checkout the sample for more details. ```xml ``` - Set image to crop ```kotlin cropImageView.setImageUriAsync(uri) // Or prefer using uri for performance and better user experience. cropImageView.setImageBitmap(bitmap) ``` - Get cropped image ```kotlin // Subscribe to async event using cropImageView.setOnCropImageCompleteListener(listener) cropImageView.getCroppedImageAsync() // Or. val cropped: Bitmap = cropImageView.getCroppedImage() ``` ### [3. Extend to make a custom activity](./sample/src/main/kotlin/com/canhub/cropper/sample/SampleCustomActivity.kt) **Note:** This way is also deprecated and will be removed in future versions. The path forward is to write your own Activity, handle all the `Uri` stuff yourself and use `CropImageView`. If you want to extend the `CropImageActivity` please be aware you will need to set up your `CropImageView` - Add `CropImageActivity` into your AndroidManifest.xml ```xml ``` - Set up your `CropImageView` after call `super.onCreate(savedInstanceState)` ```kotlin override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setCropImageView(binding.cropImageView) } ``` #### Custom dialog for image source pick When calling crop directly the library will prompt a dialog for the user choose between gallery or camera (If you keep both enable). We use the Android default AlertDialog for this. If you wanna customised it with your app theme you need to override the method `showImageSourceDialog(..)` when extending the activity _(above)_ ```kotlin override fun showImageSourceDialog(openSource: (Source) -> Unit) { super.showImageSourceDialog(openCamera) } ``` ## Posts - [Android cropping image from camera or gallery](https://canato.medium.com/android-cropping-image-from-camera-or-gallery-fbe732800b08) ## Migrating from Android Image Cropper Start by using [Version 4.3.3](https://github.com/CanHub/Android-Image-Cropper/releases/tag/4.3.3): ```groovy dependencies { implementation("com.vanniktech:android-image-cropper:4.3.3") } ``` ### Update all imports ```diff -import com.theartofdev.edmodo.cropper.CropImage -import com.theartofdev.edmodo.cropper.CropImageActivity +import com.canhub.cropper.CropImage +import com.canhub.cropper.CropImageActivity ``` ### Update all XML references ```diff -