Created
September 18, 2025 16:29
-
-
Save Tonnie-Dev/cf8f6fcd9ce299654d7a1cecceeb33e6 to your computer and use it in GitHub Desktop.
A Jetpack Compose scan overlay for QR/Barcode UI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Composable | |
| fun ScanOverlay( | |
| modifier: Modifier = Modifier, | |
| hint: String | |
| ) { | |
| val scrimColor = MaterialTheme.colorScheme.scrim.copy(alpha = 0.65f) | |
| Box(modifier = modifier) { | |
| Canvas( | |
| Modifier | |
| .matchParentSize() | |
| .graphicsLayer { | |
| compositingStrategy = CompositingStrategy.Offscreen | |
| } | |
| ) { | |
| val w = size.width | |
| val h = size.height | |
| val punchHoleFraction = .9f | |
| val frameSquareDimen = min(w, h) * punchHoleFraction | |
| val left = (w - frameSquareDimen) / 2f | |
| val top = (h - frameSquareDimen) / 2f | |
| val radius = 20.dp.toPx() | |
| // Draw scrim | |
| drawRect(scrimColor) | |
| // Punch a transparent hole for the frame | |
| drawRoundRect( | |
| color = Color.Transparent, | |
| topLeft = Offset(left, top), | |
| size = Size(width = frameSquareDimen, height = frameSquareDimen), | |
| cornerRadius = CornerRadius(radius, radius), | |
| blendMode = BlendMode.Clear | |
| ) | |
| } | |
| // Hint Text | |
| Column( | |
| Modifier | |
| .fillMaxSize() | |
| .padding(horizontal = 24.dp), | |
| horizontalAlignment = Alignment.CenterHorizontally, | |
| verticalArrangement = Arrangement.Top | |
| ) { | |
| Spacer(Modifier.fillMaxHeight(0.25f)) | |
| Text( | |
| text = hint, | |
| style = MaterialTheme.typography.titleSmall.copy(color = OnOverlay), | |
| textAlign = TextAlign.Center | |
| ) | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment