Search code examples
androidandroid-camera2android-camerax

Configure CameraX (flash mode, auto white balance mode, action mode etc)


I've already successfully implemented CameraX, however, I am stuck on configuring its settings. I'd like to set auto white balance, noise reduction, flash and focus mode (just like in Camera2). So far, the only things I found on SO refer only to Camera2 and the official documentation doesn't tell me much either. Is it even possible to set these modes on CameraX or is only Camera2 supported so far?


Solution

  • You're correct, the documentation is quite poor (I suppose because the library has been in alpha up until recently).

    To change the flash mode:

    Keep a reference to the ImageCapture object when binding:

        imageCapture = ImageCapture.Builder()
            .setFlashMode(ImageCapture.FLASH_MODE_AUTO)
            .setCaptureMode(ImageCapture.CAPTURE_MODE_MAXIMIZE_QUALITY)
            .setTargetAspectRatio(aspectRatio)
            .setTargetRotation(rotation)
            .build()
    

    When you want to change the flash mode (e.g when the user taps the flash icon):

    flashMode = ImageCapture.FLASH_MODE_OFF
    

    Focus and metering are a little bit more involved, you can see the classes you should be using here.

    As for noise reduction and white balance, I believe you'll need to still need to use the Camera2 libraries.

    implementation "androidx.camera:camera-camera2:${camerax_version}"