Android: can't use flash light in current camera session
Hello. I didn't find any flash options available in example. Do they exist? If no, it could be nice to add it in Android.
When I tried to access it by myself, with cameraManager.setTorchMode(cameraId, true) there is an error:
CameraAccessException: android.hardware.camera2.CameraAccessException: CAMERA_IN_USE (4): setTorchMode:2122: Torch for camera "0" is not available due to an existing camera user.
So, I need to find a way to use a current camera's flash.
When I'm trying to set flashlight with API < M with this code,
val camera = Camera.open()
val params = camera.parameters
params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH)
camera.setParameters(params)
camera.startPreview()
I got the next error:
E/CameraCaptureSession: Session 0: Exception while stopping repeating:
android.hardware.camera2.CameraAccessException: CAMERA_DISCONNECTED (2): checkPidStatus:2193: The camera device has been disconnected
How can I get a current camera source?
I tried to close the camera, set parameters and open it again but have only a short flash
closeCamera()
val cameraId: String = cameraManager.cameraIdList[0]
cameraManager.setTorchMode(cameraId, true)
openCamera(viewWidth, viewHeight)
I think, this function in SimpleCamera2Activity should be rewritten to make camera variable and other needed parameters public:
protected void openCamera(int widthTexture, int heightTexture) {
Also maybe mStateCallback is possible to use here.
Update: I even tried to rewrite the library class but also got only a short flash:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
manager.setTorchMode(cameraId, true);
}
manager.openCamera(cameraId, mStateCallback, null);
I found in onTorchModeChanged(cameraId: String, enabled: Boolean) that after I enable it, it becomes disabled right after that. Didn't find where.
I'll take a look at this, this weekend. Haven't attempted to mess with the flash before so I'll need to do some reading probably.
@lessthanoptimal Hello, i have the same issue. any news on this?
Hello, did you guys find any solution for this.
Hello, no didnt find a solution for this.
Intellij I @.***> schrieb am Do., 24. März 2022, 18:49:
Hello, did you guys find any solution for this.
— Reply to this email directly, view it on GitHub https://github.com/lessthanoptimal/BoofCV/issues/187#issuecomment-1077884219, or unsubscribe https://github.com/notifications/unsubscribe-auth/APC5UNDQ3OFZI7EZ5WBAC3TVBSTIZANCNFSM4TZ3YVVA . You are receiving this because you are subscribed to this thread.Message ID: @.***>
I found solution using camera x
@aiqbal281 What is the solution?
There's a tangentially related PR (and might not fix this issue) that modernizes the camera API and uses Fragments.
https://github.com/lessthanoptimal/BoofCV/issues/154
Advertising it a bit here since it's currently stuck in limbo trying how to build it into a library outside of Android Studio.
I added the code to SimpleCamera2Activity.java and was able to turn on the lights in my environment.
The camera settings must be done at startup.
It does not appear to be possible to change the settings while the camera is running.
SimpleCamera2Activity.java
protected void configureCamera( CameraDevice device,
CameraCharacteristics characteristics,
CaptureRequest.Builder captureRequestBuilder ) {
if (verbose)
Log.i(TAG, "configureCamera() default function");
captureRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_VIDEO);
captureRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON);
// Add code
captureRequestBuilder.set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_TORCH);
}
I would like someone to verify this!
This worked for me:
if (isFlashEnabled)
captureRequestBuilder.set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_TORCH);
// Before of
cameraDevice.createCaptureSession
Is not the best method, i think CameraX is better option.