🚀 Compose utility library for converting Composable content into ImageBitmap 🖼️.
Made with ❤️ for Compose Multiplatform Developers
Thank to @PatilShreyas
In the previous View system, drawing Bitmap Image from View
was very straightforward. But that's not the case with Jetpack Compose since it's different in many aspects from previous system. This library helps easy way to achieve the same results.
You can check /composeApp directory which includes example application for demonstration.
How To Test sample?
To run the application on android device/emulator:
- open project in Android Studio and run imported android run configuration
Run the desktop application: ./gradlew :composeApp:run
To run the application on iPhone device/simulator:
- Open
iosApp/iosApp.xcproject
in Xcode and run standard configuration
Run the browser application: ./gradlew :composeApp:jsBrowserDevelopmentRun --continue
Run the browser application: ./gradlew :composeApp:wasmJsBrowserDevelopmentRun --continue
In lib.versions.toml
include this dependency version catalog
[versions]
capturable = "1.1.1"
[libraries]
capturable = { module = "io.github.jmseb3:capturable", version.ref = "capturable" }
dependencies {
implementation(libs.capturable)
}
or In build.gradle
of app module, include this dependency
dependencies {
implementation("io.github.jmseb3:capturable:1.1.1")
}
You can find latest version and changelogs in the releases.
To be able to capture Composable content, you need instance of CaptureController
by which you can decide when to capture the content. You can get the instance as follow.
@Composable
fun TicketScreen() {
val captureController = rememberCaptureController()
}
rememberCaptureController()
is a Composable function.
The component which needs to be captured, a capturable()
Modifier should be applied on that @Composable component as follows.
@Composable
fun TicketScreen() {
val captureController = rememberCaptureController()
// Composable content to be captured.
// Here, everything inside below Column will be get captured
Column(modifier = Modifier.capturable(captureController)) {
MovieTicketContent(...)
}
}
To capture the content, use CaptureController#captureAsync()
as follows.
// Example: Capture the content when button is clicked
val scope = rememberCoroutineScope()
Button(onClick = {
// Capture content
scope.launch {
val bitmapAsync = captureController.captureAsync()
try {
val bitmap = bitmapAsync.await()
// Do something with `bitmap`.
} catch (error: Throwable) {
// Error occurred, do something.
}
}
}) { ... }
On calling this method, request for capturing the content will be sent and ImageBitmap
will be
returned asynchronously. This method is safe to be called from Main thread.
The Platform have captureAsyncAndShare
and captureAsyncAndSave
Read contribution guidelines for more information regarding contribution.
MIT License
Copyright (c) 2022 Shreyas Patil
Copyright (c) 2024 WonDDak
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.