Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

colors are inverted #193

Closed
MahmoudMabrok opened this issue Jul 27, 2024 · 6 comments
Closed

colors are inverted #193

MahmoudMabrok opened this issue Jul 27, 2024 · 6 comments

Comments

@MahmoudMabrok
Copy link

MahmoudMabrok commented Jul 27, 2024

I found after capture colors are inverted, i.e. white became black and so on

snippet:

            Image(
                painter = painterResource(barCode),
                modifier = Modifier
                    .fillMaxWidth()
                    .height(100.dp)
                    .capturable(captureController),
                contentDescription = null
            )
@PatilShreyas
Copy link
Owner

Can you try setting white background before capturable modifier?

@MahmoudMabrok
Copy link
Author

I found issue is only for background, content is being captured properly,

I think issue comes from part of asAndroidBitmap because bitmap before this part is captured fine

@abd3lraouf
Copy link

abd3lraouf commented Jul 29, 2024

Wonder if we can use pixel copy ?

@qaz5823091
Copy link

qaz5823091 commented Aug 2, 2024

Try to set background attribute to the last, it may be worked!

I met a similar problem that my background is turned to black.

and my code is like this:

Box(
        modifier = Modifier
            .padding(top = 20.dp, start = 32.dp, end = 32.dp)
            .aspectRatio(boxRatio)
            .background(backgroundColor)
            .capturable(captureController)
) {
...
}

To solve this problem, I found another solution using NEW VERSION of Compose UI: Compose to Bitmap
and I find the code which is set background property lastly:

val coroutineScope = rememberCoroutineScope()
val graphicsLayer = rememberGraphicsLayer()
Box(
    modifier = Modifier
        .drawWithContent {
            // call record to capture the content in the graphics layer
            graphicsLayer.record {
                // draw the contents of the composable into the graphics layer
                this@drawWithContent.drawContent()
            }
            // draw the graphics layer on the visible canvas
            drawLayer(graphicsLayer)
        }
        .clickable {
            coroutineScope.launch {
                val bitmap = graphicsLayer.toImageBitmap()
                // do something with the newly acquired bitmap
            }
        }
        .background(Color.White)   // THE LAST
) {
    Text("Hello Android", fontSize = 26.sp)
}

So I set background which is below to capturable, it is turned to normal!

Part of my final code:

Box(
    modifier = Modifier
        .padding(top = 20.dp, start = 32.dp, end = 32.dp)
        .aspectRatio(boxRatio)
        .capturable(captureController)
        .background(backgroundColor)
) {
...
}

...

Button(onClick = {
    coroutineScope.launch {
        try {
            val bitmapAsync = captureController.captureAsync().await()
            val bitmap = bitmapAsync.asAndroidBitmap()
            val bytes = ByteArrayOutputStream()
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes)
            val path = MediaStore.Images.Media.insertImage(context.contentResolver, bitmap, "Title", null)
            val sendIntent = Intent(Intent.ACTION_SEND).apply {
                putExtra(Intent.EXTRA_STREAM, Uri.parse(path))
                type = "image/JPEG"
            }
            val shareIntent = Intent.createChooser(sendIntent, null)
            startActivity(context, shareIntent, null)
        } catch (error: Throwable) {
            // Error occurred, do something.
        }
    }
})

Hope to this solution is helpful for you!

@PatilShreyas
Copy link
Owner

Thanks @qaz5823091 for sharing your learnings.

@PatilShreyas
Copy link
Owner

Probably fixed in v3.0.0, please try and let us know if the issue re-occurs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants