Multiplatform library to take/capture screenshot/snapshot/picture/image of @Composables (and also Android Views).
implementation("ir.mahozad.multiplatform:comshot:0.3.0")
- Android (Composable)
You need to pass your activity tocaptureToImage
. Also, it should be called from the Main aka UI thread (see its KDoc for more information).@Composable fun Activity.MyComposable() { val activity = this var screenshot by remember { mutableStateOf<ImageBitmap?>(null) } val composable: @Composable () -> Unit = remember { @Composable { Row { Text(text = "Hello") Text(text = "Meow!") } } } // You can also render your composable simply like this: // composable() Column { Button(onClick = { screenshot = captureToImage(activity, composable) }) { Text(text = "Capture") } screenshot?.let { Image( bitmap = it, modifier = Modifier.width(200.dp), contentDescription = null ) } } }
- Android (View)
val view = findViewById<TextView>(R.id.myTextView) val screenshot = captureToImage(view) // If you want Bitmap: val androidBitmap = screenshot.asAndroidBitmap()
- Other targets
@Composable fun MyComposable() { var screenshot by remember { mutableStateOf<ImageBitmap?>(null) } val composable: @Composable () -> Unit = remember { @Composable { Row { Text(text = "Hello") Text(text = "Meow!") } } } // You can also render your composable simply like this: // composable() Column { Button(onClick = { screenshot = captureToImage(composable) }) { Text(text = "Capture") } screenshot?.let { Image( bitmap = it, modifier = Modifier.width(200.dp), contentDescription = null ) } } }
- Capturable : https://github.com/PatilShreyas/Capturable
- Compose ScreenshotBox: https://github.com/SmartToolFactory/Compose-Screenshot
- Generate bitmap from composable: https://github.com/JohannRosenberg/bitmap-from-composable
- Generate JPEG from composable: https://github.com/Vipul12Thawre/JetCapture
- https://developer.android.com/studio/preview/compose-screenshot-testing
- https://developer.android.com/reference/androidx/test/espresso/screenshot/ViewInteractionCapture