QRCodeScanner icon indicating copy to clipboard operation
QRCodeScanner copied to clipboard

How to toggle flashlight/torch?

Open Damercy opened this issue 4 years ago • 0 comments

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()
            }
        }
    }
}

Damercy avatar Mar 24 '21 19:03 Damercy