0

As part of a react native module i had to get the LENS_INTRINSIC_CALIBRATION data from the camera2 api in a camerax fragment

after trying many things i found the easiest way to do it is with the Camera2Interop camera2 library, which provides access to the Camera2CameraInfo class and you can use it like this:


import androidx.camera.camera2.interop.Camera2CameraInfo
import androidx.camera.camera2.interop.Camera2Interop
import androidx.camera.camera2.interop.ExperimentalCamera2Interop

.....

class CameraFragment : Fragment() {

...
...

private fun startCamera() {
        val cameraProviderFuture = ProcessCameraProvider.getInstance(safeContext)
        Log.d(TAG, "start camera")
        cameraProviderFuture.addListener(Runnable {
            // Used to bind the lifecycle of cameras to the lifecycle owner
            val cameraProvider: ProcessCameraProvider = cameraProviderFuture.get()
            var intrinsics: FloatArray? = null
            // Preview
            preview = Preview.Builder().build()
           
            imageCapture = Builder().build()
            // Select back camera
            val cameraSelector = CameraSelector.Builder().requireLensFacing(CameraSelector.LENS_FACING_BACK).build()

            try {
                // Unbind use cases before rebinding
                cameraProvider.unbindAll()
                Log.d(TAG, "unbind")

                // Create a camera instance using CameraX
                val camera = cameraProvider.bindToLifecycle(this, cameraSelector, preview, imageCapture)
                Log.d(TAG, "afterbind")
                val cameraInfo = Camera2CameraInfo.from(camera.cameraInfo)

                Log.d(TAG, "aftercaminfo")
                // Get the lens intrinsic calibration
                val intrinsicCalibration = cameraInfo.getCameraCharacteristic(CameraCharacteristics.LENS_INTRINSIC_CALIBRATION)
                Log.d(TAG, "intc")
                if (intrinsicCalibration != null) {
                    Log.d(TAG, "intcnotnull")
                    intrinsics = FloatArray(5)
                    intrinsicCalibration.copyInto(intrinsics)
                }
                if (intrinsics != null) {
                    Log.d(TAG, intrinsics.joinToString(separator = " "))
                    val params = Arguments.createMap().apply {
                        putString("values", intrinsics.joinToString(separator = ", "))
                    }
                    reactContextProvider?.provideReactContext()?.let { sendEvent(it, "IntrinsicCameraValues",  params ) }
                } else {
                    Log.d(TAG, "No intrinsic values")
                }
                preview?.setSurfaceProvider(viewFinder.surfaceProvider)
            } catch (exc: Exception) {
                Log.e(TAG, "Use case binding failed", exc)
            }

        }, ContextCompat.getMainExecutor(safeContext))

    }```
Phys3
  • 1

0 Answers0