Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to toggle flashlight/torch? #39

Open
Damercy opened this issue Mar 24, 2021 · 0 comments
Open

How to toggle flashlight/torch? #39

Damercy opened this issue Mar 24, 2021 · 0 comments

Comments

@Damercy
Copy link

Damercy commented Mar 24, 2021

Is there any possibility to toggle the flashlight via code?

Maybe expose a function in QRActivity class that can allow us to toggle the flashlight programmatically instead of clicking in the camera preview? 😅

I've tried to switch on the flashlight using a utility flashlight class before launching the QRActivity (camera preview), however it always turns off the flashlight.

My flashlight util class:

class FlashlightUtil(private val context: Context) {

    fun toggleFlashlight(isOn: Boolean){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            val camManager: CameraManager? = context.getSystemService(Context.CAMERA_SERVICE) as CameraManager?
            var cameraId: String? = null
            try {
                cameraId = camManager?.cameraIdList?.get(0)
                camManager?.setTorchMode(cameraId!!, isOn) 
            } catch (e: CameraAccessException) {
                e.printStackTrace()
            }
        }
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant