coding_beginner
coding_beginner

Reputation: 1

turning on off flash light while open camera android?

i am using CameraView api 'com.otaliastudios:cameraview:2.6.4' for camera live preview , i want to turn on camera and according to document i can do it with CameraView by turning on but it does not works .

i tried to do it with CameraManager but it is not working either giving me exception that i can not use Flash light when camera is in use . i found some other similar question on stackoverflow . they seem to work but code is not making sense to me , i am not able to understand the code because code is not full .

do you have idea how can i do this , i have already defined permission AndroidManifest file for FLASHLIGHT and CAMERA .

Thank You In Advance .

Upvotes: -1

Views: 892

Answers (1)

Swapneneel
Swapneneel

Reputation: 11

I am facing the same issue. Try this solution, or read this blog: CameraX: Learn how to use CameraController

val cameraInstance : Camera = cameraProvider?.bindToLifecycle(viewLifecycleOwner, cameraSelector, previewUseCase)
private fun flashToggle() {
    // My toggle flash function 
    val cameraController = cameraInstance?.cameraControl
    if (cameraInstance?.cameraInfo?.torchState?.value == TorchState.ON) {
        cameraController?.enableTorch(false)
        binding.barscanfFlashToggle.setImageResource(R.drawable.ic_baseline_flash_off_24)
    } else {
        binding.barscanfFlashToggle.setImageResource(R.drawable.ic_baseline_flash_on_24)
        cameraController?.enableTorch(false)
    }
}

Upvotes: 0

Related Questions