Stop Taking Blurry Scans: How I Built Frame Stability Detection for Android with CameraX
Every document scanner app has the same problem: users tap the capture button while their hand is still moving, and the scan comes out blurry. I spent two weeks solving this while building a produc...

Source: DEV Community
Every document scanner app has the same problem: users tap the capture button while their hand is still moving, and the scan comes out blurry. I spent two weeks solving this while building a production Android scanner app (21K lines of Kotlin). Here's the approach that actually works. The Problem CameraX gives you a camera preview, but it has no concept of "is the frame stable enough to capture?" You need to build that yourself. The Solution: RMS-Based Frame Stability Instead of complex optical flow or gyroscope fusion, I used a simple but effective approach: compare consecutive frames using Root Mean Square (RMS) difference. class FrameStabilityAnalyzer : ImageAnalysis.Analyzer { private var lastFrameBytes: ByteArray? = null private var isStable = false private val STABILITY_THRESHOLD = 5.0 // Tune this value override fun analyze(imageProxy: ImageProxy) { val currentBytes = imageProxy.planes[0].buffer.toByteArray() lastFrameBytes?.let { previous -> val rms = calculateRMS(previous,