KaustubhPatange / kapture

A small library for Jetpack Compose to capture Composable content to Android Bitmap.
Apache License 2.0
13 stars 0 forks source link
android jetpack-compose kotlin screenshot

kapture

CI

A small utility library for Jetpack Compose to capture Composable content to Android Bitmap.

The way this work is we listen to the coordinates of the Composable through Modifier.onGloballyPositioned & crop the bitmap to the coordinates we get from the callback from the nearest root composable attached to LocalView.current.

The working is similar to SemanticsNodeInteraction.captureToImage() which we use for screenshot testing.

Implementation

Check the sample in /app directory which demonstrates the full usage of the library.

Gradle Setup

Maven Central

In your module's build.gradle, include the dependency.

dependencies {
    implementation "io.github.kaustubhpatange:kapture:$version"
}

Check the release notes for latest version here.

Usage

@Composable
fun TestScreen() {
    val screenshotController = rememberScreenshotController()
    ...
}
@Composable
fun TestScreen() {
    val screenshotController = rememberScreenshotController() // <--

    Column(modifier = Modifier
        .attachController(screenshotController)) {  // <--
        Text(...)
        Icon(...)
    }
}
@Composable
fun TestScreen() {
    val screenshotController = rememberScreenshotController() // <--

    Column(modifier = Modifier
        .attachController(screenshotController)) {  // <--
        Text(...)
        Icon(...)
    }

    LaunchedEffect(...) { // implicit coroutine scope
        val bitmap: Result<Bitmap> = screenshotController.captureToBitmap(
            config = Bitmap.Config.ARGB_8888 // optional
        )
        ...
    }
}

Contribute

If you want to contribute to this project, you're always welcome! See Contributing Guidelines.

License

Copyright 2022 Kaustubh Patange

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.