### Crop Image Using Bitmap Input Source: https://github.com/moyuruaizawa/cropify/blob/master/README.md This Kotlin code example shows how to use the Cropify composable with a Bitmap input. It initializes the CropifyState and provides an image resource and options for cropping. ```kotlin val state = rememberCropifyState() Cropify( bitmap = imageResource(R.drawable.bitmap), state = state, onImageCropped = { /* handle cropped image */ }, option = CropifyOption( frameSize = FullSize ), ) ``` -------------------------------- ### Crop Image Using Uri Input Source: https://github.com/moyuruaizawa/cropify/blob/master/README.md This Kotlin code example demonstrates using the Cropify composable with an Android Uri input. It includes handling potential image loading failures via `onFailedToLoadImage`. ```kotlin val state = rememberCropifyState() Cropify( uri = imageUri, state = state, onImageCropped = { /* handle cropped image */ }, onFailedToLoadImage = { /* handle loading failure */ }, option = CropifyOption( frameSize = FullSize ), ) ``` -------------------------------- ### Crop Image from Uri Source using Cropify Composable Source: https://context7.com/moyuruaizawa/cropify/llms.txt Illustrates using the Cropify composable to crop an image selected via a Uri, such as from the device gallery. This example includes handling potential image loading failures and configuring the cropping frame size using percentage. It also shows how to trigger the crop operation. ```kotlin import android.net.Uri import android.widget.Toast import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.runtime.Composable import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.ImageBitmap import androidx.compose.ui.platform.LocalContext import io.moyuru.cropify.Cropify import io.moyuru.cropify.CropifyOption import io.moyuru.cropify.CropifySize import io.moyuru.cropify.rememberCropifyState @Composable fun UriCropperScreen(imageUri: Uri) { val context = LocalContext.current val cropifyState = rememberCropifyState() var croppedImage by remember { mutableStateOf(null) } Cropify( uri = imageUri, state = cropifyState, onImageCropped = { croppedImage = it // Process the cropped ImageBitmap }, onFailedToLoadImage = { Toast.makeText(context, "Failed to load image: ${it.message}", Toast.LENGTH_SHORT).show() }, option = CropifyOption( frameSize = CropifySize.PercentageSize(0.9f) ), modifier = Modifier.fillMaxSize() ) // Trigger crop when ready Button(onClick = { cropifyState.crop() }) { Text("Crop") } } ``` -------------------------------- ### Add JitPack Repository to settings.gradle.kts Source: https://github.com/moyuruaizawa/cropify/blob/master/README.md This snippet shows how to add the JitPack repository to your project's settings.gradle.kts file, which is necessary for resolving dependencies hosted on JitPack. ```kotlin dependencyResolutionManagement { ... repositories { ... maven("https://jitpack.io") } } ``` -------------------------------- ### Crop Image from Bitmap Source using Cropify Composable Source: https://context7.com/moyuruaizawa/cropify/llms.txt Demonstrates how to use the Cropify composable to crop an image loaded from a Bitmap. It includes setting up the Cropify state, providing the source bitmap, handling the cropped image result, and configuring cropping options. The user interaction is triggered by a button. ```kotlin import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.runtime.Composable import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.ImageBitmap import androidx.compose.ui.res.imageResource import io.moyuru.cropify.Cropify import io.moyuru.cropify.CropifyOption import io.moyuru.cropify.CropifySize.PercentageSize.Companion.FullSize import io.moyuru.cropify.rememberCropifyState @Composable fun BitmapCropperScreen() { val cropifyState = rememberCropifyState() var croppedImage by remember { mutableStateOf(null) } val sourceImage = ImageBitmap.imageResource(id = R.drawable.sample_image) Cropify( bitmap = sourceImage, state = cropifyState, onImageCropped = { croppedImage = it // Handle cropped image - save to file, display preview, etc. }, option = CropifyOption(frameSize = FullSize), modifier = Modifier.fillMaxSize() ) // Trigger crop operation when user presses a button Button(onClick = { cropifyState.crop() }) { Text("Crop Image") } } ``` -------------------------------- ### Android Image Cropping with Cropify (Kotlin) Source: https://context7.com/moyuruaizawa/cropify/llms.txt This Kotlin code demonstrates a full integration of the Cropify library within an Android Jetpack Compose application. It includes image selection using ActivityResultContracts, customizable cropping options via CropifyOption, the Cropify composable for the cropping UI, and handling the cropped ImageBitmap result. ```kotlin import android.net.Uri import androidx.activity.compose.rememberLauncherForActivityResult import androidx.activity.result.contract.ActivityResultContracts import androidx.compose.foundation.layout.* import androidx.compose.material3.* import androidx.compose.runtime.* import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.ImageBitmap import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.unit.dp import io.moyuru.cropify.Cropify import io.moyuru.cropify.CropifyOption import io.moyuru.cropify.CropifySize import io.moyuru.cropify.rememberCropifyState @Composable fun ImageCropperScreen() { var selectedImageUri by remember { mutableStateOf(null) } var croppedResult by remember { mutableStateOf(null) } var showCropper by remember { mutableStateOf(false) } val cropifyState = rememberCropifyState() val context = LocalContext.current // Image picker launcher val imagePickerLauncher = rememberLauncherForActivityResult( contract = ActivityResultContracts.GetContent() ) { uri: Uri? -> uri?.let { selectedImageUri = it showCropper = true } } // Customizable crop options val cropOption = CropifyOption( frameColor = Color.White, frameAlpha = 0.9f, frameWidth = 3.dp, gridColor = Color.White, gridAlpha = 0.5f, maskColor = Color.Black, maskAlpha = 0.6f, backgroundColor = Color.Black, frameSize = CropifySize.FixedAspectRatio(1, 1) // Square crop ) if (showCropper && selectedImageUri != null) { Column(modifier = Modifier.fillMaxSize()) { // Cropper takes most of the screen Box(modifier = Modifier.weight(1f)) { Cropify( uri = selectedImageUri!!, state = cropifyState, option = cropOption, onImageCropped = { bitmap -> croppedResult = bitmap showCropper = false }, onFailedToLoadImage = { error -> // Handle error - show toast, log, etc. showCropper = false }, modifier = Modifier.fillMaxSize() ) } // Action buttons Row( modifier = Modifier .fillMaxWidth() .padding(16.dp), horizontalArrangement = Arrangement.SpaceEvenly ) { OutlinedButton(onClick = { showCropper = false }) { Text("Cancel") } Button(onClick = { cropifyState.crop() }) { Text("Crop") } } } } else { // Main screen with pick button and result preview Column( modifier = Modifier.fillMaxSize(), horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.Center ) { croppedResult?.let { // Display cropped image preview androidx.compose.foundation.Image( bitmap = it, contentDescription = "Cropped image", modifier = Modifier.size(200.dp) ) Spacer(modifier = Modifier.height(16.dp)) } Button(onClick = { imagePickerLauncher.launch("image/*") }) { Text("Select Image to Crop") } } } } ``` -------------------------------- ### Define Crop Frame Size with PercentageSize Source: https://context7.com/moyuruaizawa/cropify/llms.txt Sets the crop frame size as a percentage of the image dimensions. Supports full image coverage or custom width and height percentages. ```kotlin import io.moyuru.cropify.CropifyOption import io.moyuru.cropify.CropifySize.PercentageSize import io.moyuru.cropify.CropifySize.PercentageSize.Companion.FullSize // Full size frame - covers entire image (100%) val fullSizeOption = CropifyOption( frameSize = FullSize ) // Square percentage - 80% of image on both dimensions val squareOption = CropifyOption( frameSize = PercentageSize(0.8f) ) // Custom width and height percentages val customPercentOption = CropifyOption( frameSize = PercentageSize( widthPercentage = 0.9f, // 90% of image width heightPercentage = 0.7f // 70% of image height ) ) ``` -------------------------------- ### Add Cropify Dependency Source: https://github.com/moyuruaizawa/cropify/blob/master/README.md This snippet demonstrates how to add the Cropify library as a dependency in your app's build.gradle file. Replace `${cropifyVersion}` with the actual version number. ```kotlin dependencies { implementation("com.github.moyuruaizawa:cropify:${cropifyVersion}") } ``` -------------------------------- ### Add Cropify Dependency to Android Project Source: https://context7.com/moyuruaizawa/cropify/llms.txt This snippet shows how to add the Cropify library to your Android project by including the JitPack repository and the library dependency in your Gradle build files. Ensure you are using the correct version number. ```kotlin dependencyResolutionManagement { repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) repositories { google() mavenCentral() maven("https://jitpack.io") } } // build.gradle.kts (app module) dependencies { implementation("com.github.moyuruaizawa:cropify:1.0.0") } ``` -------------------------------- ### Remember Cropify State Instance Source: https://context7.com/moyuruaizawa/cropify/llms.txt Creates and remembers a CropifyState instance to manage cropping frame state and trigger cropping operations. This state survives recomposition. ```kotlin import androidx.compose.runtime.Composable import io.moyuru.cropify.Cropify import io.moyuru.cropify.rememberCropifyState @Composable fun CropperWithState() { // Create state that survives recomposition val state = rememberCropifyState() Cropify( bitmap = myBitmap, state = state, onImageCropped = { /* handle result */ } ) // Call crop() to trigger cropping - result delivered via onImageCropped callback LaunchedEffect(shouldCrop) { if (shouldCrop) { state.crop() } } } ``` -------------------------------- ### Customize Cropify Appearance with CropifyOption Source: https://context7.com/moyuruaizawa/cropify/llms.txt Configures the visual aspects of the cropping interface, including frame, grid, mask, and background colors and opacities. It also allows setting frame size constraints. ```kotlin import androidx.compose.ui.graphics.Color import androidx.compose.ui.unit.dp import io.moyuru.cropify.CropifyOption import io.moyuru.cropify.CropifySize // Default options val defaultOption = CropifyOption() // Fully customized options val customOption = CropifyOption( frameColor = Color.White, // Color of the crop frame border frameAlpha = 0.8f, // Frame opacity (0.0 - 1.0) frameWidth = 2.dp, // Frame border stroke width gridColor = Color.White, // Color of the 3x3 grid lines gridAlpha = 0.6f, // Grid lines opacity gridWidth = 1.dp, // Grid line stroke width maskColor = Color.Black, // Color of area outside crop frame maskAlpha = 0.5f, // Mask overlay opacity backgroundColor = Color.Black, // Background behind the image frameSize = CropifySize.FixedAspectRatio(16, 9) // Frame size constraint ) // Dark theme option example val darkThemeOption = CropifyOption( frameColor = Color.Cyan, frameAlpha = 1f, gridColor = Color.Cyan, gridAlpha = 0.4f, maskColor = Color.Black, maskAlpha = 0.7f, backgroundColor = Color.DarkGray ) ``` -------------------------------- ### Constrain Crop Frame with FixedAspectRatio Source: https://context7.com/moyuruaizawa/cropify/llms.txt Enforces a specific aspect ratio for the crop frame, which is maintained during user resizing. Useful for standard formats like 16:9 or 1:1. ```kotlin import io.moyuru.cropify.CropifyOption import io.moyuru.cropify.CropifySize.FixedAspectRatio // 1:1 square aspect ratio (e.g., profile pictures) val squareOption = CropifyOption( frameSize = FixedAspectRatio(1, 1) ) // 16:9 widescreen aspect ratio (e.g., video thumbnails) val widescreenOption = CropifyOption( frameSize = FixedAspectRatio(16, 9) ) // 4:3 standard aspect ratio (e.g., photos) val standardOption = CropifyOption( frameSize = FixedAspectRatio(4, 3) ) // 9:16 portrait aspect ratio (e.g., stories, reels) val portraitOption = CropifyOption( frameSize = FixedAspectRatio(9, 16) ) // Using float values for precise ratios val goldenRatioOption = CropifyOption( frameSize = FixedAspectRatio(1.618f, 1f) ) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.