Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

Camera preview starts to lag heavily when use OverlayEffect

30 views
Skip to first unread message
Assigned to scot...@google.com by mingd...@google.com

Bhushan Bhadane

unread,
Apr 28, 2025, 12:23:08 AMApr 28
to Android CameraX Discussion Group
When using CameraX's new OverlayEffect  API, if multiple overlays (e.g., 2 or more stamp Bitmaps) are added at the same time, the camera preview starts to lag heavily, sometimes freezing completely for a few seconds.
Captured photo or video output also shows stuck frames and does not behave smoothly.

Expected behavior:
- Adding multiple overlays dynamically should not cause heavy frame drops.
- Preview and capture output should remain smooth even with multiple overlays.

Observed behavior:
- Severe preview lag, frame drops, stuck frames in capture output when adding multiple stamps dynamically.

Steps to Reproduce:
1. Setup CameraX with Preview and ImageCapture / VideoCapture.
2. Add a custom `CameraEffect` or use `OverlayEffect` to draw multiple stamp Bitmaps on top of the preview.
3. Add multiple stamps dynamically (example: 2–5).
4. Observe preview lag and capture artifacts.

here i attached my code 

private fun getOverlayEffect(): OverlayEffect {
val errorListener = Consumer<Throwable> { error -> println("Error: ${error.message}") }
return OverlayEffect(
getTarget(), 0, Handler(Looper.getMainLooper()), errorListener
).also {
it.clearOnDrawListener()
it.setOnDrawListener(object : ViewTreeObserver.OnDrawListener,
Function<Frame, Boolean> {
override fun onDraw() {

}

override fun apply(input: Frame?): Boolean {
if (input != null) {
try {
val sensorToUi = cameraView.viewFinder.sensorToViewTransform
if (sensorToUi != null) {
val sensorToEffect = input.sensorToBufferTransform
val uiToSensor = Matrix()
sensorToUi.invert(uiToSensor)
uiToSensor.postConcat(sensorToEffect)
val canvas = input.overlayCanvas
canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR)
canvas.setMatrix(uiToSensor)
cameraEvent.onDrawStamp(
canvas,
cameraView.viewFinder.width,
cameraView.viewFinder.height
)
}
} catch (e: Exception) {
e.printStackTrace()
Log.e(
"Exception",
"FRAME NOT AVAILABLE: Unable to update texture contents (see logcat for details) (1.1 EXCEPTION) $e"
)
}
}
return true
}
})
}
}




override fun onDrawStamp(
input: Canvas,
width: Int,
height: Int
) {
canvasWidth = width
canvasHeight = height
if (mCamera!!.isVideoRecording) {
if (transparentBitmap != null) {
binding.imgStamp.draw(input)
}
}
}




Would love optimization or recommendations to handle multiple overlays without affecting performance.
Reply all
Reply to author
Forward
0 new messages