I'm always excited to take on new projects and collaborate with innovative minds.
Court Road, Gujrat, Pakistan
Learn how to use CameraX with Jetpack Compose to build real-time on-device AI apps. Covers fullscreen preview, ImageAnalysis, threading, performance, and ML integration.
Real-time AI apps like face detection, pose tracking, document scanning, and smart cameras depend on one critical foundation: a fast, reliable camera pipeline. On Android, that foundation is CameraX.
In this blog, you’ll learn how to combine CameraX + Jetpack Compose to power real-time on-device AI without UI lag, dropped frames, or broken previews.
This guide is written for Android developers who want to build production-grade AI apps, not just demos.
CameraX is Google’s recommended camera library because it solves problems that direct Camera2 usage makes painful:
For AI apps, CameraX enables:
CameraX is built around use cases. AI apps typically rely on three:
Displays the live camera feed to the user.
Provides frames for AI processing (face detection, pose tracking, OCR). This is where ML runs.
Used when the app needs to take photos in addition to live analysis.
For most real-time AI apps, Preview + ImageAnalysis is enough.
Jetpack Compose does not replace CameraX. Instead, Compose hosts CameraX using interoperability components.
The recommended pattern is:
This separation is critical to avoid UI freezes.
kotlin dependencies {
implementation("androidx.camera:camera-camera2:1.3.0") implementation("androidx.camera:camera-lifecycle:1.3.0") implementation("androidx.camera:camera-view:1.3.0")
}These libraries give you:
CameraX will not work without runtime permission. In Compose, permissions must be handled explicitly.
val permissionState = rememberPermissionState( android.Manifest.permission.CAMERA )
LaunchedEffect(Unit) { permissionState.launchPermissionRequest() }Always block camera initialization until permission is granted.
A common mistake is seeing the preview only partially rendered. The correct approach is using AndroidView + MATCH_PARENT.
AndroidView(
modifier = Modifier.fillMaxSize(),
factory = { context ->
PreviewView(context).apply {
scaleType = PreviewView.ScaleType.FILL_CENTER
}
}
)
This ensures the camera feed fills the entire screen.
This is where AI magic happens.
val analysis = ImageAnalysis.Builder()
.setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST)
.build()
analysis.setAnalyzer(Executors.newSingleThreadExecutor()) { imageProxy ->
// Send imageProxy to ML Kit or MediaPipe for processing
imageProxy.close() // Make sure to close it to avoid memory leaks
}
KEEP_ONLY_LATEST prevents backlog and UI lag.
Never run AI inference on the main thread.
Breaking this rule causes dropped frames, ANRs, and bad UX.
AI results should be exposed as a state.
val detectedFaces = mutableStateOf<List<Face>>(emptyList())
Compose automatically redraws overlays when state updates.
Overlays must be drawn using Canvas on top of the preview.
Canvas(modifier = Modifier.fillMaxSize()) {
detectedFaces.value.forEach { face ->
drawRect(
color = Color.Green,
topLeft = Offset(
face.boundingBox.left.toFloat(),
face.boundingBox.top.toFloat()
),
size = Size(
face.boundingBox.width().toFloat(),
face.boundingBox.height().toFloat()
)
)
}
}
Coordinate mapping must account for:
Running AI on every frame is unnecessary.
Best practice:
This dramatically improves battery life.
This architecture:
This is the same foundation used by real-world AI apps.
If you’re building an AI-powered Android app and need help with CameraX, ML integration, or performance optimization, I can help.
Your email address will not be published. Required fields are marked *