Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CameraX slow camera load speed

So I migrated from using legacy camera api to CameraX and even though it was quite simple to setup, I've noticed one issue. Now camera seems to take almost twice if not longer to start showing preview than it had before. I'm testing on galaxy s7. My code looks like this:

val previewConfig = PreviewConfig.Builder().apply {
    setTargetAspectRatio(Rational(1, 1))
    setTargetResolution(Size(binding.codeScannerView.width, binding.codeScannerView.height))
}.build()

val preview = Preview(previewConfig)

preview.setOnPreviewOutputUpdateListener { preview ->
    val parent = binding.codeScannerView.parent as ViewGroup
    parent.removeView(binding.codeScannerView)
    parent.addView(binding.codeScannerView, 0)
    binding.codeScannerView.surfaceTexture = preview.surfaceTexture
}

val analyzerConfig = ImageAnalysisConfig.Builder().apply {
    val analyzerThread = HandlerThread(
            "QrCodeReader").apply { start() }
    setCallbackHandler(Handler(analyzerThread.looper))
    setImageReaderMode(
            ImageAnalysis.ImageReaderMode.ACQUIRE_LATEST_IMAGE)
}.build()

val analyzerUseCase = ImageAnalysis(analyzerConfig).apply {
    analyzer = QrCodeAnalyzer(requireContext(), Handler(), { qrCode ->
        if (activity == null) {
            return@QrCodeAnalyzer
        }
        presenter.disableCameraPreview()
        presenter.updateTable(qrCode.toLowerCase().parseTableId(), isFromOrder, Screens.MENU_SCREEN)
    })
}

CameraX.bindToLifecycle(this, preview, analyzerUseCase)

Any ideas on how to make it appear faster?

P. S. I can also see tearing in preview once in a while

like image 477
SMGhost Avatar asked Feb 06 '26 09:02

SMGhost


1 Answers

So I've spent quite some time trying to find the solution, to no avail. I have even encountered multiple issues (with alpha04) like:

  • Random SIGSEGV crashes when turning camera on/off
  • I tried sample projects and codelabs from google which also were not working 100% of the time on tested devices
  • At some point I got notification that camera was being used in background, even though It was bound to lifecycle and window closed, which is the last thing I want my users to see.
  • Camera was indeed loading slower and I was getting horrible FPS even with analyzer off.
  • Resolution would drop down to lowest possible and preview would be pixelated on some devices
  • Every once in a while preview would start tearing vertically
  • Analyzer frame was different size than preview and there were some aspect ratio issues which took quite some time to resolve.
  • There's still quite some boilerplate required for it to work
  • Documentation for edge cases is pretty much non existent, so most of the stuff is trial and error.

In the end I just started looking for other libraries and came upon https://github.com/natario1/CameraView This is by far the easiest to use library I have ever seen for camera. Way simplier than camerax, it seems to just work, loads way faster, renders preview at 2x-3x higher FPS even with analyzer step running in the background. So far I had no issues with it.

Even though I strongly believe, that I was missing something, when using CameraX and there's probably a way to make it work, in the end it just doesn't seem worth it for now and I'll probably wait till there's a production ready version until I try again.

like image 95
SMGhost Avatar answered Feb 08 '26 23:02

SMGhost



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!