1

I'm working in android studio and using kotlin and I'm trying to get the camera to auto focus in my app.

There is a similar question here: https://stackoverflow.com/a/34528716/6939854

I'm looking for a way to implement something similar with androidx.

Some of my code as well:

 private fun startCamera() {
        val previewConfig = PreviewConfig.Builder().apply {
            setTargetAspectRatio(Rational(1, 1))
            setTargetResolution(Size(1000, 1000))
        }.build()

        // Build the viewfinder use case

        val preview = Preview(previewConfig)

        // Every time the viewfinder is updated, recompute layout
        preview.setOnPreviewOutputUpdateListener {

            // To update the SurfaceTexture, we have to remove it and re-add it
            val parent = viewFinder.parent as ViewGroup
            parent.removeView(viewFinder)
            parent.addView(viewFinder, 0)

        viewFinder.surfaceTexture = it.surfaceTexture
        updateTransform()
        }
    CameraX.bindToLifecycle(this as LifecycleOwner,preview)
}
Blue
  • 1,408
  • 4
  • 17
  • 36

0 Answers0