Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kaustubhpatange/kapture
A small library for Jetpack Compose to capture Composable content to Android Bitmap.
https://github.com/kaustubhpatange/kapture
android jetpack-compose kotlin screenshot
Last synced: 3 months ago
JSON representation
A small library for Jetpack Compose to capture Composable content to Android Bitmap.
- Host: GitHub
- URL: https://github.com/kaustubhpatange/kapture
- Owner: KaustubhPatange
- License: apache-2.0
- Created: 2022-01-05T09:19:41.000Z (about 3 years ago)
- Default Branch: master
- Last Pushed: 2022-01-05T12:20:03.000Z (about 3 years ago)
- Last Synced: 2024-04-18T03:11:06.098Z (10 months ago)
- Topics: android, jetpack-compose, kotlin, screenshot
- Language: Kotlin
- Homepage:
- Size: 111 KB
- Stars: 13
- Watchers: 2
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# kapture
[![CI](https://github.com/KaustubhPatange/kapture/actions/workflows/ci.yml/badge.svg)](https://github.com/KaustubhPatange/kapture/actions/workflows/ci.yml)
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()`](https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:compose/ui/ui-test/src/androidMain/kotlin/androidx/compose/ui/test/AndroidImageHelpers.android.kt;l=42) which we use for screenshot testing.
## Implementation
Check the sample in [/app](/app) directory which demonstrates the full usage of the library.
### Gradle Setup
![Maven Central](https://img.shields.io/maven-central/v/io.github.kaustubhpatange/kapture)
In your module's `build.gradle`, include the dependency.
```gradle
dependencies {
implementation "io.github.kaustubhpatange:kapture:$version"
}
```Check the release notes for latest version [here](CHANGELOG.md).
### Usage
- Capturing to Android Bitmap is managed by `ScreenshotController` class. You can obtain it as follows,
```kotlin
@Composable
fun TestScreen() {
val screenshotController = rememberScreenshotController()
...
}
```- Attach this controller to the Composable through a `Modifier` extension function `attachController(...)`. Through this we can capture the content for this composable including it's child heirarchy.
```kotlin
@Composable
fun TestScreen() {
val screenshotController = rememberScreenshotController() // <--Column(modifier = Modifier
.attachController(screenshotController)) { // <--
Text(...)
Icon(...)
}
}
```- To capture the content just call `ScreenshotController.captureToBitmap()`. It is a suspending function so make sure to run it in a coroutine scope or in an implicit scope provided by `LaunchedEffect`.
```kotlin
@Composable
fun TestScreen() {
val screenshotController = rememberScreenshotController() // <--Column(modifier = Modifier
.attachController(screenshotController)) { // <--
Text(...)
Icon(...)
}LaunchedEffect(...) { // implicit coroutine scope
val bitmap: Result = screenshotController.captureToBitmap(
config = Bitmap.Config.ARGB_8888 // optional
)
...
}
}
```- The call returns a `kotlin.Result` which has a bitmap if successful otherwise a throwable.
- I'll advice you to check [Tests](kapture/src/androidTest/java/com/kpstv/compose/kapture/) for some more deeper understanding.## Contribute
If you want to contribute to this project, you're always welcome!
See [Contributing Guidelines](CONTRIBUTING.md).## License
- [The Apache License Version 2.0](https://www.apache.org/licenses/LICENSE-2.0.txt)
```
Copyright 2022 Kaustubh PatangeLicensed 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 athttps://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.
```