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

17,441

Solution 1

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)

Solution 2

2021 syntax.

Turn on torch on Android, using Java.

Your typical camera preview code (such as from the google example) generally ends like this:

cameraProvider.bindToLifecycle((LifecycleOwner)this,
                 cameraSelector, imageAnalysis, preview);

to turn on/off the torch...

Camera cam = cameraProvider.bindToLifecycle((LifecycleOwner)this,
                 cameraSelector, imageAnalysis, preview);

if ( cam.getCameraInfo().hasFlashUnit() ) {
    cam.getCameraControl().enableTorch(true); // or false
}

and that's it!

Solution 3

This is one way you can do it (Kotlin). If there is a better way please let me know. Following code assumes you have already established the availability of flash on the device.

Declare a flashMode var

private var flashMode: Int = ImageCapture.FLASH_MODE_OFF

In updateCameraUI set a listener

controls.findViewById<ImageButton>(R.id.flash_button).setOnClickListener {
    when (flashMode) {
        ImageCapture.FLASH_MODE_OFF ->
            flashMode = ImageCapture.FLASH_MODE_ON
        ImageCapture.FLASH_MODE_ON ->
            flashMode = ImageCapture.FLASH_MODE_AUTO
        ImageCapture.FLASH_MODE_AUTO ->
            flashMode = ImageCapture.FLASH_MODE_OFF
    }
    // Re-bind use cases to include changes
    bindCameraUseCases()
}

In bindCameraUseCases set the flash mode

            imageCapture = ImageCapture.Builder()
                .setCaptureMode(ImageCapture.CAPTURE_MODE_MAXIMIZE_QUALITY)
                .setTargetAspectRatio(screenAspectRatio)
                .setTargetResolution(screenSize)
                .setTargetRotation(rotation)
                .setFlashMode(flashMode)
                .build()

Solution 4

2022 Syntax

imageCapture = ImageCapture.Builder()
                .setFlashMode(ImageCapture.FLASH_MODE_ON)
                    .build()


val camera = cameraProvider.bindToLifecycle(
             this, cameraSelector, preview, imageCapture, imageAnalyzer)
                
if (camera.cameraInfo.hasFlashUnit()) {
     camera.cameraControl.enableTorch(true)
}

Solution 5

I can't comment so I'm answering to expand on yevhen_69's answer.

Setting enabledTorch(true) didn't work for me either, however I found I had to set enableTorch(true) after the call to CameraX.bindToLifecycle

val previewConfig = PreviewConfig.Builder().apply {
        setLensFacing(lensFacing)
        // Any setup
        setTargetRotation(viewFinder.display.rotation)
}.build()

val preview = Preview(previewConfig)

CameraX.bindToLifecycle(this, preview)
preview.enableTorch(true)

However on a side note, CameraX is still in Alpha so its advisable still to use Camera2 API.

Share:
17,441

Related videos on Youtube

Kewin Czupryński
Author by

Kewin Czupryński

Updated on January 01, 2022

Comments

  • Kewin Czupryński
    Kewin Czupryński over 2 years

    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.

  • Pluto1010
    Pluto1010 about 5 years
    Sorry but enableTorch does not seem to work on my Huawei P10 Lite using Android 8.0. Preview is working fine but it seems that it just does not care about enableTorch(true). ¯_(ツ)_/¯ Any ideas?
  • Sakiboy
    Sakiboy over 3 years
    Brilliant answer. Thanks for taking advantage of observing the torch state. Could use a little more explanation though. Still a great answer.
  • Fattie
    Fattie over 3 years
    thank you so much! I put in the latest syntax in an answer.
  • Fattie
    Fattie over 3 years
    editors - cheers, don't remove phrases which are there for the benefit of search engines
  • Adam Burley
    Adam Burley about 3 years
    Where is this class CameraX you are using? I think CameraX is a library not a class, I cannot find any static method CameraX.getCameraInfo like what you have called here. also, this code snippet will only check the flash state and set the visibility of a toggle based on the flash state, it won't actually set the flash state
  • Adam Burley
    Adam Burley about 3 years
    Looks like that method enableTorch has now since removed from the API
  • Adam Burley
    Adam Burley about 3 years
    enableTorch is no longer a method of the class Preview; see developer.android.com/reference/androidx/camera/core/…
  • Akshay Raiyani
    Akshay Raiyani about 3 years
    This is the example when cameraX is in alpha, in latest version may be this class removed.
  • Ranjan
    Ranjan almost 3 years
    For anyone who just wants to turn flash on, you still need the camera permission to make it work and also at least 1 use case to bind to cameraProvider.
  • Alexandr
    Alexandr over 2 years
    What is the binding?
  • ahmad bajwa
    ahmad bajwa over 2 years
    that's for viewBinding, You can just call your cameraView here.
  • Pooja
    Pooja over 2 years
    Helpful... Thanks