[Feat]: Change the brightness of the flashlight ?
Version
2.6.1
Environment that reproduces the issue
Samsung Galaxy S22 Ultra - Android 14
Use case description
Hi. I can enable/disable the flash (when using the rear camera) by calling:
cameraSrtLiveStreamer.settings.camera.flash.enable = true/false
Is there any way I can change that brightness level ? I really need it.
Proposed solution
No response
Alternative solutions
No response
Hi,
Setting the torch strength is a relatively new features on Android devices. The API I found are:
- https://developer.android.com/reference/android/hardware/camera2/CaptureRequest#FLASH_STRENGTH_LEVEL API >= 35
- https://developer.android.com/reference/android/hardware/camera2/CameraManager#turnOnTorchWithStrengthLevel(java.lang.String,%20int) API >= 33
You can use turnOnTorchWithStrengthLevel in your code.
Hi,
Setting the torch strength is a relatively new features on Android devices. The API I found are:
- https://developer.android.com/reference/android/hardware/camera2/CaptureRequest#FLASH_STRENGTH_LEVEL API >= 35
- https://developer.android.com/reference/android/hardware/camera2/CameraManager#turnOnTorchWithStrengthLevel(java.lang.String,%20int) API >= 33
You can use
turnOnTorchWithStrengthLevelin your code.
Hi.
turnOnTorchWithStrengthLevel is exactly what I need.
But when using it with **io.github.thibaultbee.streampack.views.PreviewView**, I can only turn on the flash with the desired brightness level with the front camera (even though flash.available = false ).
When using the rear camera, it gives the error:
android.hardware.camera2.CameraAccessException: CAMERA_IN_USE (4): turnOnTorchWithStrengthLevel:2928: Torch for camera "0" is not available due to an existing camera user
It seems that because PreviewView is using the rear camera, I cannot use that function from outside. My wish is to be able to adjust the brightness level with both the front and rear cameras using PreviewView.
Can you integrate brightness adjustment feature into your library? I use it with API >= 33
Here is the code I use to adjust the brightness of the flash (works fine without using PreviewView):
try {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU || torchStrength < 1) {
return
}
val camManager = context.getSystemService(Context.CAMERA_SERVICE) as CameraManager
val cameraList = camManager.cameraIdList
if (cameraList.isEmpty()) return
val cameraId = cameraList[0]
val cameraCharacteristics = camManager.getCameraCharacteristics(cameraId)
val maxLevel =
cameraCharacteristics.get(CameraCharacteristics.FLASH_INFO_STRENGTH_MAXIMUM_LEVEL)
Loggers.e("maxLevel: $maxLevel")
if (maxLevel == null || maxLevel <= 1) {
Loggers.e("changeBrightnessFlashLight", "This camera not support change brightness")
return
}
var level = torchStrength
if (level > maxLevel) {
level = maxLevel
}
camManager.turnOnTorchWithStrengthLevel(cameraId, level)
} catch (e: Exception) {
e.printStackTrace()
}
}
Indeed, if you call turnOnTorchWithStrengthLevel after the camera has been started, it throws an exception. Could you call turnOnTorchWithStrengthLevel before the camera is used by StreamPack?
Indeed, if you call
turnOnTorchWithStrengthLevelafter the camera has been started, it throws an exception. Could you callturnOnTorchWithStrengthLevelbefore the camera is used by StreamPack?
Unfortunately I can't. Because I need to turn on the flash at the lowest brightness when the livestream is already running. And I need to turn it on whenever there is an event request from my server (via socket). If I'm using the front camera, I can do it perfectly. But the rear camera throws that exception.
My guess is that turnOnTorchWithStrengthLevel is incompatible with the usage of a camera and there is nothing we can do.
I could add an API for FLASH_STRENGTH_LEVEL in the CameraSettings but as it is API >= 35, it does not match your requirement.
My guess is that
turnOnTorchWithStrengthLevelis incompatible with the usage of a camera and there is nothing we can do.I could add an API for
FLASH_STRENGTH_LEVELin theCameraSettingsbut as it is API >= 35, it does not match your requirement.
Adding FLASH_STRENGTH_LEVEL with API >= 35 is also a good fallback option for later.
I'm really stuck and can't find a way to do it with API 33 :cry:
If you find another option, I could give a look.
FLASH_STRENGTH_LEVEL will be implement. Unfortunately my phone will not receive the update to API 35.
It is on hold till I get a new phone.
If you find another option, I could give a look.
Thank you very much. I will try to find a solution. If there is any new information, please comment to let me know.