BoofCV icon indicating copy to clipboard operation
BoofCV copied to clipboard

Android: can't use flash light in current camera session

Open Psijic opened this issue 5 years ago • 12 comments

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.

Psijic avatar Nov 18 '20 11:11 Psijic

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?

Psijic avatar Nov 18 '20 15:11 Psijic

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);

Psijic avatar Nov 19 '20 12:11 Psijic

I found in onTorchModeChanged(cameraId: String, enabled: Boolean) that after I enable it, it becomes disabled right after that. Didn't find where.

Psijic avatar Nov 19 '20 14:11 Psijic

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 avatar Nov 20 '20 04:11 lessthanoptimal

@lessthanoptimal Hello, i have the same issue. any news on this?

emil-sys avatar Nov 14 '21 13:11 emil-sys

Hello, did you guys find any solution for this.

aiqbal281 avatar Mar 24 '22 17:03 aiqbal281

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: @.***>

emil-sys avatar Mar 25 '22 05:03 emil-sys

I found solution using camera x

aiqbal281 avatar Mar 25 '22 15:03 aiqbal281

@aiqbal281 What is the solution?

ChetanG-RC avatar Feb 14 '23 06:02 ChetanG-RC

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.

lessthanoptimal avatar Feb 14 '23 13:02 lessthanoptimal

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!

makiprn avatar Oct 01 '23 08:10 makiprn

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.

edpmath avatar Feb 10 '24 03:02 edpmath