How does CameraX library can turn ON/OFF the torch?

Kewin Czupryński picture Kewin Czupryński · May 18, 2019 · Viewed 9.5k times · Source

I am developing a feature with the possibility of switching the torch into ON/OFF states. Some days ago, we saw a new library from Google in io2019. I came up with an idea, why not use it.

After some time, I don't see any possibilities to use the only torch from the library.

Even in the official documentation, I wasn't able to find any good pieces of information for me, what's more, the sample app from their also don't have to handle my case.

Do you have something in mind what is easy to implement or perhaps you know how to do it with CameraX?

I am worried about using camera or camera2 because the amount of code to be paste is terrible.

Links:

[1] https://developer.android.com/training/camerax

[2] https://proandroiddev.com/android-camerax-preview-analyze-capture-1b3f403a9395

[3] https://github.com/android/camera/tree/master/CameraXBasic

[4] https://github.com/android/camera/tree/master/CameraXBasic

CameraX is an Android Jetpack library that was built with the intent to make camera development easier.

Answer

murgupluoglu picture murgupluoglu · Feb 21, 2020
androidx.camera:camera-core:1.0.0-alpha10

You can check is torch available or not with this:

val camera = cameraProvider.bindToLifecycle(lifecycleOwner, cameraSelector, preview, imageAnalyzer)

camera.cameraInfo.hasFlashUnit()

And you can enable torch with:

camera.cameraControl.enableTorch(true)