Cropify

repository·master·Indexed 42 days ago

https://github.com/moyuruaizawa/cropify

A lightweight image cropper for Android Jetpack Compose that supports Bitmaps and android.net.Uri inputs. It provides a Cropify composable and CropifyState for managing the cropping process, with customizable visual options via CropifyOption for frames, grids, and masks.

Tokens
1.1K
Snippets
5
Records
6
Agent score
3%

What's inside Cropify

  1. Install Cropify via JitPack

    master

    To use Cropify in your Android project, add the JitPack repository to your settings.gradle.kts file and then add the dependency to your build.gradle.kts file.

    // 1. Add JitPack to settings.gradle.kts
    dependencyResolutionManagement {
        ...
        repositories {
            ...
            maven("https://jitpack.io")
        }
    }
    
    // 2. Add dependency to build.gradle.kts
    dependencies {
      implementation("com.github.moyuruaizawa:cropify:${cropifyVersion}")
    }
  2. Configure CropifyOption

    master

    The CropifyOption object allows you to customize the visual appearance of the cropper. Supported customization properties include:

    • Frame: frameColor, frameAlpha, frameStrokeWidth, and frameAspectRatio.
    • Grid: gridLineColor, gridLineAlpha, and gridLineStrokeWidth.
    • Mask: maskColor (the color outside the cropping frame) and maskAlpha.
    • Background: backgroundColor.
    • Frame Size: frameSize (e.g., FullSize).
  3. Use Cropify with a Bitmap

    master

    To crop a Bitmap in Jetpack Compose, use rememberCropifyState() to manage the state and pass the bitmap to the Cropify composable. The result of the crop is returned via the onImageCropped callback when you invoke state.crop().

    val state = rememberCropifyState()
    
    Cropify(
      bitmap = imageResource(R.drawable.bitmap),
      state = state,
      onImageCropped = { /* handle cropped bitmap */ },
      option = CropifyOption(
        frameSize = FullSize
      ),
    )
    
    // To trigger the crop:
    // state.crop()
  4. Use Cropify with a Uri

    master

    To crop an image loaded from an android.net.Uri, pass the uri parameter to the Cropify composable. You can handle loading failures using the onFailedToLoadImage callback. The cropped result is returned via onImageCropped when state.crop() is called.

    val state = rememberCropifyState()
    
    Cropify(
      uri = imageUri,
      state = state,
      onImageCropped = { /* handle cropped bitmap */ },
      onFailedToLoadImage = { /* handle error */ },
      option = CropifyOption(
        frameSize = FullSize
      ),
    )
    
    // To trigger the crop:
    // state.crop()
  5. Trigger a crop using CropifyState.crop()

    master

    To signal that the user has finished selecting their area and the image should be cropped, call the crop() method on your CropifyState instance. This sets the internal shouldCrop flag to true.

    val cropifyState = rememberCropifyState()
    
    // ... inside a Button onClick or similar event
    Button(onClick = { cropifyState.crop() }) {
        Text("Crop Image")
    }
    // Usage example
    val cropifyState = rememberCropifyState()
    
    // Trigger the crop
    cropifyState.crop()
  6. Initialize CropifyState with rememberCropifyState()

    master

    In Jetpack Compose, use the rememberCropifyState() composable function to create and remember a CropifyState instance. This ensures the cropping state (like the selection rectangle) survives recompositions.

    CropifyState is the central controller for the cropping process, managing the selection rectangle (frameRect) and triggering the crop action via crop().

    Note: Most properties within CropifyState (like frameRect, imageRect, and shouldCrop) are marked as internal and are intended to be managed by the library's UI components, but the state object itself is the primary entry point for controlling the lifecycle.

    @Composable
    fun rememberCropifyState(): CropifyState