2022-10-20

CameraX API - How to use it to fixate camera hardware parameter? Or how can I integrate it with Camera2

Background and Problem

I am a researcher and new to android developing, and I am currently doing simple proof-of-concept work by writing an app to capture 9 selfies under different 9 screen illuminations.

I have encountered a problem when I want to fixate as many hardware parameters as possible to make sure there will be no auto-changes in my camera settings when in between 2 selfies. Otherwise, it won't be a valid dataset for my use case.

I built my app and modified on top of CameraX (in particular, google's Getting Started with CameraXhttps://developer.android.com/codelabs/camerax-getting-started#1 )

I have already researched Camera2 as an API and its CaptureRequest has the functionality to fixate some particular ImageMetadata, such as to disable Auto Exposure, to disable Auto Focus, to disable Auto White Balance, and to fixate Lens Aperature.

My question is that

  • How to use only cameraX to fixate camera hardware parameter? I did not find the counterpart in cameraX..
  • Or how can I integrate cameraX with Camera2 to fixate those things above?
  • Or any other "quick and dirty" method than can achieve the purpose I intended?

I thought I can use my Camera2 together with CameraX code because CameraX uses Camera2 API under the hood, but when I tried, the app keeps quitting when running.

Any suggestion will be helpful, Thanks a lot!!!

Here is my MainActivity.kt

import android.hardware.camera2.CaptureRequest


class MainActivity : AppCompatActivity() {
    private var imageCapture: ImageCapture? = null 
    private lateinit var outputDirectory: File 
    private lateinit var cameraExecutor: ExecutorService

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        if (allPermissionsGranted()) {
            startCamera()
        } else {
            ActivityCompat.requestPermissions(this, REQUIRED_PERMISSIONS, REQUEST_CODE_PERMISSIONS)
        }
        ....
    }

    private fun takePhoto() {
        ...
    }

    private fun startCamera() {
       // TRY TO FIXATE THE CAMERA PARAMETER HERE
       lateinit var captureBuilder: CaptureRequest.Builder
       captureBuilder.set(CaptureRequest.CONTROL_AE_MODE, 65538)

       val cameraProviderFuture = ProcessCameraProvider.getInstance(this)

       cameraProviderFuture.addListener(Runnable {

            // Used to bind the lifecycle of cameras to the lifecycle owner
            val cameraProvider: ProcessCameraProvider = cameraProviderFuture.get()

            // Preview
            val preview = Preview.Builder()
                .build()
                .also {
                    it.setSurfaceProvider(viewFinder.createSurfaceProvider())
                }

            imageCapture = ImageCapture.Builder().build()

            // Select back camera as a default
            val cameraSelector = CameraSelector.DEFAULT_FRONT_CAMERA

            try {
                // Unbind use cases before rebinding
                cameraProvider.unbindAll()

                // Bind use cases to camera
                cameraProvider.bindToLifecycle(
                    this, cameraSelector, preview, imageCapture
                )

            } catch (exc: Exception) {
                Log.e(TAG, "Use case binding failed", exc)
            }

        }, ContextCompat.getMainExecutor(this))
    }

    ...
}

Can you particularly look at startCamera()?, in which I try to fixate the camera setting at beginning?



No comments:

Post a Comment